【问题标题】:Camera Emulator "facing" acting strange相机模拟器“面对”行为怪异
【发布时间】:2013-10-02 15:13:46
【问题描述】:

在调试器中运行它,我看到 info.facing == 0。这意味着它是一个后置摄像头。

当我尝试实例化 Camrea 对象时,我得到了 null。

在模拟器上,我将设备设置为禁用后置摄像头并启用前置摄像头。为什么没有后置摄像头,设备却认为有后置摄像头?

我正在使用 Eclipse ADT。

这是我的方法。我从来没有到达第二个循环。 getCamreaInstance 正在返回为 null 的 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;
        }

    }

【问题讨论】:

    标签: android eclipse android-emulator android-camera


    【解决方案1】:

    这一行:

    CameraInfo info = new CameraInfo();
    

    获取当前的相机配置。它只是一个空的默认构造函数。获取准确的 CameraInfo 对象的唯一方法是Camera#getCameraInfo()

    你得到一个空相机的原因是因为默认的facing是0。所以,它进入第一个块,尝试返回空的open(),因为:

    如果设备没有后置摄像头,则返回 null。

    您可以从一开始就拨打getNumberOfCameras() 来查看有多少个摄像头。然后打开一个并检查它是CameraInfo,看看它面向哪个方向。

    但是,如果您始终希望默认使用后置摄像头(根据您的代码,这似乎很可能),只需删除对 facing 的检查并检查 open() 上的 null。

    【讨论】:

    • 请原谅我的无知。 java中的#有什么作用? getCameraInfo 也是具有空参数的有效方法吗?它在 api 文档中看起来像它,但在我的 ide 中却没有……developer.android.com/reference/android/hardware/…
    • 对不起,# 只是表示方法的符号。这是一个静态方法,不,没有没有参数的选项。为了节省输入参数,我链接到该方法的文档,以便您自己查看。空构造函数出现在文档中,因为根据定义,所有对象都有一个空构造函数。但它并没有任何事情。
    • 很高兴知道。感谢您清除它。你这个答案是对的
    • 另外,我的相机在单步调试器时工作,但如果我在没有调试器的情况下在模拟器中运行时,只会显示黑色。有什么想法吗?
    猜你喜欢
    • 2022-11-12
    • 2020-03-31
    • 2012-05-27
    • 2015-05-02
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多