如何做一个大尺寸的位图图像处理没有崩溃 [英] How to do image processing on a big size bitmap without crashing

查看:259
本文介绍了如何做一个大尺寸的位图图像处理没有崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载一个大尺寸(约10 MB)的图像,让用户添加一些效果(如灰度等)。所以,我用装 inSampleSize 位图来显示preVIEW用户:

I want to load a big size (about 10 MB) image and let user add some effects (such as Grayscale and so on). So, I loaded the bitmap using inSampleSize to show a preview to user:

public Bitmap load(Context context, int[] holderDimention, String image_url) throws Exception {
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image_url, bitmapOptions);

    int inSampleSize = 1;

    final int outWidth = bitmapOptions.outWidth;
    final int outHeight = bitmapOptions.outHeight;

    final int holderWidth = holderDimention[0];
    final int holderHeight = holderDimention[1];

    // Calculation inSampleSize
    if (outHeight > holderHeight || outWidth > holderWidth) {
        final int halfWidth = outWidth / 2;
        final int halfHeight = outHeight / 2;

        while ((halfHeight / inSampleSize) > holderHeight && (halfWidth / inSampleSize) > holderWidth) {
            inSampleSize *= 2;
        }
    }

    bitmapOptions.inSampleSize = inSampleSize;

    // Decoding bitmap
    bitmapOptions.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(image_url, bitmapOptions);
}

最后,在背景我想不降采样(它是必要的)施加在原始图像上的影响。所以我用下面的code的子类中,的AsyncTask

public Bitmap load(Context context, String image_url) throws IOException {
    return BitmapFactory.decodeFile(image_url);
}

但我得到一个内存不足的异常。我怎么能解决这个问题?

But I got an out of memory exception. How could I handle this problem?

推荐答案

您需要部分地做到这一点。仅加载图像,过程的一部分,保存部分。请查看 Bitma pregionDe codeR

You need to do it partially. Load only part of the image, process, save part. Check out BitmapRegionDecoder.

相关问题:没有加载到内存中

Related question: Crop image without loading into memory

这篇关于如何做一个大尺寸的位图图像处理没有崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆