简体   繁体   中英

Detect if image was taken with front camera

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.

Try the following code:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM