What is the best method to capture images from a live video device for use by a Java-based application?

前端 未结 6 1174
南笙
南笙 2020-11-28 21:56

I am looking into an image processing problem for semi-real time detection of certain scenarios. My goal is to have the live video arrive as Motion JPEG frames in my Java c

6条回答
  •  北海茫月
    2020-11-28 22:01

    This is my JavaCV implementation with high resolution video output and no noticeable drop in the frame-rate than other solutions (only when my webcam refocuses do I notice a slight drop, only for a moment though).

    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.swing.JFrame;
    
    import com.googlecode.javacv.CanvasFrame;
    import com.googlecode.javacv.OpenCVFrameGrabber;
    import com.googlecode.javacv.OpenCVFrameRecorder;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    
    public class Webcam implements Runnable {
    
        IplImage image;
        static CanvasFrame frame = new CanvasFrame("Web Cam");
        public static boolean running = false;
    
        public Webcam()
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        @Override
        public void run()
        {
            try
            {
                grabber.setImageWidth(800);
                grabber.setImageHeight(600);
                grabber.start();
                while (running)
                {
                    IplImage cvimg = grabber.grab();
                    BufferedImage image;
                    if (cvimg != null)
                    {
                        // opencv_core.cvFlip(cvimg, cvimg, 1); // mirror
                        // show image on window
                        image = cvimg.getBufferedImage();
                        frame.showImage(image);
                    }
                }
                grabber.stop();
                frame.dispose();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public static void main(String... args)
        {
            Webcam webcam = new Webcam();
            webcam.start();
        }
    
        public void start()
        {
            new Thread(this).start();
            running = true;
        }
    
        public void stop()
        {
            running = false;
        }
    }
    

提交回复
热议问题