import hypermedia.video.OpenCV;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
public class Sample extends Frame implements Runnable {
final int FRAME_RATE = 1000 / 30;
private OpenCV cv;
private Thread t;
private Image frame = null;
public Sample() {
cv = new OpenCV();
cv.capture(640, 480);
setBounds(100, 100, cv.width, cv.height);
setVisible(true);
addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
stop();
System.exit(0);
}
}
});
t = new Thread(this);
t.start();
}
@Override
public void run() {
while (t != null && cv != null) {
try {
Thread.sleep(FRAME_RATE);
cv.read();
MemoryImageSource mis = new MemoryImageSource(cv.width,
cv.height, cv.pixels(), 0, cv.width);
frame = createImage(mis);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
if (frame == null)
return;
g.drawImage(frame, 0, 0, null);
}
public void stop() {
t = null;
cv.dispose();
}
public static void main(String[] args) {
new Sample();
}
}
0 件のコメント:
コメントを投稿