2012-12-03 132 views
2

我想不斷捕捉攝像頭並讓它在畫布上進行查看。保存圖像很好。它每秒鐘拍攝一張照片。但是canvas.showImage()不起作用。我的屏幕保持空白。控制檯說每個框架都有「清理相機」。JavaCV畫布保留空白

頭等艙:

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

回答

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

}

+0

在上面的代碼變量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); 
} 
+0

這可行,但你必須修改源...應該將此修復提交給該項目。 – rednoyz