潘志宝
5 天以前 6d75723f3e3bd43895db2470bc5fabb2314dbe8b
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.ThermalCamera;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10 import java.io.IOException;
11 import java.io.UnsupportedEncodingException;
12 import java.util.concurrent.locks.ReentrantLock;
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.JPanel;
19 import javax.swing.JTextField;
20 import javax.swing.SwingUtilities;
21
22 import com.sun.jna.Memory;
23 import com.sun.jna.Native;
24 import com.sun.jna.Pointer;
25
26 import com.iailab.netsdk.common.BorderEx;
27 import com.iailab.netsdk.common.Res;
28 import com.iailab.netsdk.demo.module.ThermalCameraModule;
29 import com.iailab.netsdk.lib.ToolKits;
30 import com.iailab.netsdk.lib.NetSDKLib.*;
31
32 /**
33  * 热图信息对话框
34  */
35 public class HeatMapDialog extends JDialog{
36
37     /**
38      * 
39      */
40     private static final long serialVersionUID = 1L;
41     private JDialog target = this;
42     private ReentrantLock lock = new ReentrantLock();
43     private NET_RADIOMETRY_DATA gData = new NET_RADIOMETRY_DATA();
44     
45     public HeatMapDialog() {
46         setTitle(Res.string().getShowInfo("HEATMAP"));
47         setLayout(new BorderLayout());
48         setModal(true);
49         pack();
50         setSize(400, 440);
51         setResizable(false);
52         setLocationRelativeTo(null);
53         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
54         
55         ///////////////////////////////
56         operatePanel = new OperatePanel();
57         showPanel = new HeatMapShowPanel();
58         
59         add(showPanel, BorderLayout.CENTER);
60         add(operatePanel, BorderLayout.NORTH);
61
62         addWindowListener(new WindowAdapter() {
63             public void windowClosing(WindowEvent e){
64                 try {
65                     ThermalCameraModule.radiometryDetach();
66                 }finally {
67                     dispose();
68                 }
69             }
70         });
71     }
72     
73     
74     /**
75      * 订阅回调
76      */
77     private RadiometryAttachCB cbNotify = new RadiometryAttachCB();
78     private class RadiometryAttachCB implements fRadiometryAttachCB {
79         
80         @Override
81         public void invoke(LLong lAttachHandle, final NET_RADIOMETRY_DATA pBuf,
82                 int nBufLen, Pointer dwUser) {
83
84             copyRadiometryData(pBuf);
85             SwingUtilities.invokeLater(new Runnable() {
86                 public void run() {
87                     target.setTitle(Res.string().getShowInfo("HEATMAP"));
88                     operatePanel.saveBtn.setEnabled(true);
89                     showPanel.updateData();
90                 }
91             });
92         }                    
93     }
94     
95     private void copyRadiometryData(NET_RADIOMETRY_DATA data) {
96         lock.lock();
97         gData.stMetaData = data.stMetaData;
98         gData.dwBufSize = data.dwBufSize;
99         gData.pbDataBuf = new Memory(data.dwBufSize);
100         gData.pbDataBuf.write(0, data.pbDataBuf.getByteArray(0, data.dwBufSize), 0, data.dwBufSize);
101         lock.unlock();
102     }
103     
104     /**
105      * 操作界面
106      * */
107     public class OperatePanel extends JPanel {
108         /**
109          * 
110          */
111         private static final long serialVersionUID = 1L;
112         
113         public OperatePanel() {
114             BorderEx.set(this, Res.string().getShowInfo("HEATMAP_OPERATE"), 1);
115             setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));            
116             
117             attachBtn = new JButton(Res.string().getShowInfo("RADIOMETRY_ATTACH"));
118             fetchBtn = new JButton(Res.string().getShowInfo("RADIOMETRY_FETCH"));
119             saveBtn = new JButton(Res.string().getShowInfo("SAVE_HEATMAP"));
120
121             Dimension btnDimension = new Dimension(120, 20);
122             attachBtn.setPreferredSize(btnDimension);
123             fetchBtn.setPreferredSize(btnDimension);
124             saveBtn.setPreferredSize(btnDimension);
125             
126             fetchBtn.setEnabled(false);
127             saveBtn.setEnabled(false);
128             
129             add(attachBtn);
130             add(fetchBtn);
131             add(saveBtn);
132                         
133             attachBtn.addActionListener(new ActionListener() {
134
135                 @Override
136                 public void actionPerformed(ActionEvent e) {
137                     SwingUtilities.invokeLater(new Runnable() {
138                         public void run() {
139                             attach();
140                         }
141                     });
142                 }
143             });
144             
145             fetchBtn.addActionListener(new ActionListener() {
146
147                 @Override
148                 public void actionPerformed(ActionEvent e) {
149                     SwingUtilities.invokeLater(new Runnable() {
150                         public void run() {
151                             fetch();
152                         }
153                     });
154                 }
155             });    
156             
157             saveBtn.addActionListener(new ActionListener() {
158
159                 @Override
160                 public void actionPerformed(ActionEvent e) {
161                     SwingUtilities.invokeLater(new Runnable() {
162                         public void run() {
163                             try {
164                                 save();
165                             } catch (IOException e) {
166                                 // TODO Auto-generated catch block
167                                 e.printStackTrace();
168                             }
169                         }
170                     });
171                 }
172             });    
173         }
174         
175         public void attach() {
176             
177             freeMemory();
178             
179             target.setTitle(Res.string().getShowInfo("HEATMAP"));
180             if (ThermalCameraModule.isAttaching()) {
181                 ThermalCameraModule.radiometryDetach();
182                 fetchBtn.setEnabled(false);
183                 saveBtn.setEnabled(false);
184                 attachBtn.setText(Res.string().getShowInfo("RADIOMETRY_ATTACH"));
185             }else {
186                 if (ThermalCameraModule.radiometryAttach(ThermalCameraFrame.THERMAL_CHANNEL, cbNotify)) {
187                     attachBtn.setText(Res.string().getShowInfo("RADIOMETRY_DETACH"));
188                     showPanel.clearData();
189                     fetchBtn.setEnabled(true);
190                 }else {
191                     JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
192                 }
193             }
194         }
195         
196         public void fetch() {
197             
198             freeMemory();
199             saveBtn.setEnabled(false);
200             int nStatus = ThermalCameraModule.radiometryFetch(ThermalCameraFrame.THERMAL_CHANNEL);
201             
202             if (nStatus != -1) {
203                 showPanel.clearData();
204                 String[] arrStatus = Res.string().getTemperStatusList();
205                 if (nStatus >= 1 && nStatus <= arrStatus.length) {
206                     target.setTitle(Res.string().getShowInfo("HEATMAP") + " : " + arrStatus[nStatus-1]);
207                 }
208             }else {
209                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
210             }        
211         }
212         
213         public void save() throws IOException {
214             lock.lock();
215             boolean bSaved = ThermalCameraModule.saveData(gData);
216             lock.unlock();
217             if (bSaved) {
218                 JOptionPane.showMessageDialog(null, Res.string().getShowInfo("HEATMAP_SAVE_SUCCESS"), Res.string().getPromptMessage(), JOptionPane.PLAIN_MESSAGE);
219             }else {
220                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
221             }
222         }
223         
224         public void freeMemory() {
225             lock.lock();
226             if (gData.pbDataBuf != null) {    
227                 Native.free(Pointer.nativeValue(gData.pbDataBuf));
228                 Pointer.nativeValue(gData.pbDataBuf, 0);
229             }
230             lock.unlock();
231         }
232         
233         private JButton attachBtn;
234         private JButton fetchBtn;
235         private JButton saveBtn;
236     }
237     
238     /**
239      * 查询显示界面
240      * */
241     public class HeatMapShowPanel extends JPanel {
242         /**
243          * 
244          */
245         private static final long serialVersionUID = 1L;
246
247         public HeatMapShowPanel() {
248             BorderEx.set(this, Res.string().getShowInfo("HEATMAP_METADATA_INFO"), 1);
249             setLayout(new FlowLayout(FlowLayout.CENTER, 15, 25));
250             
251             JLabel heightLabel = new JLabel(Res.string().getShowInfo("HEIGHT"), JLabel.LEFT);
252             heightTextField = new JTextField();
253             JLabel widthLabel = new JLabel(Res.string().getShowInfo("WIDTH"), JLabel.LEFT);
254             widthTextField = new JTextField();
255             JLabel channelLabel = new JLabel(Res.string().getShowInfo("CHANNEL"), JLabel.LEFT);
256             channelTextField = new JTextField();
257             JLabel timeLabel = new JLabel(Res.string().getShowInfo("TIME"), JLabel.LEFT);
258             timeTextField = new JTextField();
259             JLabel lengthLabel = new JLabel(Res.string().getShowInfo("LENGTH"), JLabel.LEFT);
260             lengthTextField = new JTextField();
261             JLabel sensorTypeLabel = new JLabel(Res.string().getShowInfo("SENSOR_TYPE"), JLabel.LEFT);
262             sensorTypeTextField = new JTextField();
263             
264             Dimension lableDimension = new Dimension(100, 20);
265             Dimension textFieldDimension = new Dimension(140, 20);
266             heightLabel.setPreferredSize(lableDimension);
267             widthLabel.setPreferredSize(lableDimension);
268             channelLabel.setPreferredSize(lableDimension);
269             timeLabel.setPreferredSize(lableDimension);
270             lengthLabel.setPreferredSize(lableDimension);
271             sensorTypeLabel.setPreferredSize(lableDimension);
272             heightTextField.setPreferredSize(textFieldDimension);
273             widthTextField.setPreferredSize(textFieldDimension);
274             channelTextField.setPreferredSize(textFieldDimension);
275             timeTextField.setPreferredSize(textFieldDimension);
276             lengthTextField.setPreferredSize(textFieldDimension);
277             sensorTypeTextField.setPreferredSize(textFieldDimension);
278             
279             heightTextField.setEditable(false);
280             widthTextField.setEditable(false);
281             channelTextField.setEditable(false);
282             timeTextField.setEditable(false);
283             lengthTextField.setEditable(false);
284             sensorTypeTextField.setEditable(false);
285
286             add(heightLabel);
287             add(heightTextField);
288             add(widthLabel);
289             add(widthTextField);
290             add(channelLabel);
291             add(channelTextField);
292             add(timeLabel);
293             add(timeTextField);
294             add(lengthLabel);
295             add(lengthTextField);
296             add(sensorTypeLabel);
297             add(sensorTypeTextField);
298         }
299         
300         public void updateData() {
301             String[] data = new String[6];
302
303             lock.lock();
304             data[0] = String.valueOf(gData.stMetaData.nHeight);
305             data[1] = String.valueOf(gData.stMetaData.nWidth);
306             data[2] = String.valueOf(gData.stMetaData.nChannel+1);
307             data[3] = gData.stMetaData.stTime.toStringTimeEx();
308             data[4] = String.valueOf(gData.stMetaData.nLength);
309             try {
310                 data[5] = new String(gData.stMetaData.szSensorType, "GBK").trim();
311             } catch (UnsupportedEncodingException e) {
312                 data[5] = new String(gData.stMetaData.szSensorType).trim();
313             }
314             lock.unlock();
315             
316             setData(data);
317         }
318         
319         public void clearData() {
320             setData(new String[6]);
321         }
322         
323         private void setData(String[] data) {
324             
325             if (data.length != 6) {
326                 System.err.printf("data length  %d != 6", data.length);
327                 return;
328             }
329
330             heightTextField.setText(data[0]);
331             widthTextField.setText(data[1]);
332             channelTextField.setText(data[2]);
333             timeTextField.setText(data[3]);
334             lengthTextField.setText(data[4]);
335             sensorTypeTextField.setText(data[5]);
336         }
337
338         private JTextField heightTextField;
339         private JTextField widthTextField;
340         private JTextField channelTextField;
341         private JTextField timeTextField;
342         private JTextField lengthTextField;
343         private JTextField sensorTypeTextField;
344     }
345     
346     
347     private OperatePanel operatePanel;
348     private HeatMapShowPanel showPanel;
349 }