当我的应用处于人像状态时,请使用风景相机

时间:2015-08-28 11:45:43

标签: java android camera screen-rotation

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

我的应用锁定在纵向模式(我不想要横向UI)但我希望能够切换相机显示以在肖像中显示相机预览和风景模式或者。

我对拍摄图片不感兴趣。

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

camera.setDisplayOrientation(90);

我的预览工作正常,但是,当我尝试以横向模式显示相机预览时,我会这样做:

camera.setDisplayOrientation(0);

并且预览实际上会旋转,但相机中的图像不会旋转,最终结果只是旋转到横向的肖像图像。

我已经尝试过各方面:

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

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

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

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

enter image description here

enter image description here

enter image description here

enter image description here

正如您所看到的,parameters.setRotation()在预览中执行没有效果,这在纵向时是正确的,但只是在横向旋转'导致相机不在#39; t自行旋转,但只需旋转肖像图像。

那么,当应用程序处于纵向状态时,有没有办法在横向显示相机预览?

感谢。

1 个答案:

答案 0 :(得分:0)

检查此方法以设置相机的方向。参数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类。