【问题标题】:JavaCV canvas remains blankJavaCV 画布保持空白
【发布时间】:2012-12-03 16:13:23
【问题描述】:

我想不断捕捉网络摄像头并让它在画布上查看。图像的保存很好。它每秒拍一张照片。但是 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;
}
}

【问题讨论】:

    标签: java webcam javacv


    【解决方案1】:

    尝试使用此代码捕获网络摄像头。 在运行此代码之前加载库。 (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);
    }
    

    }

    【讨论】:

    • 在上面的代码中,fr,im,gr 变量没有被使用,也不需要。
    【解决方案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);
    }
    

    【讨论】:

    • 这可行,但您必须修改源...应该将此修复提交给项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多