문제

I am having a strange behaviour when trying to decode a photo of the size 2448x2448 pixels. In code, I am calculating that a inSampleSize of 6 should be applied (based on the required size of the resulting bitmap) and when I call BitmapFactory.decodeStream with those options I am expecting a bitmap like this:

  • full_photo_width = 2448
  • full_photo_height = 2448
  • inSampleSize = 6
  • expected_width = (2448 / 6) = 408
  • expected_height (2448 / 6) = 408
  • actual_width = 612
  • actual_height = 612

Here is the code:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        int photo_width = options.outWidth;
        int photo_height = options.outHeight;
        float rotation = rotationForImage(this, uri);
        if (rotation != 0f) {
            // Assume the photo is portrait oriented
            matrix.preRotate(rotation);
            float photo_ratio = (float) ((float)photo_width / (float)photo_height);
            frame_height = (int) (frame_width / photo_ratio);

        } else {
            // Assume the photo is landscape oriented
            float photo_ratio = (float) ((float)photo_height / (float)photo_width);
            frame_height = (int) (frame_width * photo_ratio);

        }
        int sampleSize = calculateInSampleSize(options, frame_width, frame_height);
        if ((sampleSize % 2) != 0) {
            sampleSize++;
        }
        options.inSampleSize = sampleSize;
        options.inJustDecodeBounds = false;

        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And the calculateInSampleSize function:

    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) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

        // We round the value to the highest, always.
        if ((height / inSampleSize) > reqHeight || (width / inSampleSize > reqWidth)) {
            inSampleSize++;
        }

    }

    return inSampleSize;
}

The code works for all the photos for all the photos and decodeStream is returning a bitmap with the correct size (depending on the calculated inSampleSize) in all the cases, except with a particular photo. Am I missing something here? Thanks!

도움이 되었습니까?

해결책

Please refer to official API documentation: inSampleSize.

Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top