3

一周前我发布了一个名为“Cam Shooter”的应用程序,它使用相机表面视图和开火按钮。我收到了 20 多个关于此错误的报告:

 java.lang.RuntimeException: set display orientation failed
 at android.hardware.Camera.setDisplayOrientation(Native Method)
 at com.jouni.camsniper.MainActivity.surfaceChanged(MainActivity.java:139)
 at android.view.SurfaceView.updateWindow(SurfaceView.java:573)
 at android.view.SurfaceView.setFrame(SurfaceView.java:296)
 at android.view.View.layout(View.java:7199)
 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
 at android.view.View.layout(View.java:7205)
 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
 at android.view.View.layout(View.java:7205)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 at android.view.View.layout(View.java:7205)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 at android.view.View.layout(View.java:7205)
 at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
 at android.view.ViewRoot.handleMessage(ViewRoot.java:1870)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:130)
 at android.app.ActivityThread.main(ActivityThread.java:3683)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
 at dalvik.system.NativeStart.main(Native Method)

在我的代码中,有一些行可以修复相机的旋转和纵横比,这可能是导致问题的原因。

 @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {       
    Parameters parameters = mCamera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size previewSize = previewSizes.get(4); //480h x 720w

    parameters.setPreviewSize(previewSize.width, previewSize.height);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

    mCamera.setParameters(parameters);

    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if(display.getRotation() == Surface.ROTATION_0) {                        
        mCamera.setDisplayOrientation(90);                             // LINE 139
    } else if(display.getRotation() == Surface.ROTATION_270) {
        mCamera.setDisplayOrientation(180);
    }

    mCamera.startPreview();
}

有谁知道如何摆脱这个错误?

4

1 回答 1

1

您找到解决问题的方法了吗?我想知道您是否尝试在设置预览显示的新旋转之前停止捕获和绘制预览帧?例如:

mCamera.stopPreview();                    
mCamera.setDisplayOrientation(degrees);
mCamera.startPreview();
于 2018-05-23T16:40:28.973 回答