2

我想不断地捕捉网络摄像头并让它在画布上查看。图像的保存很好。它每秒拍一张照片。但是 canvas.showImage() 不起作用。我的屏幕仍然空白。控制台每帧都会显示“已清理相机”。

头等舱:

public class StartUp {
public static void main(String[] args) {
    CanvasFrame canvas = new CanvasFrame("Cam");
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    CaptureImage captureimage = new CaptureImage();
    while(true){
        try {
            IplImage img = captureimage.captureFrame();
            canvas.showImage(img);
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
}

二等:

public class CaptureImage {

public IplImage captureFrame() {
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    try {
        grabber.start();
        IplImage img = grabber.grab();
        if (img != null) {
            cvSaveImage("Image.jpg",img);     
            return img;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}
4

2 回答 2

2

尝试使用此代码捕获网络摄像头。在运行此代码之前加载库。(JavaCV jar 文件等)

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.OpenCVFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.*;

public class capture{

CanvasFrame frame,fr;
IplImage image,im;
OpenCVFrameGrabber grabber,gr;
public capture() throws Exception {
    frame=new CanvasFrame("Cam 1");
    grabber=new  OpenCVFrameGrabber(0);
     grabber.start();
     while (((frame.isVisible()) && (image = grabber.grab()) != null)){
         //cvFlip(image,image,1); if needed to flip.

         frame.showImage(image);


     }
     frame.setDefaultCloseOperation(1);
     frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
}

}

于 2012-12-16T18:22:52.770 回答
2

我自己也有同样的问题。最后用以下代码重写了 initCanvas 方法:

protected void initCanvas(boolean fullScreen, DisplayMode displayMode, double gamma) {
    canvas = new Canvas() {
        /**
         * 
         */
        private static final long   serialVersionUID    = 1L;
        @Override public void update(Graphics g) {
            paint(g);
        }
        @Override public void paint(Graphics g) {
            // Calling BufferStrategy.show() here sometimes throws
            // NullPointerException or IllegalStateException,
            // but otherwise seems to work fine.
            try {
                BufferStrategy strategy = canvas.getBufferStrategy();
                if (strategy != null) {
                    do {
                        do {
                            g = strategy.getDrawGraphics();
                            if (color != null) {
                                g.setColor(color);
                                g.fillRect(0, 0, getWidth(), getHeight());
                            }
                            if (image != null) {
                                g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
                            }
                            if (buffer != null) {
                                g.drawImage(buffer, 0, 0, getWidth(), getHeight(), null);
                            }
                            g.dispose();
                        } while (strategy.contentsRestored());
                        strategy.show();
                    } while (strategy.contentsLost());
                }
                else {
                    g = canvas.getGraphics();
                    if (color != null) {
                        g.setColor(color);
                        g.fillRect(0, 0, getWidth(), getHeight());
                    }
                    if (image != null) {
                        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
                    }
                    if (buffer != null) {
                        g.drawImage(buffer, 0, 0, getWidth(), getHeight(), null);
                    }
                    g.dispose();
                }
            } catch (NullPointerException e) {
            } catch (IllegalStateException e) { 
            }
        }
    };
    if (fullScreen) {
        canvas.setSize(getSize());
        needInitialResize = false;
    } else {
        needInitialResize = true;
    }
    getContentPane().add(canvas);
    canvas.setVisible(true);
    canvas.createBufferStrategy(2);
    //canvas.setIgnoreRepaint(true);
}
于 2013-11-13T06:23:14.140 回答