在调试器中运行它,我看到 info.facing == 0。这意味着它是一个后置摄像头。
当我尝试实例化 Camrea 对象时,我得到了 null。
在模拟器上,我将设备设置为禁用后置摄像头并启用前置摄像头。为什么设备认为有后置摄像头,而实际上没有?
我正在使用 Eclipse ADT。
这是我的方法。我从来没有到达第二个循环。 getCamreaInstance 返回的 c 为 null。
public static Camera getCameraInstance(){
Camera c = null;
CameraInfo info = new CameraInfo();
if (info.facing == CameraInfo.CAMERA_FACING_BACK){
//Calling Camera.open() throws an exception if the camera is already in use by another application, so we wrap it in a try block.
//Failing to check for exceptions if the camera is in use or does not exist will cause your application to be shut down by the system.
try {
c = Camera.open();
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c;
}
//we want the back facing, if we cant get that then we try and get the front facing
else if (info.facing == CameraInfo.CAMERA_FACING_FRONT){
c = Camera.open(Camera.getNumberOfCameras()-1); //i should test and see if -1 is a valid value in the case that a device has no camera
return c;
}
else{
//there are no cameras, so we need to account for that since 'c' will be null
return c;
}
}
最佳答案
这一行:
CameraInfo info = new CameraInfo();
不获取当前相机配置。它只是一个空的默认构造函数。获得准确的 CameraInfo 对象的唯一方法是 Camera#getCameraInfo() .
你得到一个 null Camera 的原因是因为默认的 facing
是 0。所以,它进入第一个 block ,尝试 open()
返回 null因为:
If the device does not have a back-facing camera, this returns null.
您可以从一开始就调用 getNumberOfCameras()
来查看有多少个摄像头。然后打开一个并检查它的 CameraInfo
以查看它面向哪个方向。
但是,如果您始终希望默认使用后置摄像头(根据您的代码,这似乎很可能),只需删除对 facing
的检查并检查 open() 是否为 null
。
关于android - Camera Emulator "facing"行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19140387/