繁体   English   中英

如何以纵向打开相机?

[英]How to open camera with a portrait orientation?

我的整个android相机应用都处于纵向模式,因此当我打开相机时,我需要它自动以纵向模式打开。 当我现在打开相机时,预览是横向的。 如何设置相机预览以纵向模式打开,以使预览看起来正确?

您可以使用Android Developer文档中的此方法来旋转相机预览。

公共最终空白setDisplayOrientation(以度为单位)

以度为单位设置预览显示的顺时针旋转。 这会影响预览帧和快照后显示的图片。 此方法对于纵向模式应用程序很有用。 请注意,前置摄像头的预览显示在旋转之前会水平翻转,也就是说,图像会沿摄像头传感器的中心垂直轴反射。 因此,用户可以将自己视为镜子。

private 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 = 0;

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

我希望这有帮助。

可能是因为您尚未指定screenRotation尝试:

 android:screenOrientation="portrait" 

或尝试以下方法:

在清单中添加方向属性

android:screenOrientation=["unspecified" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "userLandscape" | "userPortrait" |
                                     "sensor" | "fullSensor" | "nosensor" |
                                     "user" | "fullUser" | "locked"]
So in your case it will be

<activity android:name=".yourCameractivity"
      ....
      android:screenOrientation="portrait"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM