Capturing image from webcam in java?

How can I continuously capture images from a webcam?

I want to experiment with object recognition (by maybe using java media framework).

I was thinking of creating two threads

one thread:

  • Node 1: capture live image
  • Node 2: save image as "1.jpg"
  • Node 3: wait 5 seconds
  • Node 4: repeat...
  • other thread:

  • 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...

  • This JavaCV implementation works fine.

    Code:

    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();
        }
    }
    

    There is also post on configuration for JavaCV

    You can modify the codes and be able to save the images in regular interval and do rest of the processing you want.


    Some time ago I've created generic Java library which can be used to take pictures with a PC webcam. The API is very simple, not overfeatured, can work standalone, but also supports additional webcam drivers like OpenIMAJ, JMF, FMJ, LTI-CIVIL, etc, and some IP cameras.

    Link to the project is https://github.com/sarxos/webcam-capture

    Example code (take picture and save in test.jpg):

    Webcam webcam = Webcam.getDefault();
    webcam.open();
    BufferedImage image = webcam.getImage();
    ImageIO.write(image, "JPG", new File("test.jpg"));
    

    It is also available in Maven Central Repository or as a separate ZIP which includes all required dependencies and 3rd party JARs.


    JMyron is very simple for use. http://webcamxtra.sourceforge.net/

    myron = new JMyron();
    myron.start(imgw, imgh);
    myron.update();
    int[] img = myron.image();
    
    链接地址: http://www.djcxy.com/p/81010.html

    上一篇: 使用java检测WebCam设备

    下一篇: 从java摄像头捕获图像?