【问题标题】:Android front camera images are being saved upside downAndroid前置摄像头图像正在倒置保存
【发布时间】:2016-05-08 18:49:08
【问题描述】:

我正在构建一个使用相机功能的 android 应用程序。我遇到的问题是我从前置摄像头获得的图像数据(字节 [])在我的三星 s7 和 nexus 手机上颠倒过来。它在预览中看起来是正确的,但是在我按原样保存数据然后在画廊中显示图像之后,它们都是颠倒的。我知道我可以在保存之前翻转图像数据,但我已经在运行 4.4 (kitkat) 的蓝光 C 5.0 HD 上测试了代码,并且该手机上的图像数据以正确的方式出现。所以总是翻转图像会导致其他设备上的错误。有人告诉我这个问题是因为在制造新的三星和 nexus 手机时,前置摄像头是倒置的以节省空间。我不确定这是否正确,但如果是这样,如果我翻转所有图像,它将弄乱具有正确相机方向的手机。那么有没有办法在保存图片之前检测图片数据的方向呢?

这是我正在使用的代码:

mCamera.takePicture(null, null, mPicture);

回调:

private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        processCameraCallback(data);
    }
};

处理数据:

    public void processCameraCallback(byte[] data) {
    confirmPhoto(true);
    //Make a new empty picture file
    try {
        pictureFile = Utils.createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        Log.e(TAG, "Failed to create photo file: " + ex.toString());
        confirmPhoto(false);
        return;
    }

    //Write the file to the storage
    try {
        FileOutputStream fos;
        if (pictureFile != null) {

            fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
        confirmPhoto(false);
        return;
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
        confirmPhoto(false);
        return;
    }
}

【问题讨论】:

  • 你有没有想过这个问题?我在使用本机相机应用程序的 S7 上遇到了同样的问题,还补偿了 EXIF 数据中返回的方向。

标签: android camera android-camera orientation


【解决方案1】:

使用此代码调整相机方向:

private  int detectCameraDisplayOrientation(Activity activity,
                                            Camera.CameraInfo 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;
    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;
    }
   return result;
}

这是来自相机 api 示例,可在 nexus 5x 上正常工作。(带有倒置相机)

之后只需调用 camera.setDisplayOrientation(displayOrientation); 它会正确保存图片。

【讨论】:

  • 该代码正在检测整个手机是否旋转。我的问题是,如果我用前置摄像头拍摄正常的直立图像,图像数据会以倒置的图像返回。我有三星 s7 edge 和 nexus 5,都有这个问题。我还有一个运行 4.4 (kitkat) 的蓝光 C 5.0 HD,并且该手机上的图像数据以正确的方式出现。
  • 此代码根据手机旋转设置相机图像的正确旋转。补偿相机倒置位置。你试过了吗?如果是并且它不起作用 - 好的......如果不是 - 你的评论似乎很奇怪。摄像头方向本身位于 cameraInfo.orientation 字段中,如果您只需要它。
  • 对不起,我试过你的解决方案,但没有解决。
  • camera.setDisplayOrientation 仅修复预览中的问题,但图像仍然以错误的方向保存。
  • 是的,这取决于硬件。看这里 - stackoverflow.com/questions/20478765/… afaik 如果图像以错误的方向保存,它应该写正确的 exif 标签。
猜你喜欢
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多