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