Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从网络摄像机保存图像_Java_Image_Webcam_Jmf - Fatal编程技术网

Java 从网络摄像机保存图像

Java 从网络摄像机保存图像,java,image,webcam,jmf,Java,Image,Webcam,Jmf,语言:Java 它还用于:JMF 下面是代码(不带Swing表单),它允许使用WEB摄像头捕捉图像: MediaLocator getWebCam = new MediaLocator("vfw://0"); private Player player; Timer timer = new Timer(40, this); public BufferedImage grabFrameImage() { Image image = null;

语言:Java
它还用于:JMF

下面是代码(不带Swing表单),它允许使用WEB摄像头捕捉图像:

MediaLocator getWebCam = new MediaLocator("vfw://0");
    private Player player;
    Timer timer = new Timer(40, this);

    public BufferedImage grabFrameImage() {

        Image image = null;
        FrameGrabbingControl frameGrabbingControl = null;

        if (player != null)
            frameGrabbingControl = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
        Buffer buffer = frameGrabbingControl.grabFrame();
        if (buffer != null)
            image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer);
        if (image != null)
            return (BufferedImage) image;

        return null;
    }

    public WorkWithWebCam() throws NoDataSourceException, IOException, NoPlayerException {

            initComponents();
            player = Manager.createPlayer(Manager.createDataSource(getWebCam));
            player.start();

    }
private void jButton1ActionPerformed(ActionEvent e) {
        timer.start();
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    new WorkWithWebCam().setVisible(true);
                }catch(Exception ex){}
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        panelMain.getGraphics().drawImage(this.grabFrameImage(), 0, 0, 400, 300, null);
    }
你能告诉我如何用网络摄像机保存图像吗

展望未来,我们扩展了执行的方法:

public void actionPerformed(ActionEvent e) {
        panelMain.getGraphics().drawImage(this.grabFrameImage(), 0, 0, 400, 300, null);
        BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        String fileOut = "temp.jpg";
        Graphics g = image.getGraphics();
        panelMain.paint(g);
        try {
            ImageIO.write(image, "jpg", new FileOutputStream(fileOut));
        } catch (IOException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
唯一的一点是,网络摄像头不会保存任何图片,而白色背景的右侧会有一条垂直条纹

image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer);
ImageIO.write(image, "png", new File("screengrab.png"));
有关详细信息,请参阅