潘志宝
5 天以前 7fce3006ecd0b670e33c2d3ba123778e79e2e943
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.TargetRecognition;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.HeadlessException;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.WindowAdapter;
10 import java.awt.event.WindowEvent;
11 import java.io.File;
12 import java.util.concurrent.ExecutionException;
13
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JOptionPane;
18 import javax.swing.JTextField;
19 import javax.swing.SwingUtilities;
20 import javax.swing.SwingWorker;
21
22 import com.iailab.netsdk.common.PaintPanel;
23 import com.iailab.netsdk.common.Res;
24 import com.iailab.netsdk.demo.module.TargetRecognitionModule;
25 import com.iailab.netsdk.lib.ToolKits;
26
27 public class DownloadPictureDialog extends JDialog{
28
29     /**
30      * 
31      */
32     private static final long serialVersionUID = 1L;
33
34     public DownloadPictureDialog() {
35         setTitle(Res.string().getDownLoadPicture());
36         setLayout(new BorderLayout());
37         setModal(true);  
38         pack();
39         setSize(380, 500);
40         setResizable(false);
41         setLocationRelativeTo(null);
42         setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
43         
44         JLabel picPathLabel = new JLabel(Res.string().getPicturePath()+":", JLabel.CENTER);
45         picPathTextField = new JTextField();
46         downloadBth = new JButton(Res.string().getDownLoadPicture());
47         
48         picPathTextField.setPreferredSize(new Dimension(180, 20));
49         downloadBth.setPreferredSize(new Dimension(80, 20));
50         
51         paintPanel = new PaintPanel();
52         paintPanel.setPreferredSize(new Dimension(360, 450));
53         
54         setLayout(new FlowLayout());
55         add(picPathLabel);
56         add(picPathTextField);
57         add(downloadBth);
58         add(paintPanel);
59         
60         downloadBth.addActionListener(new ActionListener() {    
61             @Override
62             public void actionPerformed(ActionEvent arg0) {
63                 SwingUtilities.invokeLater(new Runnable() {                
64                     @Override
65                     public void run() {
66                         paintPanel.setOpaque(true);
67                         paintPanel.repaint();
68                         downloadBth.setEnabled(false);
69                     }
70                 });
71                 
72                 downloadPicture();
73             }
74         });
75         
76         addWindowListener(new WindowAdapter() {
77             public void windowClosing(WindowEvent e) {
78                 dispose();
79             }
80         });
81     }
82     
83     private void downloadPicture() {
84         new SwingWorker<Boolean, Integer>() {    
85             String szFileName = "";                  // 要下载的图片路径
86             String pszFileDst = "";                   // 保存图片的路径
87             
88             @Override
89             protected Boolean doInBackground() {
90                 if(picPathTextField.getText().isEmpty()) {
91                     JOptionPane.showMessageDialog(null, Res.string().getEnterPicturePath(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
92                     return false;
93                 }
94                 
95                 szFileName = picPathTextField.getText();    
96                 pszFileDst = "./person.jpg";                  
97                 
98                 File file = new File(pszFileDst);
99                 if(file.exists()) {
100                     file.delete();
101                 }
102                 
103                 if(!TargetRecognitionModule.downloadPersonPic(szFileName, pszFileDst)) {
104                     return false;
105                 }
106
107                 return true;
108             }    
109             
110             @Override
111             protected void done() {
112                 if(picPathTextField.getText().isEmpty()) {
113                     return;
114                 }
115                 
116                 try {
117                     if(get()) {                
118                         // 下载成功后,面板上显示下载的图片
119                         ToolKits.openPictureFile(pszFileDst, paintPanel);    
120                     } else {
121                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
122                     }
123                 } catch (HeadlessException e) {
124                     e.printStackTrace();
125                 } catch (InterruptedException e) {
126                     e.printStackTrace();
127                 } catch (ExecutionException e) {
128                     e.printStackTrace();
129                 }
130                 downloadBth.setEnabled(true);
131             }
132         }.execute();
133     }
134     
135     private JTextField picPathTextField;
136     private PaintPanel paintPanel;
137     private JButton downloadBth;
138 }