So it was due to the orientation. The solution which was grabbed from some other site..
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
if (imageUri == null) {
Log.d(TAG, "imageUri is null!")
return
}
Log.d(TAG, "$imageUri")
val bmp = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
//we need to rotate the image as cameras can be embedded in the hardware in different orientations. Make the bitmap upright before we attempt to process it..
val rotatedImg = rotateImageIfRequired(this, bmp, imageUri!!)
processImage(rotatedImg) //parse this into the ML vision
} else {
Log.d(TAG, "onActivityResult -> resultCode: $resultCode")
}
}
private fun rotateImageIfRequired(context: Context, img: Bitmap, selectedImage: Uri): Bitmap {
Log.d(TAG, "rotateImageIfRequired")
val input = context.getContentResolver().openInputStream(selectedImage);
var ei: ExifInterface? = null
if (Build.VERSION.SDK_INT > 23)
ei = ExifInterface(input);
else
ei = ExifInterface(selectedImage.getPath());
val orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> return rotateImage(img, 90f);
ExifInterface.ORIENTATION_ROTATE_180 -> return rotateImage(img, 180f);
ExifInterface.ORIENTATION_ROTATE_270 -> return rotateImage(img, 270f);
else -> return img;
}
}
private fun rotateImage(img: Bitmap, degree: Float): Bitmap {
Log.d(TAG, "rotateImage() degree: $degree")
var matrix = Matrix();
matrix.postRotate(degree);
val rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}