java - Android - 相机在某些设备的后置摄像头上提供颠倒的数据

标签 java android camera android-camera orientation

我正在 GLSurfaceView 上渲染相机预览,一切正常,但相机预览在某些设备上显示颠倒,特别是在 Nexus 5X 上。我已经检查过这个解决方案Android - Camera preview is sideways 。这是我处理方向问题的代码

 private void setCameraDisplayOrientation() {
    Constants.debugLog(TAG_DEBUG, "setCameraDisplayOrientation");
    Constants.debugLog(TAG, "setCameraDisplayOrientation ");

    if (camera == null) {
        Constants.debugLog(TAG + " Owncamera", "setCameraDisplayOrientation - camera null");
        return;
    }

    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(currentCameraId, info);

    WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int rotation = winManager.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;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        this.cameraOrientation = 2;
    } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
        this.cameraOrientation = 1;
    }

    Constants.debugLog(TAG_DEBUG, "result: " + result + "  this.cameraOrientation == " + this.cameraOrientation);
    Constants.debugLog(TAG, "result: " + result + "  this.cameraOrientation == " + this.cameraOrientation);
    //Log.e("camera_orient", "res: "+ result);
    camera.setDisplayOrientation(result);
}

Camera.CameraInfo info 类中,我可以获得 orientation 。对于最大设备,我在肖像模式上获得的值为“前置摄像头= 270,后置摄像头= 90”,但在Nexus 5X上为前置和后置摄像头提供摄像头方向 270,270。除了在其他设备上,我的摄像头为纵向前置和后置摄像头提供结果值 90模式,但对于 Nexus 5x 前置摄像头 90 后置摄像头 270。我也尝试过在 camera.setDisplayOrientation(90); Nexus 5X 设备上设置值固定 90,但没有工作 如何处理这个问题,以便所有设备相机方向都相同,并且我不会得到任何旋转图像??

最佳答案

问题原因:默认情况下,使用相机 api 1 时,大多数设备会返回默认方向为 90 度的图像。在使用相机 api 2 的情况下,我不确定这一点。但如果是Nexus 5x 和一些罕见的设备(使用相机 api 1),它返回默认旋转 270 度的图像,这是某些特定型号设备上的异常(exception)情况。因此,您必须为 Nexus 5X 添加 180 度的额外旋转。请检查打印 Nexus 5X 图像方向的日志。

关于java - Android - 相机在某些设备的后置摄像头上提供颠倒的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447987/

相关文章:

java - Spring 启动: template not found with Freemarker

android - 使用 mozilla pdf.js Android API 级别低于 19 在 Android WebView 中显示 PDF 文件

java - MediaRecorder:setCamera() - 错误相机不可用

ios - 头部运动检测 - iOS

java - 在java中对数组进行排序

java - 为什么在 String.valueOf 中返回 "null"String 而不是 java null?

java - 从类中删除 ActionListener

android - TextView 右侧的 ImageButton

java - 将字符串从 Activity 传递到 Fragment

iphone - 如何使用 UIImagePickerControllerCropRect