This file contains 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
private fun rotateImage(img: Bitmap, degree: Float): Bitmap { | |
val matrix = Matrix() | |
matrix.postRotate(degree) | |
return Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true) | |
} | |
private fun flip(bitmap: Bitmap, horizontal: Boolean): Bitmap { | |
val matrix = Matrix() | |
matrix.preScale((if (horizontal) -1f else 1f), (if (!horizontal) -1f else 1f)) | |
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) |
This file contains 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
private fun rotateImageIfRequired(img: Bitmap, selectedImage: Uri): Bitmap? { | |
selectedImage.path?.let { | |
val orientation = ExifInterface(it).getAttributeInt( | |
ExifInterface.TAG_ORIENTATION, | |
ExifInterface.ORIENTATION_NORMAL) | |
return when (orientation) { | |
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(img, 90f) | |
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(img, 180f) | |
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(img, 270f) | |
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> flip(img, true) |
This file contains 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
fun handleSamplingAndRotationBitmap(context: Context, selectedImage: Uri): Bitmap? { | |
val imageStream = context.contentResolver.openInputStream(selectedImage) | |
val image = BitmapFactory.decodeStream(imageStream, null, BitmapFactory.Options()) | |
imageStream?.close() | |
return if(image != null){ | |
rotateImageIfRequired(image, selectedImage) | |
} else { | |
image | |
} | |
} |
This file contains 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
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
val imageBitmap: Bitmap? | |
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { | |
imageBitmap = handleSamplingAndRotationBitmap(this, Uri.fromFile(File(currentPhotoPath))) | |
imageBitmap?.let { | |
imageView.setImageBitmap(it) | |
} | |
} | |
} |
This file contains 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
private fun createImageFile(filename: String): File? { | |
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES) | |
return File.createTempFile( | |
filename, /* name */ | |
null, /* suffix can be null */ | |
storageDir /* directory */ | |
).apply { | |
// Save a file: path for use with ACTION_VIEW intents | |
currentPhotoPath = absolutePath | |
} |
This file contains 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
private fun takeAPicture() { | |
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent -> | |
takePictureIntent.resolveActivity(packageManager)?.also { | |
val photoFile: File? = try { | |
createImageFile(FILENAME) | |
} catch (ex: IOException) { | |
Log.e(TAG, ex.message) | |
null | |
} | |
photoFile?.also { |