安卓相机Api - Nexus 6 -相机显示显示反向



要求以人像模式显示相机。

相机视图在所有其他设备上正常显示,如- Nexus 4, Nexus 5,三星S3,三星S4等。

使用NEXUS 6时,相机显示为上下颠倒。

这是我如何设置相机参数-

private void setCameraParameters() {
    try {
        Parameters parameters = mCamera.getParameters();
        Camera.CameraInfo camInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(mCameraId, camInfo);
        int cameraRotationOffset = camInfo.orientation;
        System.out.println("Offset :" + cameraRotationOffset);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            parameters.set("orientation", "portrait");
            mCamera.setDisplayOrientation(90);
            Camera.Size size = getBestCameraSize(90,getWidth(),getHeight(),parameters);
            if(size==null) {
                size = getOptimalPreviewSize(90, getWidth(), getHeight(), parameters);
            }
            parameters.setPreviewSize(size.width,size.height);
            mCamera.setParameters(parameters);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

经过我的实验,我明白了将显示方向设置为270度在Nexus 6上工作得很好。

查询-如何找出所有设备需要显示方向为90和所有设备需要显示方向为270 ?如何检测呢?

使用此代码设置相机的方向为人像。这是一个通用的,应该适用于所有设备:

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

来源:http://developer.android.com/reference/android/hardware/Camera.html

最新更新