4

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

在设备 2.2.1

在设备 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);

            }
     }   

我已经在清单中指定了屏幕方向肖像。请建议我一个解决方案。提前致谢。

4

1 回答 1

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;
}
于 2014-01-27T11:04:32.037 回答