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

时间:2015-04-14 01:32:14

标签: android bitmap camera mediastore image-capture

我有一个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 个答案:

答案 0 :(得分:1)

请尝试以下代码:

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

答案 1 :(得分:0)

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

int i = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;

通常,您需要检测从文件中读取的所有图片的方向。

使用以下方法。

ExifInterface exif = new ExifInterface(outputFileUri.getEncodedPath());
String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
相关问题