安卓相机旋转

9

我有一部安装了Android 2.1操作系统的摩托罗拉Defy手机,并且正在开发一个带有相机预览功能的应用程序。问题是,在安装了Android 2.1操作系统的三星Galaxy S上,相机可以正常工作,但在摩托罗拉手机上,相机会旋转90度。我已经尝试过以下方法:

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

但是它没有起作用。我尚未找到任何解决方案。


请参考以下链接中的答案:https://dev59.com/3WYq5IYBdhLWcg3w8VG8 - Shirish Herwade
这个答案可以帮助你。 - IgniteCoders
6个回答

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

谢谢你的回答,但是setDisplayOrientation只适用于Android 2.2或2.3,而我的应用程序在2.1中。是否有另一种方法也适用于2.1?如果有人解决了这个问题,请帮助我。 - Laura
1
嗯,我在2.1版本上测试过了,它能够完美运行。我认为你指的是mediarRecord.setDisplayOrientation- 这个只在2.2或更高版本中支持。 - Peter
Camera.setDisplayOrientation仅支持API 8(2.2)。请参阅http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)。 - User
4
lp和aspect是什么变量? - Derzu
这是关于编程的内容。将其翻译成中文:这是在2011年发生的事情,我记不清了。aspect指的是aspectRatio,lp代表layoutparams。 - Peter

8
现在,在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);
}

1
@Fahad 相机 ID 指的是后置或前置摄像头。0 表示后置,1 表示前置。最好使用静态实例变量 Camera.CameraInfo.CAMERA_FACING_BACKCamera.CameraInfo.CAMERA_FACING_FRONT - Steven
@Timmmm JFYI,“camera.setDisplayOrientation” 在三星 Galaxy SII 上不起作用。 - Steven
@StevenWolfe 在 camera.setDisplayOrientation(result); 之后尝试使用 camera.getParameters().setRotation(result); - Pierre

2

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

这段代码可能在某些设备上可行,但在我的Milestone/Droid上失败了 :(

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

您可以在http://code.google.com/p/android/issues/detail?id=1193#c42中了解更多信息。


这个在我的橙色旧金山上运作得非常棒。谢谢 :) - jampez77

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


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

如果CameraPreview扩展了某个视图,那么在onLayout方法中我们还需要反转宽度/高度的计算。这实际上非常有效。 - radhoo

0

我认为您无法进行任何设置来支持API 2.2至2.1。该API不在您当前设备库中。您必须切换到2.2以支持API级别8。顺便说一下,我也尝试使用API级别7:

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

这个函数在三星Galaxy Tab上运行良好,但在Nexus One上不行。三星Galaxy Tab使用的是2.2.0操作系统,而Nexus One使用的是2.2.1操作系统。当我尝试使用API级别8时:

camera.setDisplayOrientation(90);

它们两个都很好用。所以我认为当我们在Android OS 2.2.1上使用时,API级别7存在问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接