在调试器中运行它,我看到 info.facing == 0。这意味着它是一个后置摄像头。
当我尝试实例化 Camrea 对象时,我得到空值。
在模拟器上,我将设备设置为禁用后置摄像头并启用前置摄像头。为什么设备认为没有背面摄像头?
我正在使用 Eclipse ADT。
这是我的方法。我从来没有到达第二个循环。getCamreaInstance 正在返回为空的 c。
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;
}
}