Android-相机在某些设备的后置相机上提供倒置数据 [英] Android - Camera provide upside down data on back camera in some devices

查看:241
本文介绍了Android-相机在某些设备的后置相机上提供倒置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在GLSurfaceView上渲染camera preview,但一切正常,但是camera preview在某些设备上倒挂显示,特别是在 Nexus 5X 上.我已经检查了此解决方案 Android-相机预览是横向的 .这是我处理定向问题的代码

I am rendering camera preview on GLSurfaceView everything works fine but camera preview show upside down on some device specially on Nexus 5X . I have checked this solution Android - Camera preview is sideways . This is my code for handling orientation problem

 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.对于最大的设备,我在portrait mode上获得的值为"for front camera = 270, back camera = 90",但在Nexus 5X上,它为前置和后置摄像头提供了camera orientation 270,270 .在纵向模式下为前置和后置摄像头提供result值90,但为Nexus 5x前置摄像头90和后置摄像头270提供值.我也尝试通过在camera.setDisplayOrientation(90); Nexus 5X设备上设置固定值为90的方式来尝试操作,但不起作用 如何处理此问题,以便所有设备相机的方向都相同,而我不会得到任何旋转的图像?

From the Camera.CameraInfo info class, i can get orientation . For maximum device the values i get on portrait mode is "for front camera = 270, back camera = 90 "" but on Nexus 5X it provide camera orientation 270,270 for both front & back camera. Beside on other devices my camera provide result value 90 for both front and back camera in portrait mode but for Nexus 5x front camera 90 back camera 270. I have also tried by setting value fixed 90 on camera.setDisplayOrientation(90); Nexus 5X device but don't work How to handle this problem so that all device camera orientation will be same and i won't get any rotated image ??

推荐答案

问题原因:默认情况下,最大数量的设备在使用Camera api 1时会以90度默认方向返回图像.有关在使用相机API 2的情况下的解决方法.但是在Nexus 5x和某些稀有设备(使用相机API 1)的情况下,它会返回默认旋转270度的图像,这在某些特定型号的设备上是例外情况.因此,您必须为Nexus 5X添加180度的附加旋转角度.请检查打印Nexus 5X图像方向的日志.

Reason of problem: By default maximum devices return an image with 90 degree default orientation while using camera api 1. I am not sure about this in case of using camera api 2. But in case of Nexus 5x and some rare device (using camera api 1) , It return image with 270 degree default rotation which is an exceptional case on some specific model device. SO you have to add an addition rotation of 180 degree for Nexus 5X. Please check printing the log of image orientation of Nexus 5X.

这篇关于Android-相机在某些设备的后置相机上提供倒置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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