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

Correctly rotate and flip image using Exif attributes. #443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
- Added options to customize colors of toolbar back button, title and menu texts. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437)
### Fixed
- Correctly rotate and flip image using Exif attributes. [#443](https://github.com/CanHub/Android-Image-Cropper/issues/443)
- Fixed and issue where setting toolbar color to white would do nothing. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437)

## [4.3.2] - 08/09/2022
Expand Down
28 changes: 14 additions & 14 deletions cropper/src/main/java/com/canhub/cropper/BitmapUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,12 @@ internal object BitmapUtils {
* If no rotation is required the image will not be rotated.<br></br>
* New bitmap is created and the old one is recycled.
*/
fun orientateBitmapByExif(bitmap: Bitmap?, context: Context, uri: Uri?): RotateBitmapResult {
var ei: ExifInterface? = null
try {
val `is` = context.contentResolver.openInputStream(uri!!)
if (`is` != null) {
ei = ExifInterface(`is`)
`is`.close()
}
} catch (ignored: Exception) {
fun orientateBitmapByExif(bitmap: Bitmap?, context: Context, uri: Uri): RotateBitmapResult {
val exifInterface = context.contentResolver.openInputStream(uri)?.use { ExifInterface(it) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we close every single time just like in #440

return when {
exifInterface != null -> orientateBitmapByExif(bitmap, exifInterface)
else -> RotateBitmapResult(bitmap, 0)
}
return if (ei != null) orientateBitmapByExif(bitmap, ei) else RotateBitmapResult(bitmap, 0)
}

/**
Expand All @@ -95,7 +90,8 @@ internal object BitmapUtils {
fun orientateBitmapByExif(bitmap: Bitmap?, exif: ExifInterface): RotateBitmapResult {
val orientationAttributeInt =
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val degrees: Int = when (orientationAttributeInt) {

val degrees = when (orientationAttributeInt) {
ExifInterface.ORIENTATION_ROTATE_90, ExifInterface.ORIENTATION_TRANSVERSE,
ExifInterface.ORIENTATION_TRANSPOSE -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
Expand All @@ -106,7 +102,11 @@ internal object BitmapUtils {
orientationAttributeInt == ExifInterface.ORIENTATION_TRANSPOSE
val flipVertically = orientationAttributeInt == ExifInterface.ORIENTATION_FLIP_VERTICAL ||
orientationAttributeInt == ExifInterface.ORIENTATION_TRANSVERSE
return RotateBitmapResult(bitmap, degrees, flipHorizontally, flipVertically)

return when (bitmap) {
null -> RotateBitmapResult(null, degrees, flipHorizontally, flipVertically)
else -> RotateBitmapResult(rotateAndFlipBitmapInt(bitmap, degrees, flipHorizontally, flipVertically), 0, false, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling rotateAndFlipBitmapInt is the fix and once we've flipped and rotated we no longer need to rotate / flip hence 0, false, false

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style.

Line detected that is longer than the defined maximum line length in the code style.
}
}

/**
Expand Down Expand Up @@ -862,8 +862,8 @@ internal object BitmapUtils {
val matrix = Matrix()
matrix.setRotate(degrees.toFloat())
matrix.postScale(
(if (flipHorizontally) -1 else 1).toFloat(),
(if (flipVertically) -1 else 1).toFloat()
(if (flipHorizontally) -1f else 1f),
(if (flipVertically) -1f else 1f),
)
val newBitmap =
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, false)
Expand Down