Detect if image was taken with front camera

末鹿安然 提交于 2019-12-07 18:00:32

问题


I have an Android app that allows a user to upload a profile picture using their camera. The problem is, when the user takes a photo with the front facing camera, the image stored on the phone is mirrored.

I am able to mirror the image back to it's original state however, I am unable to perform the flip on front facing camera pictures exclusively.

Is there a way to figure out if the picture was taken with the front facing camera?

Here is some code I use for getting the picture

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);

Any help would be greatly appreciated, thank you.


回答1:


Try the following code:

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



回答2:


I saw you have this line in your code. This is what you need. You just need to complete it.

int i = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;

Generally you need to detect the orientation of all the pictures you read from file.

Use this method below.

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


来源:https://stackoverflow.com/questions/29617735/detect-if-image-was-taken-with-front-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!