【问题标题】:Saving images from the web-camera从网络摄像头保存图像
【发布时间】:2012-05-11 05:48:15
【问题描述】:

语言:Java
另外使用:JMF

下面是代码(没有 Swing-forms),它允许使用 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);
    }

您能告诉我如何使用网络摄像头保存图像吗?

展望未来,我们扩展了 actionPerformed 方法:

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

    }

唯一的一点,网络摄像头没有保存图片,而且是白色背景,右边有竖条。

【问题讨论】:

    标签: java image webcam jmf


    【解决方案1】:
    image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer);
    ImageIO.write(image, "png", new File("screengrab.png"));
    

    详情请见ImageIO.write(RenderedImage,String,File)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2014-07-18
      • 2013-02-09
      相关资源
      最近更新 更多