Rotating a Camera SurfaceView to portrait

前端 未结 7 1038
日久生厌
日久生厌 2020-12-14 04:37

I have looked up a few posts on changing the orientation of the camera with a surface view, but I have taken my code from the examples at:

http://developer.android.c

7条回答
  •  没有蜡笔的小新
    2020-12-14 04:58

    I was able to solve the rotation problem by putting the following code in onSurfaceChanged():

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
    
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }
    
        // make any resize, rotate or reformatting changes here
        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
    
            mCamera.setDisplayOrientation(90);
    
        } else {
    
            mCamera.setDisplayOrientation(0);
    
        }
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
    
        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    

    BUT this created another problem, or better put, didn't solve a problem-while oriented correctly, the preview image still only took up the same amount of space that the landscape view did. I ended up just giving up and forcing landscape orientation with:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    in onCreate() and designed my layout for landscape. Best of luck, and I hope someone has an answer for this secondary problem!

提交回复
热议问题