2

我创建了一个自定义相机,它在 Api 22 上一直失败,但适用于所有其他版本。我试过了。我正在使用折旧的相机 API,因为 minver 是 API 19。一直在尝试解决问题,但似乎无法解决。请帮忙

这是相机预览类

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)
4

1 回答 1

2

找到了解决办法,相机预览没有问题,我没有设置要打开的相机类型。但是仍然不明白为什么当我在 API 19 和 & 22 上发短信时它会抛出错误。

于 2018-08-14T15:51:41.337 回答