第一:目标是什么? 我的Java应用程序应该显示摄像机图片的小预览,并且当用户单击按钮时,应该可以拍摄全分辨率的图片。
我在Windows 8.1上使用webcam-capture-0.3.12.jar 以及具有2个值的分辨率数组[2]
Resolutions[INDEX_PREVIEW_RESOLUTION]: WebcamResolution.QVGA.getSize()
Resolutions[INDEX_CAMERA_RESOLUTION]: custom resolution 3264x2448
当我通过getFullResolutionImage()方法进入Eclipse时,它可以工作。 但是当我全速运行相同的代码时,来自摄像机的图像为NULL。
我试图添加wait(5000ms)以便进行上下文切换并让其他线程呼吸。 没有成功。
一个细节使我花了很多时间才意识到: 关闭()/打开()相机并设置所需的分辨率
Camera.setViewSize(Resolutions [INDEX_PREVIEW_RESOLUTION]);
还不够,我还必须重复声明
Camera.setCustomViewSizes(Resolutions);
每次我重新打开相机时-这是意外的-为什么?
public BufferedImage getFullResolutionImage() {
long stamp;
BufferedImage image;
System.out.println("Camera is open " + Camera.isOpen());
if (Camera.isOpen() == true) {
System.out.println("close camera before switching from preview to full resolution");
Camera.close();
}
Camera.setCustomViewSizes(Resolutions);
Camera.setViewSize(Resolutions[INDEX_CAMERA_RESOLUTION]);
stamp = System.currentTimeMillis();
Camera.open(false); // synchronous
System.out.println("Camera.open() took " + (System.currentTimeMillis() - stamp) + "ms");
stamp = System.currentTimeMillis();
image = Camera.getImage();
System.out.println("taking a picture took " + (System.currentTimeMillis() - stamp) + "ms");
if (image != null)
System.out.println("image has width/height " + image.getWidth() + " " + image.getHeight());
else
System.out.println("IMAGE == NULL");
Camera.close();
System.out.println("closed camera to switch back to preview resolution");
System.out.println("now switch back to preview resolution");
stamp = System.currentTimeMillis();
try {
Camera.setCustomViewSizes(Resolutions);
Camera.setViewSize(Resolutions[INDEX_PREVIEW_RESOLUTION]);
} catch (Exception ex) {
System.out.println("Exception while switching back after " + (System.currentTimeMillis() - stamp) + "ms");
}
stamp = System.currentTimeMillis();
Camera.open(true); // asynchronous
System.out.println("Camera.open() took " + (System.currentTimeMillis() - stamp) + "ms");
System.out.println("image has width/height " + image.getWidth() + " " + image.getHeight());
return image;
}
逐步执行此代码可获得以下结果:
Camera is open true
close camera before switching from preview to full resolution
Camera.open() took 10190ms
taking a picture took 1218ms
image has width/height 3264 2448
closed camera to switch back to preview resolution
now switch back to preview resolution
Camera.open() took 3584ms
image has width/height 3264 2448
在没有调试器的情况下运行相同的代码会给我
Camera is open true
close camera before switching from preview to full resolution
Camera.open() took 9171ms
taking a picture took 0ms
IMAGE == NULL
closed camera to switch back to preview resolution
now switch back to preview resolution
Camera.open() took 1489ms
我不明白-为什么使用/不使用调试器之间的区别? 没有调试器:open()是如此之慢=:-O MS Windows相机应用程序即使在高分辨率下也能显示流畅的图像流。
我没主意-希望得到任何提示