0

I'm displaying picture in the screen:

 Bitmap bitmap = decodeSampledBitmapFromResource(data, 100, 100);

decodeSampledBitmapFromResource() method looks like this:

 public static Bitmap decodeSampledBitmapFromResource(String path,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

After the changing the rotation of the picture on 90 degrees with the use of the Matrix, quality of the picture decreased:

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bitmap = decodeSampledBitmapFromResource(data, 100, 100);
bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

Why the quality of the displayed picture decreased? and how to make it in the normal quality?

Picture before the rotation: enter image description here

Picture after the rotation:

enter image description here

8
  • what is 'data' ? what does that 'decodeSampledBitmapFromResource'method do?
    – Stultuske
    Jul 22, 2016 at 12:31
  • @Stultuske, just have edited my question
    – Lucky_girl
    Jul 22, 2016 at 12:37
  • What are the dimensions of the image you started with? Do you have an example of the quality before and after?
    – Bryan
    Jul 22, 2016 at 12:40
  • Try using filter as false, no need to interpolate anything in your 90° rotation case. Jul 22, 2016 at 12:50
  • @Bryan just have added examples
    – Lucky_girl
    Jul 22, 2016 at 13:02

1 Answer 1

2

It looks like you are pulling your code almost directly from the documentation, which is great, but you need to understand what that code is doing. Take this code for example:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

I am guessing this is the same as your calculateInSampleSize() method. This method calculates the divisor that should be used to scale your image to best match the size given.

In your case, you are providing 100px for both the width and height. This means that if the starting image is say 680 x 600px, your inSampleSize will be 2, therefore the resulting image will be 340 x 300px, which is a loss in quality. This is helpful in reducing the image size if you do not need to display the full resolution, but in your case it looks like you do need to use the original image size to display it at the resolution you require.

This does not mean you should forgo calculating the inSampleSize, it just means that you should provide a required width and height that better matches your ImageView size.

int requiredWidth = imageView.getMeasuredWidth();
int requiredHeight = imageView.getMeasureHeight();

Note: this will not work if the ImageView width and/or height is set to wrap_content, as the width and height will not be measured until a source image is provided.

In your case, I would also swap the required width and height when you calculate the inSampleSize because you are rotating the image 90°:

options.inSampleSize = calculateInSampleSize(options, requiredHeight, requiredWidth);

Lastly, make sure your ImageView.ScaleType is not set to FIT_XY, as this will distort the image, resulting in a perceived loss in quality. Other scale types can also increase the size of the image beyond the original dimensions, which can also result in a perceived loss in quality.


Finally, if your application is displaying a lot of images, I would suggest using an image loading library to load the images for you. These libraries will handle scaling the image to fit the ImageView, and most support wrap_content as well. Glide, Picasso and Fresco are a few good examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.