Android相机旋转

时间:2011-03-15 08:14:30

标签: android android-camera

我有一台摩托罗拉Defy OS Android 2.1,我用相机预览制作应用程序。问题是相机在搭载Android 2.1的三星Galaxy S上工作正常,但在摩托罗拉上,相机旋转90度。我试过这样做:

Parameters parameters = camera.getParameters();
parameters.setRotation(90);

但它不起作用。我还没有找到任何解决方案。

6 个答案:

答案 0 :(得分:16)

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        camera.setDisplayOrientation(90);
        lp.height = previewSurfaceHeight;
        lp.width = (int) (previewSurfaceHeight / aspect);
    } else {
        camera.setDisplayOrientation(0);
        lp.width = previewSurfaceWidth;
        lp.height = (int) (previewSurfaceWidth / aspect);
    }

答案 1 :(得分:6)

现在Android文档中有官方示例代码(在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);
}

答案 2 :(得分:2)

2.1下不存在camera.setDisplayOrientation(int)!

此代码可能有效,但在里程碑/机器人中失败:(

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

您可以在http://code.google.com/p/android/issues/detail?id=1193#c42

中查看更多内容

答案 3 :(得分:2)

我发现此代码适用于Android 1.6及更高版本(适用于我使用2.1并在纵向模式下呈现预览而无需旋转)

public void surfaceCreated(SurfaceHolder holder){
        try{
            camera = Camera.open();
            setDisplayOrientation(camera, 90);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        }catch(IOException e){
            Log.d("CAMERA", e.getMessage());
        }

}

protected void setDisplayOrientation(Camera camera, int angle){
        Method downPolymorphic;
        try
        {
            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
            if (downPolymorphic != null)
                downPolymorphic.invoke(camera, new Object[] { angle });
        }
        catch (Exception e1)
        {
        }
}

该活动在AndroidManifest.xml上有android:screenOrientation =“portrait”

http://code.google.com/p/android/issues/detail?id=1193#c42

答案 4 :(得分:1)

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

答案 5 :(得分:0)

我认为您无法进行任何设置来支持API 2.2到2.1。 API在您当前的设备lib中没有。您必须更改为2.2以支持API级别8.顺便说一下,我还尝试使用API​​级别7:

Parameters parameters = camera.getParameters();
parameters.setRotation(90);

此功能适用于Samsung Galaxy Tab但Nexus one。三星Galaxy Tab使用OS 2.2.0,Nexus使用OS 2.2.1。当我尝试使用API​​级别8时:

camera.setDisplayOrientation(90);

两者都运作良好。所以我认为当我们在Android OS 2.2.1上使用时,API级别7存在问题。