Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(camera): Reset exif orientation if corrected #510

Merged
merged 1 commit into from Jul 6, 2021

Conversation

@jcesarmobile
Copy link
Member

@jcesarmobile jcesarmobile commented Jul 2, 2021

If getPhoto is called with correctOrientation: true and the photo gets the orientation corrected, reset the exif orientation too.

closes #503

@Ionitron Ionitron added this to Needs review 🤔 in Capacitor Engineering ⚡️ Jul 2, 2021
@jcesarmobile jcesarmobile requested a review from carlpoole Jul 2, 2021
@jcesarmobile jcesarmobile merged commit a65c05e into main Jul 6, 2021
6 checks passed
Capacitor Engineering ⚡️ automation moved this from Needs review 🤔 to Done 🎉 Jul 6, 2021
@jcesarmobile jcesarmobile deleted the reset-orientation-exif branch Jul 6, 2021
@dragermrb
Copy link

@dragermrb dragermrb commented Jul 12, 2021

Hi!

This fix will fail on some Xiaomis devices (maybe others). String imageUri.getPath() can't be opened by ExifInterface

// imageUri.toString(): content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F35142/ORIGINAL/NONE/image%2Fjpeg/1770703045
// imageUri.getPath(): /0/1/content://media/external/images/media/35142/ORIGINAL/NONE/image/jpeg/1770703045

E/Capacitor/CameraPlugin: Unable to process image
    java.io.FileNotFoundException: /0/1/content:/media/external/images/media/35142/ORIGINAL/NONE/image/jpeg/1770703045: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileInputStream.<init>(FileInputStream.java:159)
        at java.io.FileInputStream.<init>(FileInputStream.java:115)
        at androidx.exifinterface.media.ExifInterface.initForFilename(ExifInterface.java:5283)
        at androidx.exifinterface.media.ExifInterface.<init>(ExifInterface.java:3946)
        at com.capacitorjs.plugins.camera.ImageUtils.correctOrientation(ImageUtils.java:81)
        at com.capacitorjs.plugins.camera.CameraPlugin.prepareBitmap(CameraPlugin.java:448)
        at com.capacitorjs.plugins.camera.CameraPlugin.returnResult(CameraPlugin.java:358)
        at com.capacitorjs.plugins.camera.CameraPlugin.processPickedImage(CameraPlugin.java:300)

You could try to use and inputStream (like on method getOrientation()) but then you can't save the file

public static Bitmap correctOrientation(...) {
    // ...
                InputStream iStream = c.getContentResolver().openInputStream(imageUri);
                ExifInterface exif = new ExifInterface(iStream);
}
E/Capacitor/CameraPlugin: Unable to process image
    java.io.IOException: Failed to save new file. Original file is stored in /data/user/0/com.myapp.ip/cache/temp3462730607062375942tmp
        at androidx.exifinterface.media.ExifInterface.saveAttributes(ExifInterface.java:4783)
        at com.capacitorjs.plugins.camera.ImageUtils.correctOrientation(ImageUtils.java:87)
        at com.capacitorjs.plugins.camera.CameraPlugin.prepareBitmap(CameraPlugin.java:445)
        at com.capacitorjs.plugins.camera.CameraPlugin.returnResult(CameraPlugin.java:355)
        at com.capacitorjs.plugins.camera.CameraPlugin.processPickedImage(CameraPlugin.java:297)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.getcapacitor.Plugin.triggerActivityCallback(Plugin.java:154)

@WIStudent
Copy link

@WIStudent WIStudent commented Jul 19, 2021

I am getting the same error on my Pixel 3a when selecting a portrait image from the gallery.

E/Capacitor/CameraPlugin: Unable to process image
    java.io.FileNotFoundException: /1/1/content:/media/external/images/media/13171/ORIGINAL/NONE/image/jpeg/356575476: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:492)
        at java.io.FileInputStream.<init>(FileInputStream.java:160)
        at java.io.FileInputStream.<init>(FileInputStream.java:115)
        at androidx.exifinterface.media.ExifInterface.initForFilename(ExifInterface.java:5283)
        at androidx.exifinterface.media.ExifInterface.<init>(ExifInterface.java:3946)
        at com.capacitorjs.plugins.camera.ImageUtils.correctOrientation(ImageUtils.java:81)
        at com.capacitorjs.plugins.camera.CameraPlugin.prepareBitmap(CameraPlugin.java:438)
        at com.capacitorjs.plugins.camera.CameraPlugin.returnResult(CameraPlugin.java:351)
        at com.capacitorjs.plugins.camera.CameraPlugin.processPickedImage(CameraPlugin.java:295)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.getcapacitor.Plugin.triggerActivityCallback(Plugin.java:154)
        at com.getcapacitor.Plugin.lambda$initializeActivityLaunchers$0$Plugin(Plugin.java:116)

(btw it would probably the best if

} catch (IOException e) {
call.reject(UNABLE_TO_PROCESS_IMAGE);
return;
}
would log the exception also to logcat)

It happens because imageUri contains a content provider URI and not a regular file uri. Also, even if it would work, rewriting the exif data on the picture from the gallery is probably not a good idea because then the image in the gallery would contain the wrong exif data.

@danbunkr
Copy link

@danbunkr danbunkr commented Jul 19, 2021

@WIStudent : I was also getting the same error on a Samsung Galaxy S10 when trying to upload a photo from the gallery. Downgrading the plugin to "@capacitor/camera": "1.0.2", seems to resolve the issue for Android. Going to test IOS later this afternoon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Linked issues

Successfully merging this pull request may close these issues.

5 participants