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

前端 未结 6 1062
南笙
南笙 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 22:07

    Have you ever looked at Processing.org? It's basically a simplified application framework for developing "artsy" applications and physical computing platforms, but it's based on Java and you can dig down to the "real" Java underneath.

    The reason it came to mind is that there are several video libraries for Processing which are basically Java components (at least I think they are - the site has all the technical information you might need). There is a tutorial on using the Processing libraries and tools in the Eclipse IDE. There are also numerous examples on video capture and processing.

    Even if you can't use the libraries directly, Processing is a great language/environment for working out algorithms. There are several great examples of image and video capture and real-time processing there.

    0 讨论(0)
  • 2020-11-28 22:11

    This JavaCV implementation works fine.

    CODE:

    import com.googlecode.javacv.OpenCVFrameGrabber;
    
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    import static com.googlecode.javacv.cpp.opencv_highgui.*;
    
    public class CaptureImage {
        private static void captureFrame() {
            // 0-default camera, 1 - next...so on
            final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
            try {
                grabber.start();
                IplImage img = grabber.grab();
                if (img != null) {
                    cvSaveImage("capture.jpg", img);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            captureFrame();
        }
    }
    

    There is also post on viewing live video from Camera .And configuration for JavaCV :

    I think this will meet your requirements.

    0 讨论(0)
  • 2020-11-28 22:12

    FMJ can definitely capture video and turn it into MJPEG frames.

    0 讨论(0)
  • 2020-11-28 22:15

    Regarding the dead-ness of JMF, are you aware of the FMJ implementation? I don't know whether it qualifies as the "best" solution, but it's probably worth adding to the discussion.

    0 讨论(0)
  • 2020-11-28 22:15

    Below is shown a very simple implementation using Marvin Framework. Using Marvin you can add real time video processing easily.

    import javax.swing.JFrame;
    import marvin.gui.MarvinImagePanel;
    import marvin.image.MarvinImage;
    import marvin.video.MarvinJavaCVAdapter;
    import marvin.video.MarvinVideoInterface;
    
    public class SimpleVideoTest extends JFrame implements Runnable{
    
        private MarvinVideoInterface    videoAdapter;
        private MarvinImage             image;
        private MarvinImagePanel        videoPanel;
    
        public SimpleVideoTest(){
            super("Simple Video Test");
    
            // Create the VideoAdapter and connect to the camera
            videoAdapter = new MarvinJavaCVAdapter();
            videoAdapter.connect(0);
    
            // Create VideoPanel
            videoPanel = new MarvinImagePanel();
            add(videoPanel);
    
            // Start the thread for requesting the video frames 
            new Thread(this).start();
    
            setSize(800,600);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SimpleVideoTest t = new SimpleVideoTest();
            t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        @Override
        public void run() {
            while(true){
                // Request a video frame and set into the VideoPanel
                image = videoAdapter.getFrame();
                videoPanel.setImage(image);
            }
        }
    }
    

    Another example applying multiple algorithms for real time video processing.

    0 讨论(0)
提交回复
热议问题