【问题标题】:Android camera rotateAndroid相机旋转
【发布时间】:2011-03-15 08:14:29
【问题描述】:

我有一个 Motorola Defy OS Android 2.1,我用相机预览制作了一个应用程序。问题是摄像头在装有 Android 2.1 的三星 Galaxy S 上运行良好,但在摩托罗拉上,摄像头旋转了 90 度。我试过这样做:

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

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

【问题讨论】:

标签: android android-camera


【解决方案1】:
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 中工作吗?如果有人解决了这个问题,请帮助我。
  • 嗯。我在 2.1 上对其进行了测试,效果很好。我认为你的意思是 mediarRecord.setDisplayOrientation- 这在 2.2 或更高版本中确实支持
  • Camera.setDisplayOrientation 仅支持 API 8 (2.2) 请参阅 developer.android.com/reference/android/hardware/…
  • lp和aspect的变量是什么?
  • 它是在 2011 年。我不记得了。纵横比是纵横比,lp - layoutparams
【解决方案2】:

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

【讨论】:

  • @Fahad 摄像头 ID 指的是后置或前置摄像头。 0 表示背面,1 表示正面。可能最好使用静态实例变量Camera.CameraInfo.CAMERA_FACING_BACK 和Camera.CameraInfo.CAMERA_FACING_FRONT。
  • @Timmmm JFYI, camera.setDisplayOrientation 在三星 Galaxy SII 上什么都不做。
  • @StevenWolfe 在camera.setDisplayOrientation(result); 之后尝试camera.getParameters().setRotation(result);
【解决方案3】:

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

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

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

你可以在http://code.google.com/p/android/issues/detail?id=1193#c42看到更多

【讨论】:

  • 这对我的橙色旧金山非常有用。谢谢:)
【解决方案4】:

我发现此代码适用于 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

【讨论】:

    【解决方案5】:
    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方法中反转宽度/高度计算。
    【解决方案6】:

    我认为你不能做任何设置来支持 API 2.2 到 2.1。您当前的设备库中没有该 API。必须改到2.2才能支持API level 8。顺便说一下,我也尝试使用API​​ level 7:

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

    此功能在三星 Galaxy Tab 上运行良好,但在 Nexus 上运行良好。三星 Galaxy Tab 使用 OS 2.2.0,而 Nexus 使用 OS 2.2.1。当我尝试使用 API 级别 8 时:

    camera.setDisplayOrientation(90);
    

    他们都运作良好。所以我认为当我们在 Android OS 2.2.1 上使用 API 级别 7 时会出现问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      相关资源
      最近更新 更多