Android Camera in View with Buttons and Text over

2019-06-04 01:54发布

问题:

I implemented a Camera in my View like it is done in the API Demos. But it only works in the landscape mode. But that's not good for me, because I want to use it mostly in the portrait mode. So the buttons and so on look very bad. How is it possible to get a SurfaceView, which shows the CameraPreview and all together works in the protrait mode?

I don't think that it is necessary to post the code because I just used the code form the API demos!

Thank you very much!

回答1:

If you do not specify the screen orientation in AndroidManifest.xml file, the default display orientation is the landscape position and every time you rotate the phone android will change the acquired image in order to maintain the image aligned with phone. Thus, you have two options:

1.1. Specify the screen orientation in AndroidManifest.xml file and set the properly screen orientation:

AndroidManifest.xml:

        <activity
        android:label="@string/app_name"
        android:name=".CameraPreview" 
        android:screenOrientation="portrait" >

CameraPreview.java:

       mCamera.setPreviewDisplay(holder);
       mCamera.setDisplayOrientation(90);

or

AndroidManifest.xml:

        <activity
        android:label="@string/app_name"
        android:name=".CameraPreview" 
        android:screenOrientation="landscape" >

CameraPreview.java:

       mCamera.setPreviewDisplay(holder);
       mCamera.setDisplayOrientation(0);

1.2. Do not specify the screen orientation in AndroidManifest.xml file and set the properly screen orientation every time the screen is rotated:

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

      int rotation = ((Activity)mCtx).getWindowManager().getDefaultDisplay().getRotation();
      int degrees = 0;
      switch (rotation) {
        case Surface.ROTATION_0: degrees = 90; break;
        case Surface.ROTATION_90: degrees = 0; break;
        case Surface.ROTATION_180: degrees = 270; break;
        case Surface.ROTATION_270: degrees = 180; break;
      }

      mCamera.setDisplayOrientation(degrees);

      Camera.Parameters parameters = mCamera.getParameters();
      parameters.setPreviewSize(w, h);
      mCamera.setParameters(parameters);
      mCamera.startPreview();
    }

But since, it seems that you already have designed a portrait layout with buttons and so on, I think the best option is the first one.

Hope this helps and best regards.