1

如何在所有设备中保持相机处于纵向模式?我的应用高于 2.2

我试过了

在此处输入图像描述

public void setRotation(int rotation) {

    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;

    CameraInfo cameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);

    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (cameraInfo.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (cameraInfo.orientation - degrees + 360) % 360;
    }

    mCamera.stopPreview();
    mCamera.setDisplayOrientation(result);
    mCamera.startPreview();
}

但三星设备忽略mCamera.setDisplayOrientation(90)

编辑:

我发现忽略了这一点的设备:

Galaxy y(GT-S5360)(Android 2.3.6)

银河王牌(GT-S5830)(Android 2.6.3)

但 Galaxy S2 和其他更高版本的手机运行良好。

4

1 回答 1

0

你可以试试

    public int getCorrectCameraOrientation(CameraInfo info, Camera camera) {

  int rotation = context.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;  
  }else{
    result = (info.orientation – degrees + 360) % 360;
  }

  return result;
}

将此方法用作

mCamera.setDisplayOrientation(getCorrectCameraOrientation(mCameraInfo, mCamera));

这不足以进行旋转处理,您还需要执行以下步骤

mCamera.getParameters().setRotation(getCorrectCameraOrientation(mCameraInfo, mCamera));
于 2018-11-30T04:58:23.433 回答