Android Camera in View with Buttons and Text over

三世轮回 提交于 2020-01-03 03:37:08

问题


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.



来源:https://stackoverflow.com/questions/8475532/android-camera-in-view-with-buttons-and-text-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!