android 2.3.6中的摄像头方向

时间:2012-06-25 07:44:43

标签: android orientation android-camera

我在Android 2.3.6设备中显示相机预览时遇到问题。我使用此代码更改了相机方向,但是对于2.3.6它不起作用,而对于其他版本它正常工作。

In device 2.2.1

In device 2.3.6

设备2.2.1中的第一个图像,设备2.3.6中的第二个图像。我希望相机预览与设备2.3.6中的2.2.1相同。下面是我的代码

if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            {
         mCamera.setDisplayOrientation(90);


       }
        else
        {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {

               p.set("orientation", "portrait");
               p.set("rotation", 90);

            }
           if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {

                p.set("orientation", "landscape");
                p.set("rotation", 90);

            }
     }   

我在清单中指定了屏幕方向画像。请建议我解决。谢谢。

1 个答案:

答案 0 :(得分:0)

请参考以下代码:

private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
    int rotation = getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    int result;

    if(Build.VERSION.SDK_INT>10){
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        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;
        }

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

    }else{
        if(cameraId==CAMERA_FACING_BACK){
            camera.setDisplayOrientation(90);
        }else{
            camera.setDisplayOrientation(270);
        }
    }
}

您可以使用

获取相机ID
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private Camera openFrontFaceCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                mCameraId=camIdx;
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private Camera openRearFaceCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK  ) {
            try {
                mCameraId=camIdx;
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}