android - Camera Emulator "facing"行为异常

标签 android eclipse android-emulator android-camera

在调试器中运行它,我看到 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/

相关文章:

android - 单击按钮时插入sqlite

android - 用于第 3 方/内置应用程序的 iPhone 自动化

eclipse - BPEL Apache ODE .部署文件

java - 当文件不是项目的一部分时在 Eclipse 中突出显示变量

android - 膨胀类 android.support.design.widget.TabLayout 时出错

模拟器的 android 8.0 api 26 arm64-v8a 系统镜像

android - Android studio 模拟器上有任何按钮可以打开打瞌睡模式吗?

android - WebView JavaScript 桥文档

java - android onQuerySubmit() 方法搜索小部件给出错误

android - 默认 Android Gallery 应用程序不编译 :(