【问题标题】:Use camera in landscape when my app is in portrait当我的应用处于纵向时,在横向使用相机
【发布时间】:2015-08-28 11:45:42
【问题描述】:

我已经尝试了各种方法来完成我想要的行为,但似乎没有任何效果。

我的应用锁定在纵向模式(我不想要横向 UI)但我希望能够切换相机显示以显示纵向和横向的相机预览 模式交替。

我对拍摄图像不感兴趣。

要以纵向显示预览,我只需这样做:

camera.setDisplayOrientation(90);

我的预览效果很好,但是,当我尝试在横向模式下显示相机预览时,我会这样做:

camera.setDisplayOrientation(0);

预览实际上是旋转的,但是来自相机的图像没有旋转,最终的结果只是一个纵向图像旋转到横向。

各种方法我都试过了:

1)用parameters.setRotation()旋转相机,但这只会影响最终保存的图像,与预览无关;

2) 设置parameters.set("orientation", "landscape") 但这似乎没有效果(可能是一个旧的且尚未支持的命令?);

3)设置以下方法的每个组合,但没有,有效旋转源自相机的图像的唯一方法似乎是物理旋转我的设备......

这是我的意思的实际演示:

如您所见,parameters.setRotation() 在预览中没有效果,在纵向时是正确的,但在横向时只是旋转,因为相机不会自行旋转,但只需放置旋转的肖像图像。

那么,有没有办法在应用处于纵向时以横向显示相机预览?

谢谢。

【问题讨论】:

  • 你有没有看到这个问题 - stackoverflow.com/questions/16128608/…
  • @Alexandr 是的,但正如我在问题中所说,我的问题不是最终保存的图像(我对捕获图像不感兴趣),而只是预览。

标签: java android camera screen-rotation


【解决方案1】:

查看此方法来设置相机的方向。参数orientation是Display.getRotation(),你可以根据自己的需要改变设置。:

public void setCameraOrientation(int orientation) {
    int orientationDegree = 0;
    switch (orientation) {
        case Surface.ROTATION_0:
            orientationDegree = 0;
            break;
        case Surface.ROTATION_90:
            orientationDegree = 90;
            break;
        case Surface.ROTATION_180:
            orientationDegree = 180;
            break;
        case Surface.ROTATION_270:
            orientationDegree = 270;
            break;
    }
    int displayOrientation = (cameraInfo.orientation - orientationDegree + 360) % 360;
    camera.setDisplayOrientation(displayOrientation);
    Camera.Parameters parameters = camera.getParameters();
    parameters.setRotation(displayOrientation);
    camera.setParameters(parameters);
}

有关整个上下文,请查看我的相机演示应用程序here。特别是CameraPreview 类。

【讨论】:

  • 我的应用被锁定为纵向,正如我在第 1 点中所说,parameters.setRotation() 对预览图像没有影响,就在最后的捕获中。我已经通过实践演示更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
相关资源
最近更新 更多