繁体   English   中英

Android肖像锁定相机预览

[英]Android portrait locked Camera Preview

我已经像在Android的ApiDemos应用程序中一样实现了Camera Preview。 问题是我想要我的活动,预览被显示为锁定为肖像。 所以我在清单中将screenOrientation设置为portrait。

'getOptimalPreviewSize'方法为预览返回相反的值,这意味着,当需要返回480x720时,该方法返回720x480,并且我的活动的FrameLayout中心有一个小预览(这不是我想要的,我希望它匹配父级(是的,布局用“match_parent”定义)。

我试过了:

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

似乎没有任何帮助。

为什么我无法在纵向模式下显示相机预览? 锁定。

谢谢

以下是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) {
        }
    });
}

使用以下布局文件:

<?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>

尝试setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

这会将活动锁定到该方向。

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

这将设置相机的方向并正确预览。

暂无
暂无

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

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