简体   繁体   English

Android肖像锁定相机预览

[英]Android portrait locked Camera Preview

I have implemented the Camera Preview just as it is in the ApiDemos application of Android. 我已经像在Android的ApiDemos应用程序中一样实现了Camera Preview。 The thing is that I want my activity, where the Preview is shown to be locked to portrait. 问题是我想要我的活动,预览被显示为锁定为肖像。 So I set screenOrientation to portrait in the manifest. 所以我在清单中将screenOrientation设置为portrait。

The 'getOptimalPreviewSize' method returns opposite values for the preview, meaning, when it's needed to return 480x720, the method returns 720x480, and I have small preview centered in my FrameLayout of the activity (which is not what I want, I want it to match the parent (yes, the layout is defined with "match_parent"). 'getOptimalPreviewSize'方法为预览返回相反的值,这意味着,当需要返回480x720时,该方法返回720x480,并且我的活动的FrameLayout中心有一个小预览(这不是我想要的,我希望它匹配父级(是的,布局用“match_parent”定义)。

I tried: 我试过了:

  • setDisplayOrientation(90); setDisplayOrientation(90);
  • parameters.set("orientation", "portrait"); parameters.set(“orientation”,“portrait”);
  • parameters.set("rotation", "90"); parameters.set(“rotation”,“90”);

Nothing seems to help. 似乎没有任何帮助。

Why can't I show Preview of the Camera in portrait mode? 为什么我无法在纵向模式下显示相机预览? Locked. 锁定。

Thanks 谢谢

Here is a working example of a portrait preview of camera on SurfaceView : 以下是SurfaceView上相机纵向预览的工作示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface);
    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {

        private Camera camera;

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera = Camera.open();
                camera.setPreviewDisplay(holder);
                camera.setDisplayOrientation(90);
                camera.startPreview();
            } catch (IOException e) {
                Log.e(TAG, "Error", e);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) { 
            camera.stopPreview();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });
}

With the following layout file: 使用以下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Try setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 尝试setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

That will lock the activity to that orientation. 这会将活动锁定到该方向。

The following code from the Google Docs: 来自Google文档的以下代码:

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);
}

Which will set the orientation of the camera and preview correctly. 这将设置相机的方向并正确预览。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM