我终于让JavaCV使用我的网络摄像头,但我找不到改变图像抓取输出格式的方法。
我的HP笔记本电脑配有HP HD(720p)固定网络摄像头。我得到的唯一解决方案是640x480。我尝试使用FrameGrabber的所有可用子类。
我正在使用这个简单的代码:
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class Webcam {
public static void main(String[] args) throws Exception {
FrameGrabber grabber = FrameGrabber.createDefault(0);
grabber.start();
IplImage grabbedImage = grabber.grab();
CanvasFrame frame = new CanvasFrame("Some Title", CanvasFrame.getDefaultGamma() / grabber.getGamma());
while (frame.isVisible() && (grabbedImage = grabber.grab()) != null) {
frame.showImage(grabbedImage);
}
frame.dispose();
grabber.stop();
}
}
我是否遗漏了某些内容,或者是否有其他方法可以使用JavaCV从网络摄像头捕获图像?
答案 0 :(得分:4)
我找到了另一种从网络摄像头捕获的方法,还设置了一些属性,如首选宽度和高度。
此代码适用于我:
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_highgui;
import com.googlecode.javacv.cpp.opencv_highgui.CvCapture;
public class Webcam {
public static void main(String[] args) throws Exception {
CvCapture capture = opencv_highgui.cvCreateCameraCapture(0);
opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);
IplImage grabbedImage = opencv_highgui.cvQueryFrame(capture);
CanvasFrame frame = new CanvasFrame("Webcam");
while (frame.isVisible() && (grabbedImage = opencv_highgui.cvQueryFrame(capture)) != null) {
frame.showImage(grabbedImage);
}
frame.dispose();
opencv_highgui.cvReleaseCapture(capture);
}
}
希望这可以帮助有同样问题的人。