This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Image_Picker] fixed image orientation problem on Android #1878
Closed
juliocbcotta
wants to merge
3
commits into
flutter:master
from
juliocbcotta:plugin/image_picker_image_rotation
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
...ges/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ExifDataCopier.java
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ExifHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package io.flutter.plugins.imagepicker; | ||
|
|
||
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import android.graphics.Matrix; | ||
| import androidx.exifinterface.media.ExifInterface; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class ExifHandler { | ||
|
|
||
| public ExifInterface copyExif(final String filePathOri, String filePathDest) throws IOException { | ||
|
|
||
| ExifInterface newExif = new ExifInterface(filePathDest); | ||
| final ExifInterface originalExif = new ExifInterface(filePathOri); | ||
| List<String> attributes = | ||
| Arrays.asList( | ||
| "FNumber", | ||
| "ExposureTime", | ||
| "ISOSpeedRatings", | ||
| "GPSAltitude", | ||
| "GPSAltitudeRef", | ||
| "FocalLength", | ||
| "GPSDateStamp", | ||
| "WhiteBalance", | ||
| "GPSProcessingMethod", | ||
| "GPSTimeStamp", | ||
| "DateTime", | ||
| "Flash", | ||
| "GPSLatitude", | ||
| "GPSLatitudeRef", | ||
| "GPSLongitude", | ||
| "GPSLongitudeRef", | ||
| "Make", | ||
| "Model", | ||
| "Orientation"); | ||
|
|
||
| for (String attribute : attributes) { | ||
| setIfNotNull(originalExif, newExif, attribute); | ||
| } | ||
|
|
||
| newExif.saveAttributes(); | ||
|
|
||
| return newExif; | ||
| } | ||
|
|
||
| private static void setIfNotNull(ExifInterface oldExif, ExifInterface newExif, String property) { | ||
| if (oldExif.getAttribute(property) != null) { | ||
| newExif.setAttribute(property, oldExif.getAttribute(property)); | ||
| } | ||
| } | ||
|
|
||
| // from : https://github.com/google/cameraview/issues/22#issuecomment-363047917 | ||
| public Bitmap getNormalOrientationBitmap(String imageAbsolutePath) | ||
| throws IOException { | ||
| Bitmap bitmap = BitmapFactory.decodeFile(imageAbsolutePath); | ||
| ExifInterface ei = new ExifInterface(imageAbsolutePath); | ||
|
|
||
| int orientation = | ||
| ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); | ||
|
|
||
| Matrix matrix = new Matrix(); | ||
| switch (orientation) { | ||
| case ExifInterface.ORIENTATION_NORMAL: | ||
| return bitmap; | ||
| case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | ||
| matrix.setScale(-1, 1); | ||
| break; | ||
| case ExifInterface.ORIENTATION_ROTATE_180: | ||
| matrix.setRotate(180); | ||
| break; | ||
| case ExifInterface.ORIENTATION_FLIP_VERTICAL: | ||
| matrix.setRotate(180); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it just be |
||
| matrix.postScale(-1, 1); | ||
| break; | ||
| case ExifInterface.ORIENTATION_TRANSPOSE: | ||
| matrix.setRotate(90); | ||
| matrix.postScale(-1, 1); | ||
| break; | ||
| case ExifInterface.ORIENTATION_ROTATE_90: | ||
| matrix.setRotate(90); | ||
| break; | ||
| case ExifInterface.ORIENTATION_TRANSVERSE: | ||
| matrix.setRotate(-90); | ||
| matrix.postScale(-1, 1); | ||
| break; | ||
| case ExifInterface.ORIENTATION_ROTATE_270: | ||
| matrix.setRotate(-90); | ||
| break; | ||
| } | ||
|
|
||
| return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); | ||
| } | ||
|
|
||
| public void setNormalOrientation(ExifInterface exif) throws IOException { | ||
| exif.setAttribute(ExifInterface.TAG_ORIENTATION, | ||
| String.valueOf(ExifInterface.ORIENTATION_NORMAL)); | ||
| exif.saveAttributes(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep the file name the same for this PR? Renaming the file keeps the diff hard to review in Github.