简体   繁体   中英

How to capture image from webcam WHILST already streaming from webcam too with Java Gstreamer?

I am using the gstreamer library for a Java project that needs to be able to capture an image from a webcam.

I already have the code that displays the webcam stream, I just can't figure out how to capture an image at the press of a button next to it.

I have searched the internet, but could only find pieces of code that show either the stream, or capture the image, but none illustrated both... I've tried to merge those pieces of code, but that didn't work for me either.

What do I have to do to get this to work?

public class WebcamPanel extends JPanel {

    private static Pipeline pipe; 

    public WebcamPanel(){

        String[] args = {};
        args = Gst.init("Webcam", args); 

        pipe = new Pipeline("pipeline");

        final Element videosrc = ElementFactory.make("dshowvideosrc", "source"); 
        final Element videofilter = ElementFactory.make("capsfilter", "flt"); 
        videofilter.setCaps(Caps.fromString("video/x-raw-yuv, width=320, height=240")); 

        setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();

        JButton takePic = new JButton();
        takePic.setPreferredSize(new Dimension(50,50));

        c.gridx = 0;
        c.insets = new Insets(0,10,0,0);
        add(takePic,c);

        c.gridx = 2;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(0,40,0,0);

        SwingUtilities.invokeLater(new Runnable() { 
            public void run() { 
                VideoComponent videoComponent = new VideoComponent(); 
                Element videosink = videoComponent.getElement(); 
                // This gives 2nd window with stream from webcam 
                // Element videosink = ElementFactory.make("xvimagesink", "sink"); 
                pipe.addMany(videosrc, videofilter, videosink); 
                Element.linkMany(videosrc, videofilter, videosink); 

                videoComponent.setPreferredSize(new Dimension(320, 240)); 
                add(videoComponent,c);
                videoComponent.setVisible(true);
                // Start the pipeline processing 
                pipe.setState(State.PLAYING); 
            }
        });

    }
}

Have you take a look at camerabin2? This will implement the whole camera workflow for you (viewfinder, image capture, video captue, effects, ...). The basic approach is to either tee off a 2nd stream and capture selected images from it (eg use a valve ! jpegenc ! multifilesink and open the valve for selected images) or to use a output-selector to the image-saving pipe or to use a buffer-probe (hacky).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM