将相机 SurfaceView 旋转为纵向 [英] Rotating a Camera SurfaceView to portrait

查看:13
本文介绍了将相机 SurfaceView 旋转为纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了一些关于使用表面视图更改相机方向的帖子,但我从以下示例中获取了我的代码:

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.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

提供维度的函数如下所示...

The function that provides the dimensions looks like this...

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

我的问题是当我改变设备的方向时,预览图片保持横向.我尝试设置相机的方向,但这导致了非常奇怪的结果.有谁知道我需要更改什么才能正常旋转?

My problem is that when I change the orientation of the device, the preview picture stays landscape. I tried setting the orientation of the camera but this resulted in very strange results. Does anyone know what I need to change to have this rotate properly?

推荐答案

正确调整相机预览方向的代码有点复杂,因为它必须考虑到

The code to correctly adjust the camera preview orientation is a bit complex, since it has to take into account

  1. 传感器的相对方向和设备的自然"方向(通常是手机纵向,平板电脑横向)
  2. 设备的当前 UI 方向(纵向、横向或反向)
  3. 有问题的摄像头是前置摄像头还是后置摄像头(因为前置预览流是水平镜像的)

Camera.setDisplayOrientation 的文档有示例代码说明如何正确处理这个问题.我在这里复制它:

The documentation for Camera.setDisplayOrientation has sample code on how to deal with this correctly. I'm reproducing it here:

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

在您的 UI 绘制完成(onSurfaceChanged 将用作指示器)或设备 UI 旋转(onConfigurationChanged 将用作指示器)后调用此函数.

Call this after your UI has been drawn (onSurfaceChanged would work as an indicator) or the device UI rotates (onConfigurationChanged would work as an indicator).

这篇关于将相机 SurfaceView 旋转为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆