4

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.

codingViking
  • 171
  • 3
  • 8

2 Answers2

1

Try the following code:

CameraInfo cameraInfo = new CameraInfo();
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
         // do your logic
}
samgak
  • 23,944
  • 4
  • 60
  • 82
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
  • I tried with this, `Camera.CameraInfo cameraInfo = new Camera.CameraInfo();` `if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) ` and it did not enter the if statement and stated that Camera is deprecated – codingViking Apr 14 '15 at 01:52
  • How many cameras are there? Sometimes it will be camera facing back if there is just one. – Taylor Courtney Apr 14 '15 at 02:48
  • @TaylorCourtney on the device I use to test (Nexus 5 with Android 5.1) there are 2 cameras, all my tests have been with a picture taken with the front facing camera. – codingViking Apr 14 '15 at 02:55
  • Ok, You can use CameraDevice or use a different sdk to compile with an older one like api 15 or so. – Taylor Courtney Apr 14 '15 at 03:04
0

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);
songchenwen
  • 1,362
  • 9
  • 14
  • My orientation variable gets set to `0` which is `ORIENTATION_UNDEFINED` – codingViking Apr 14 '15 at 01:59
  • @codingViking This means your camera app didn't handle the exif info right. Then I don't think there's a perfect way you can tell the image orientation by yourself. Maybe you can put a manual flip button on your app. – songchenwen Apr 14 '15 at 02:12