如何以纵向打开相机? [英] How to open camera with a portrait orientation?

查看:112
本文介绍了如何以纵向打开相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My whole android camera app is in portrait mode, so when I open the camera I need it to automatically open in portrait mode. When I open the camera now the preview is sideways. How do I set the camera preview to open in portrait mode so the preview will look right?

推荐答案

您可以从

You could use this method from the Android Developer documentation to rotate the camera preview.

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

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

Set the clockwise rotation of preview display in degrees. This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

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

我希望这会有所帮助.

这篇关于如何以纵向打开相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆