Android相机无法在APi 22(Lollipop)上运行,但可以在所有其他版本上运行

时间:2018-08-14 11:26:14

标签: java android android-camera camera-api

我创建了一个自定义相机,它在Api 22上一直失败,但在所有其他版本上都可以使用。我已经试过了。我正在使用已贬值的Camera API,因为主服务器为API19。一直在尝试解决此问题,但似乎无法解决。请帮忙

这是Camera Preview类

public CameraPreview(Context context, android.hardware.Camera camera,int cameraType) {
    super(context);
    this.context = context;
    this.camera = camera;
    this.cameraType=cameraType;
    surfaceHolder = getHolder();
//        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.addCallback(this);




}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera.setPreviewDisplay(holder);
        setCameraDisplayOrientation((Activity) context,cameraType,camera);

        camera.startPreview();
    } catch (IOException e) {
        Log.d("tag", "Error setting camera preview: " + e.getMessage());
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    if (holder == null) {
        return;
    }

    try {
        camera.stopPreview();
    } catch (Exception e) {
        Log.d("tag", "Error setting camera stop: " + e.getMessage());
    }

    try {
        camera.setPreviewDisplay(holder);
//                camera.setDisplayOrientation(90);
        setCameraDisplayOrientation((Activity) context,cameraType,camera);
        camera.startPreview();
    } catch (IOException e) {
        Log.d("tag", "Error setting camera preview: " + e.getMessage());
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.release();
}
public static void setCameraDisplayOrientation(Activity activity,
                                               int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

我一直遇到的错误是在“开始预览”上

8-14 11:18:00.115 4391-4391/com.customcamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.customcamera, PID: 4391
java.lang.RuntimeException: startPreview failed
    at android.hardware.Camera.startPreview(Native Method)
    at com.customcamera.CameraPreview.surfaceCreated(CameraPreview.java:45)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:580)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

找到了解决方案,“相机预览”没有问题,我没有将相机类型设置为打开。但是仍然不明白为什么我在用API 19和&22发短信时它不会引发错误。