如何连续从摄像头捕获图像?
我想尝试对象识别(也许使用Java Media Framework)。
我当时正在考虑创建两个线程
一个线程:
其他线程:
此JavaCV实现效果很好。
码:
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(); } }
还有关于JavaCV的配置的文章
你可以修改代码,并能够定期保存图像并进行所需的其他处理。