【问题标题】:Detect if image was taken with front camera检测图像是否是用前置摄像头拍摄的
【发布时间】:2015-06-19 11:54:16
【问题描述】:

我有一个 Android 应用程序,允许用户使用他们的相机上传个人资料照片。问题是,当用户使用前置摄像头拍照时,手机上存储的图像会被镜像。

我可以将图像镜像回其原始状态,但是我无法专门对前置摄像头图片执行翻转。

有没有办法判断这张照片是不是用前置摄像头拍摄的?

这是我用来获取图片的一些代码

final boolean isCamera;
if (data == null) {
    isCamera = true;
} else {
    final String action = data.getAction();
    if (action == null) {
        isCamera = false;
    } else {
        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    }
}

Uri selectedImageUri;
if (isCamera) {
    selectedImageUri = outputFileUri;
} else {
    selectedImageUri = (data == null) ? null : data.getData();
}
Bitmap selectedBitmap;

// Check if the url is not null
if (selectedImageUri != null) {
    // store the new bitmap
    selectedBitmap = BitmapFactory.decodeFile(outputFileUri.getEncodedPath());
    int i = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;

    // if camera and front facing flip
    // HERE IS WHERE I NEED HELP
    if(isCamera && selectedBitmap != null){
        selectedBitmap = UtilsLibrary.flip(selectedBitmap);
        FileOutputStream out = null;
        try {

            out = new FileOutputStream(selectedImageUri.getEncodedPath());
            selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
cropImage(selectedImageUri);

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: android bitmap camera mediastore image-capture


    【解决方案1】:

    试试下面的代码:

    CameraInfo cameraInfo = new CameraInfo();
    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
             // do your logic
    }
    

    【讨论】:

    • 我试过这个,Camera.CameraInfo cameraInfo = new Camera.CameraInfo();if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) ,但它没有进入 if 语句并声明相机已被弃用
    • 有多少个摄像头?如果只有一个,有时它会是相机朝后。
    • @TaylorCourtney 在我用来测试的设备(Nexus 5 和 Android 5.1)上有 2 个摄像头,我所有的测试都是用前置摄像头拍摄的照片。
    • 好的,您可以使用 CameraDevice 或使用不同的 sdk 与较旧的 sdk 进行编译,例如 api 15 左右。
    【解决方案2】:

    我看到你的代码中有这一行。这就是你需要的。你只需要完成它。

    int i = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;
    

    一般你需要检测你从文件中读取的所有图片的方向。

    使用下面的这个方法。

    ExifInterface exif = new ExifInterface(outputFileUri.getEncodedPath());
    String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    

    【讨论】:

    • 我的方向变量设置为0,即ORIENTATION_UNDEFINED
    • @codingViking 这意味着您的相机应用程序没有正确处理 exif 信息。那么我认为没有一种完美的方式可以让您自己判断图像方向。也许您可以在您的应用程序上放置一个手动翻转按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多