mirror of
https://github.com/Steffo99/twom.git
synced 2024-11-22 08:04:26 +00:00
Fix image picker
This commit is contained in:
parent
19d01e2c40
commit
d262ae8cf7
1 changed files with 72 additions and 4 deletions
|
@ -1,6 +1,9 @@
|
||||||
package eu.steffo.twom.create
|
package eu.steffo.twom.create
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
|
import android.graphics.Matrix
|
||||||
|
import android.media.ExifInterface
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.PickVisualMediaRequest
|
import androidx.activity.result.PickVisualMediaRequest
|
||||||
|
@ -54,10 +57,75 @@ fun CreateActivityContent(
|
||||||
if (it == null) {
|
if (it == null) {
|
||||||
avatarBitmap = null
|
avatarBitmap = null
|
||||||
} else {
|
} else {
|
||||||
val stream = context.contentResolver.openInputStream(it)
|
// Open two streams...
|
||||||
if (stream != null) {
|
// One to read the EXIF metadata from:
|
||||||
avatarBitmap = BitmapFactory.decodeStream(stream).asImageBitmap()
|
val exifStream = context.contentResolver.openInputStream(it)
|
||||||
stream.close()
|
// One to read the image data itself from:
|
||||||
|
val bitmapStream = context.contentResolver.openInputStream(it)
|
||||||
|
|
||||||
|
if (exifStream != null && bitmapStream != null) {
|
||||||
|
// Use the EXIF metadata to determine the orientation of the image
|
||||||
|
val exifInterface = ExifInterface(exifStream)
|
||||||
|
val orientation =
|
||||||
|
exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1)
|
||||||
|
exifStream.close()
|
||||||
|
|
||||||
|
// Parse the image data as-is
|
||||||
|
val originalBitmap = BitmapFactory.decodeStream(bitmapStream)
|
||||||
|
bitmapStream.close()
|
||||||
|
|
||||||
|
// Determine the starting points and the size to crop the image to a 1:1 square
|
||||||
|
val xStart: Int
|
||||||
|
val yStart: Int
|
||||||
|
val size: Int
|
||||||
|
if (originalBitmap.width > originalBitmap.height) {
|
||||||
|
yStart = 0
|
||||||
|
xStart = (originalBitmap.width - originalBitmap.height) / 2
|
||||||
|
size = originalBitmap.height
|
||||||
|
} else {
|
||||||
|
xStart = 0
|
||||||
|
yStart = (originalBitmap.height - originalBitmap.width) / 2
|
||||||
|
size = originalBitmap.width
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a transformation matrix to rotate the bitmap based on the orientation
|
||||||
|
val transformationMatrix: Matrix = Matrix()
|
||||||
|
|
||||||
|
// TODO: Make sure these transformations are valid
|
||||||
|
when (orientation) {
|
||||||
|
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> transformationMatrix.postScale(
|
||||||
|
-1f,
|
||||||
|
1f
|
||||||
|
)
|
||||||
|
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_180 -> transformationMatrix.postRotate(180f)
|
||||||
|
ExifInterface.ORIENTATION_FLIP_VERTICAL -> transformationMatrix.postScale(
|
||||||
|
1f,
|
||||||
|
-1f
|
||||||
|
)
|
||||||
|
|
||||||
|
ExifInterface.ORIENTATION_TRANSPOSE -> {/* TODO: Transpose the image Matrix */
|
||||||
|
}
|
||||||
|
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_90 -> transformationMatrix.postRotate(90f)
|
||||||
|
ExifInterface.ORIENTATION_TRANSVERSE -> {/* TODO: Flip horizontally the image Matrix, then transpose it */
|
||||||
|
}
|
||||||
|
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_270 -> transformationMatrix.postRotate(270f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crop the bitmap
|
||||||
|
val croppedBitmap = Bitmap.createBitmap(
|
||||||
|
originalBitmap,
|
||||||
|
xStart,
|
||||||
|
yStart,
|
||||||
|
size,
|
||||||
|
size,
|
||||||
|
transformationMatrix,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
|
avatarBitmap = croppedBitmap.asImageBitmap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue