6

我在android中开发了一个相机应用程序。它使用表面视图捕获图像。下面是我用来打开相机的代码

try {
            // attempt to get a Front Camera instance
            c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out
                    .println("fail to connect to Front Camera");
        }
        if (c == null) {
            try {
                // attempt to get a Back Camera instance
                c = Camera.open(1);
            } catch (Exception e) {
                // TODO: handle exception
                System.out
                        .println("fail to connect to Camera   with      id   =   1");
            }
        }
        if (c == null) {
            try {
                // attempt to get a Back Camera instance
                c = Camera.open(0);
            } catch (Exception e) {
                // TODO: handle exception
                System.out
                        .println("fail to connect to Camera   with      id   =   0");
            }
        }
        if (c == null) {
            try {
                // attempt to get a Back Camera instance
                c = Camera.open();
            } catch (Exception e) {
                // TODO: handle exception
                System.out
                        .println("fail to connect to Back Camera");
                return c;
            }

其中 c 是 Camera 的一个对象。

它在除 Nexus 7 平板电脑之外的其他手机上运行良好。在 Nexus 7 中,除了最后一个 ie 之外,代码在所有情况下都会引发异常c = Camera.open();,但对象仍然cnull.

这是堆栈跟踪

11-22 12:36:57.559 W/System.err(7621): java.lang.NullPointerException
11-22 12:36:57.559 W/System.err(7621): at 

com.MyPackage.OpenCamera.getFrontCameraInstance(OpenCamera.java:238)
11-22 12:36:57.559 W/System.err(7621): at 

com.MyPackage.OpenCamera.onCreate(OpenCamera.java:123)
11-22 12:36:57.559 W/System.err(7621): at 

android.app.Activity.performCreate(Activity.java:5104)
11-22 12:36:57.559 W/System.err(7621): at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-22 12:36:57.559 W/System.err(7621): at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-22 12:36:57.559 W/System.err(7621): at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-22 12:36:57.559 W/System.err(7621): at 

android.app.ActivityThread.access$600(ActivityThread.java:141)
11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread

$H.handleMessage(ActivityThread.java:1234)
11-22 12:36:57.559 W/System.err(7621): at 

android.os.Handler.dispatchMessage(Handler.java:99)
11-22 12:36:57.559 W/System.err(7621): at android.os.Looper.loop

(Looper.java:137)
11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.main

(ActivityThread.java:5039)
11-22 12:36:57.559 W/System.err(7621): at 

java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invoke

(Method.java:511)
11-22 12:36:57.559 W/System.err(7621): at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

(ZygoteInit.java:793)
11-22 12:36:57.559 W/System.err(7621): at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-22 12:36:57.559 W/System.err(7621): at dalvik.system.NativeStart.main

(Native Method)
11-22 12:36:57.559 I/System.out(7621): Error in setting Parameter
11-22 12:36:57.609 I/ActivityManager(480): Displayed 
11-22 12:36:57.679 W/System.err(7621): at 

android.location.Geocoder.getFromLocation(Geocoder.java:136)
11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.

$MyTimmer$1.run(OpenCamera.java:336)
11-22 12:36:57.679 W/System.err(7621): java.lang.NullPointerException
11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.

$MyTimmer$1.run(OpenCamera.java:344)

所以我不能用它来捕捉图像。有什么解决办法???

谢谢...

4

2 回答 2

6

在 Nexus 7 上使用以下代码时: int cameraId = CameraInfo.CAMERA_FACING_FRONT; cameraId 为 1,但是,如您所见,使用 1 作为参数会导致错误。我发现使用 0 而不是 1 有效,但每次使用后必须释放相机,所以我继续调用 Camera.open(cameraId).release(); 我意识到这很丑陋,但它似乎解决了 Nexus 7 的问题。

    int cameraId = -1;
    boolean errorFound = false;
    boolean hasFeatCamera = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);

    if (hasFeatCamera) {

            try{
                 cameraId = CameraInfo.CAMERA_FACING_BACK;
                 Camera.open(cameraId).release();
                }catch(Exception e){
                    Camera.open(0).release();
                }

    } else if(CameraInfo.CAMERA_FACING_FRONT>-1){

        try{
         cameraId = CameraInfo.CAMERA_FACING_FRONT;
         Camera.open(cameraId).release();
        }catch(Exception e){
            errorFound = true;

        }

        if(errorFound){
            try{
                Camera.open(0).release(); // Put in for Nexus 7 as  CameraInfo.CAMERA_FACING_FRONT= 1 but it only loads if the id is actually 0
                cameraId = 0;
            }catch(Exception e){
                cameraId = -1;
            }
        }
    }

    if(cameraId<0){
        Toast.makeText(this, "No camera found.",
                    Toast.LENGTH_LONG).show();
     } 
于 2013-01-18T20:34:34.423 回答
3

有点晚了,但也许它可以帮助某人。我遇到了完全相同的问题,无法让 Nexus 7 相机与提供给 getCamera(idx) 的任何索引一起工作。

我从 Playstore 安装了一个相机“Kamera Nexus 7”应用程序。重置/更新后我没有任何相机应用程序。这个应用程序有效,我用过一次后,我可以使用 Camera.getCamera(0);

所以也许这里的答案中没有包含一个额外的“解锁要求”?(在安装应用程序之前,它们对我不起作用)

于 2013-09-02T13:50:45.610 回答