检测图片是否使用前置摄像头拍摄

4

我有一个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);

任何帮助都将不胜感激,谢谢。
2个回答

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语句,并且显示Camera已被弃用。 - codingViking
有多少相机?如果只有一个,有时会面向后方。 - Taylor Courtney
@TaylorCourtney 在我用来测试的设备上(Android 5.1 的 Nexus 5),有两个摄像头,我所有的测试都是使用前置摄像头拍摄的照片。 - codingViking
好的,您可以使用CameraDevice或使用不同的SDK来编译旧版本的API 15左右。 - Taylor Courtney

0

我看到你的代码里有这一行。这就是你需要的。你只需要把它完成就可以了。

int i = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;

通常情况下,您需要检测从文件中读取的所有图片的方向。
请使用以下方法。
ExifInterface exif = new ExifInterface(outputFileUri.getEncodedPath());
String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

我的方向变量被设置为 0,即 ORIENTATION_UNDEFINED - codingViking
@codingViking 这意味着您的相机应用程序没有正确处理exif信息。那么我认为您自己无法完美地确定图像方向。也许您可以在应用程序中放置一个手动翻转按钮。 - songchenwen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接