如何从网络摄像头连续捕获图像?
我想尝试对象识别(可能使用java媒体框架).
我在考虑创建两个线程
一个帖子:
节点1:捕获实时图像
节点2:将图像保存为"1.jpg"
节点3:等待5秒
节点4:重复......
其他线程:
节点1:等到捕获图像
节点2:使用"1.jpg"获取每个像素的颜色
节点3:将数据保存在数组中
节点4:重复......
gtiwari333.. 46
这个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的配置文章
您可以修改代码并能够定期保存图像并执行所需的其余处理.
这个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的配置文章
您可以修改代码并能够定期保存图像并执行所需的其余处理.
前段时间我创建了通用的Java库,可用于通过PC网络摄像头拍照.API非常简单,不会过度使用,可以独立工作,但也支持其他网络摄像头驱动程序,如OpenIMAJ,JMF,FMJ,LTI-CIVIL等,以及一些IP摄像头.
该项目的链接是https://github.com/sarxos/webcam-capture
示例代码(拍照并保存在test.jpg中):
Webcam webcam = Webcam.getDefault(); webcam.open(); BufferedImage image = webcam.getImage(); ImageIO.write(image, "JPG", new File("test.jpg"));
它也可以在Maven Central Repository中使用,也可以作为单独的ZIP包含所有必需的依赖项和第三方JAR.
JMyron非常简单易用. http://webcamxtra.sourceforge.net/
myron = new JMyron(); myron.start(imgw, imgh); myron.update(); int[] img = myron.image();
这是一个类似的问题,有些 - 但尚未接受 - 答案.其中一人提到FMJ是JMF的Java替代品.
这种使用JavaCV取决于gt_ebuddy的答案,但我的视频输出质量远高于他的答案.我还添加了一些其他的随机改进(如倒闭程序时ESC和CTRL+C被按下,并确保关闭该程序使用正确的资源).
import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.KeyStroke; import com.googlecode.javacv.CanvasFrame; import com.googlecode.javacv.OpenCVFrameGrabber; import com.googlecode.javacv.cpp.opencv_core.IplImage; public class HighRes extends JComponent implements Runnable { private static final long serialVersionUID = 1L; private static CanvasFrame frame = new CanvasFrame("Web Cam"); private static boolean running = false; private static int frameWidth = 800; private static int frameHeight = 600; private static OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); private static BufferedImage bufImg; public HighRes() { // setup key bindings ActionMap actionMap = frame.getRootPane().getActionMap(); InputMap inputMap = frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); for (Keys direction : Keys.values()) { actionMap.put(direction.getText(), new KeyBinding(direction.getText())); inputMap.put(direction.getKeyStroke(), direction.getText()); } frame.getRootPane().setActionMap(actionMap); frame.getRootPane().setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); // setup window listener for close action frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { stop(); } }); } public static void main(String... args) { HighRes webcam = new HighRes(); webcam.start(); } @Override public void run() { try { grabber.setImageWidth(frameWidth); grabber.setImageHeight(frameHeight); grabber.start(); while (running) { final IplImage cvimg = grabber.grab(); if (cvimg != null) { // cvFlip(cvimg, cvimg, 1); // mirror // show image on window bufImg = cvimg.getBufferedImage(); frame.showImage(bufImg); } } grabber.stop(); grabber.release(); frame.dispose(); } catch (Exception e) { e.printStackTrace(); } } public void start() { new Thread(this).start(); running = true; } public void stop() { running = false; } private class KeyBinding extends AbstractAction { private static final long serialVersionUID = 1L; public KeyBinding(String text) { super(text); putValue(ACTION_COMMAND_KEY, text); } @Override public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); if (action.equals(Keys.ESCAPE.toString()) || action.equals(Keys.CTRLC.toString())) stop(); else System.out.println("Key Binding: " + action); } } } enum Keys { ESCAPE("Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)), CTRLC("Control-C", KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK)), UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)); private String text; private KeyStroke keyStroke; Keys(String text, KeyStroke keyStroke) { this.text = text; this.keyStroke = keyStroke; } public String getText() { return text; } public KeyStroke getKeyStroke() { return keyStroke; } @Override public String toString() { return text; } }