在java中捕获来自网络摄像头的图像? [英] Capturing image from webcam in java?
本文介绍了在java中捕获来自网络摄像头的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从网络摄像头连续捕获图像?
How can I continuously capture images from a webcam?
我想尝试对象识别(可能使用java媒体框架)。
I want to experiment with object recognition (by maybe using java media framework).
我想创建两个线程
一个线程:
- 节点1:捕获实时图像
- 节点2:将图像保存为1.jpg
- 节点3:等待5秒
- 节点4:重复...
- Node 1: capture live image
- Node 2: save image as "1.jpg"
- Node 3: wait 5 seconds
- Node 4: repeat...
其他线程:
- 节点1:等到拍摄图像
- 节点2:使用1。 jpg从每个pixle获取颜色
- 节点3:在数组中保存数据
- 节点4:重复...
- Node 1: wait until image is captured
- Node 2: using the "1.jpg" get colors from every pixle
- Node 3: save data in arrays
- Node 4: repeat...
推荐答案
这个JavaCV实现工作正常。
This JavaCV implementation works fine.
代码:
import org.bytedeco.javacv.*;
import static org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_core.cvFlip;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
/**
* Created by gtiwari on 1/3/2017.
*/
public class Test implements Runnable {
final int INTERVAL = 100;///you may use interval
CanvasFrame canvas = new CanvasFrame("Web Cam");
public Test() {
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public void run() {
FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next camera
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
IplImage img;
int i = 0;
try {
grabber.start();
while (true) {
Frame frame = grabber.grab();
img = converter.convert(frame);
//the grabbed frame will be flipped, re-flip to make it right
cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
//save
cvSaveImage((i++) + "-aa.jpg", img);
canvas.showImage(converter.convert(img));
Thread.sleep(INTERVAL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test gs = new Test();
Thread th = new Thread(gs);
th.start();
}
}
您可以修改代码,并能够定期保存图像并完成所需的其余处理。
You can modify the codes and be able to save the images in regular interval and do rest of the processing you want.
这篇关于在java中捕获来自网络摄像头的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文