Android студия decodeStream возвращает изображение в альбомной ориентации - PullRequest
0 голосов
/ 26 марта 2020

В студийном проекте android я получаю изображение из галереи, используя метод BitmapFactory.decodeStream. Но полученное растровое изображение всегда находится в альбомной ориентации, поэтому изображения, высота которых превышает ширину, поворачиваются на 90 градусов. Как решить проблему? Вот код геттера

public static Bitmap decodeSampledBitmapFromResource(Context context, Uri resourceUri, int reqWidth, int reqHeight) throws IOException {
    InputStream imageStream = context.getContentResolver().openInputStream(resourceUri);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(imageStream, null, options);

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

    imageStream.close();
    imageStream = context.getContentResolver().openInputStream(resourceUri);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    //the bitmap is always in landscape orientation
    return BitmapFactory.decodeStream(imageStream, null, options);
}

private static int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of the 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 += 1;
        }
    }

    return inSampleSize;
}
...