将相机SurfaceView旋转为纵向

时间:2011-09-17 14:16:37

标签: android camera rotation

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

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

提供尺寸的功能如下所示......

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

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

7 个答案:

答案 0 :(得分:32)

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

  1. 传感器的相对方向和设备的“自然”方向(通常是手机的肖像,平板电脑的风景)
  2. 设备的当前UI方向(纵向,横向或相反)
  3. 相关摄像头是前置摄像头还是后置摄像头(因为前置预览流是水平镜像的)
  4. Camera.setDisplayOrientation的文档包含有关如何正确处理此问题的示例代码。我在这里复制它:

    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将用作指示符)。

答案 1 :(得分:8)

我能够通过将以下代码放在onSurfaceChanged()来解决旋转问题:

    if (mHolder.getSurface() == null) {
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }

    // make any resize, rotate or reformatting changes here
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {

        mCamera.setDisplayOrientation(90);

    } else {

        mCamera.setDisplayOrientation(0);

    }
    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e) {
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }

但是这会产生另一个问题,或者说更好的问题,并没有解决问题 - 虽然正确定位,预览图像仍然只占用了景观视图所用的相同空间。我最终只是放弃并强迫横向进行:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
在onCreate()中

并设计了我的横向布局。祝你好运,我希望有人能解决这个次要问题!

答案 2 :(得分:6)

尝试这一点,但我尝试使用三星Galaxy Tab

public void surfaceCreated(SurfaceHolder holder)
{
   // The Surface has been created, acquire the camera and tell it where to draw.
   mCamera = Camera.open();

   Parameters params = mCamera.getParameters();

   if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
   {
    params.set("orientation", "portrait");
    mCamera.setDisplayOrientation(90);
   }

    try
      {
      mCamera.setPreviewDisplay(holder);
      }
      catch (IOException exception)
      {
        mCamera.release();
        mCamera = null;
      }

}

答案 3 :(得分:2)

我通过添加:

解决了这个问题
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

到onCreate事件。

答案 4 :(得分:2)

请试试这个..

@Override
 public void surfaceChanged(SurfaceHolder holder,
                       int format, int width, int height)
{
// TODO Auto-generated method stub

if(previewing)
{
    camera.stopPreview();
    previewing = false;
}
 Camera.Parameters parameters = camera.getParameters();
      Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
   int or=cameraInfo.orientation;
     // You need to choose the most appropriate previewSize for your app
   // .... select one of previewSizes here
           /* parameters.setPreviewSize(previewSize.width, previewSize.height);*/
   if(display.getRotation() == Surface.ROTATION_0)
    {

    camera.setDisplayOrientation(90);
    or=90;
    }

    if(display.getRotation() == Surface.ROTATION_180)
    {
        camera.setDisplayOrientation(270);
    or=270;
    }
    if(display.getRotation() == Surface.ROTATION_270)
    {
        camera.setDisplayOrientation(180);
        or=180;
    }

  parameters.setRotation(or);

  camera.setParameters(parameters);
 try
{
        camera.setPreviewDisplay(cameraSurfaceHolder);
        camera.startPreview();
        previewing = true;
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

答案 5 :(得分:2)

我做到了!

我已经在surfaceCreated()方法中以这种方式解决了这个问题

 public void surfaceCreated(SurfaceHolder holder) {
    try {

        camera = Camera.open();
    } catch (RuntimeException e) {
        System.err.println(e);
        return;
    }
    Camera.Parameters param;
    param = camera.getParameters();
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
    {
        param.set("orientation", "portrait");
        setCameraDisplayOrientation(this,1,camera);
    }
    camera.setParameters(param);

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (Exception e) {
        System.err.println(e);
        return;
    }
}

在下面添加此方法

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

3:确保您在此活动之前添加了摄像头permiison以支持活动中的牛轧糖,因为此活动中没有呼叫摄像机的权限,因为已创建活动的表面创建呼叫,并且您的权限将等待打开摄像头

答案 6 :(得分:-1)

仅添加

mCamera.setDisplayOrientation(90);
相关问题