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