forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCamera.java
More file actions
executable file
·160 lines (143 loc) · 5.2 KB
/
Copy pathTestCamera.java
File metadata and controls
executable file
·160 lines (143 loc) · 5.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package javaforce.utils;
/** Tests Camera function (to be deleted)
*
* @author pquiring
*
* Created : Jun 9, 2014
*/
import java.util.*;
import javaforce.*;
import javaforce.media.*;
public class TestCamera extends javax.swing.JFrame {
/**
* Creates new form Test2
*/
public TestCamera() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
preview = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Camera Test");
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
camera = new Camera();
if (camera == null) {
JFLog.log("cam == null");
return;
}
if (!camera.init()) {
JFLog.log("init failed");
return;
}
String devices[] = camera.listDevices();
if (devices == null || devices.length == 0) {
JFLog.log("no devices found");
return;
}
JFLog.log("device count=" + devices.length);
for (int a = 0; a < devices.length; a++) {
JFLog.log("device=" + devices[a]);
}
camera.start(0, 320, 200);
width = camera.getWidth();
height = camera.getHeight();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
java.awt.EventQueue.invokeLater(new Runnable() {public void run() {
int px[] = camera.getFrame();
if (px == null) return; //not ready
if (img == null) {
img = new JFImage(width, height);
}
System.arraycopy(px, 0, img.getBuffer(), 0, width * height);
/* cnt++;
if (cnt > 5) {
img.savePNG("test" + cnt + ".png");
}*/
preview.setIcon(img);
preview.repaint();
}});
}
}, 500, 500);
}//GEN-LAST:event_jButton1ActionPerformed
int cnt = 0;
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
if (camera == null) {
return;
}
timer.cancel();
timer = null;
camera.stop();
camera = null;
}//GEN-LAST:event_jButton2ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestCamera().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel preview;
// End of variables declaration//GEN-END:variables
private Camera camera;
private Timer timer;
private int frame;
private JFImage img;
private int width, height;
}