如何从输入流旋转图像而不将其加载到内存中?

时间:2014-12-31 14:09:22

标签: android image image-processing bitmap rotation

我仔细阅读了这些主题,但仍然没有找到以下问题的答案。

Android rotate bitmap without making a copy

Android: rotate image without loading it to memory

Rotating images on android. Is there a better way?

JNI bitmap operations , for helping to avoid OOM when using large images


我有Uri代表一个大图像。我需要将此图像的旋转副本保存到SD卡。但是当我做的时候

BitmapFactory.decodeStream(contentResolver.openInputStream(uri));

我得到一个OutOfMemoryError。

我想做的就是这样的事情

    ContentResolver resolver = getContentResolver();
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    File imageFile = null;
    try {
        inputStream = resolver.openInputStream(uri);


        //I don't know how to apply this matrix to the bytes of InputStream
        Matrix matrix = new Matrix();
        matrix.postRotate(90);

        imageFile = createImageFile();

        outputStream = new FileOutputStream(imageFile);
        byte[] buffer = new byte[1024];
        while (inputStream.read(buffer) != -1) {

            //I'd like to apply the 'matrix' to 'buffer' here somehow but I can't
            //since 'buffer' contains the bytes of the image, not the pixel values of the bitmap
            outputStream.write(buffer);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Util.closeQuietly(inputStream);
        Util.closeQuietly(outputStream);
    }

对此有什么解决方案?

1 个答案:

答案 0 :(得分:0)

如果您可以接受质量较低的图像,可以使用BitmapFactory.Options读取它 通常,在使用这些选项时,您无法直观地区分

 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inSampleSize = 2; //Scale it down
 options.inPreferredConfig = Bitmap.Config.RBG_565; // Less memory intensive color format
 Bitmap bitmap = BitmapFactory.decodeStream( fis, null, options );

希望这有帮助。

我没有看到如何在不首先阅读它的情况下操纵它。