java - 从网络摄像头保存图像

标签 java image webcam jmf

语言:Java
另外使用:JMF

下面是代码(没有 Swing 表单),它允许使用网络摄像头捕获图像:

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);
    }

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

展望 future ,我们扩展了方法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.
        }

    }

唯一的是,网络摄像头没有保存任何图片,并且右侧有垂直条纹的白色背景。

最佳答案

image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer);
ImageIO.write(image, "png", new File("screengrab.png"));

参见ImageIO.write(RenderedImage,String,File)了解详情。

关于java - 从网络摄像头保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10546063/

相关文章:

android - Android 下 unity3d 中的 webcamTexture 旋转不同

java - 用于排除 REQUIRES_NEW 方法上的死锁的数据源配置

java - Android: java.lang.RuntimeException: takePicture 失败

java - 在JSP页面上使用Bootstrap模式

WPF 图像工具提示

html - 图像不工作 - 这是一个 Chrome 错误吗?

flash - 通过 ActionScript 关闭网络摄像头使用

javascript - 将网络摄像头变成拉伸(stretch)/缩放背景图像

java - admob 不工作是因为(我认为)google play 服务

HTML - 扩展名错误的图像