ebeninki
ebeninki

Reputation: 989

Loading Bitmap into ImageView lets rotate the image shown

I have the following code which creates a Bitmap from a given image image file path:

private fun loadBitmap(filePath: String?): Bitmap {

        val options = BitmapFactory.Options()
        options.inPreferredConfig = Bitmap.Config.ARGB_8888
        return BitmapFactory.decodeFile(filePath, options)
}

It is called in the onCreateView() method of my fragment class:

bitmapOfImage = loadBitmap(args.getString(FILE_PATH))

The Bitmap bitmapOfImage is modified in a certain way with RenderScript and then it is loaded into an ImageView:

// do some modification on the bitmap
// ...

// copy modified pixels to 'bitmapIn'
allocationOut.copyTo(bitmapIn)

// load it into the imageview specified by its ID 'modifiedImage'
binding.modifiedImage.setImageBitmap(bitmapIn)

So, I take the image with CameraX and the file path is passed along to loadBitmap() method of the image processing fragment mentioned above. But although I take the image in a portrait mode it is shown in a landscape version.

Here a little image how it looks like when I take the image:

enter image description here

But this is how it looks like after the bitmap is loaded into imageview: enter image description here

As you can see the image seems to be rotated to the right. Why ? Is this happening by default ? How can I fix that ? For the sake of completeness, here is my XML layout used to show the captured photo:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/imagecontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ImageModificationFragment">

        <ImageView
            android:id="@+id/modified_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="@string/shows_filtered_image" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Upvotes: 2

Views: 501

Answers (2)

Sam
Sam

Reputation: 1

Setting the image by uri works for me:

imageView.setImageURI(uri)

Upvotes: 0

YMY
YMY

Reputation: 698

To achieve the desired orientation, check orientation with ExifInterface and rotate the bitmap if necessary.

Example code below:

    Matrix matrix = new Matrix();
    Bitmap bitmap = 'your bitmap';
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageBytes = stream.toByteArray();
    ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(imageBytes));
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(),
                    matrix, true);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(),
                    matrix, true);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(),
                    matrix, true);
            break;
        default:
            break;
    }

Upvotes: 2

Related Questions