Android,内存不足转换为黑白

时间:2014-01-04 08:32:15

标签: android performance android-bitmap

当用户触摸它时,我将一些资源转换为黑白。我有这个函数在我的Util java文件中执行此操作:

public static Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);

    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    Bitmap blackAndWhiteBitmap = orginalBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint();
    paint.setColorFilter(colorMatrixFilter);

    Canvas canvas = new Canvas(blackAndWhiteBitmap);
    canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

    return blackAndWhiteBitmap;
}

但有时候,在某些设备中我会收到此错误:

java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCopy(Native Method)
at android.graphics.Bitmap.copy(Bitmap.java:479)
at com.mygame.util.Util.convertColorIntoBlackAndWhiteImage(Util.java:145)

该功能的第145行是

Bitmap blackAndWhiteBitmap = orginalBitmap.copy(Bitmap.Config.ARGB_8888, true);

准确地说,我从其他stackoverflow回答中获取了该函数。

发生了什么事?

谢谢

2 个答案:

答案 0 :(得分:5)

这可能对你有帮助......

public static Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);

    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
            colorMatrix);
    Bitmap blackAndWhiteBitmap = orginalBitmap.copy(
            Bitmap.Config.ARGB_8888, true);
    // Recycle the Bitmap and free the memory once you copied into new
    // Bitmap
    orginalBitmap.recycle();
    orginalBitmap = null;
    // still problem exists call
    // System.gc();
    // System.gc();

    Paint paint = new Paint();
    paint.setColorFilter(colorMatrixFilter);

    Canvas canvas = new Canvas(blackAndWhiteBitmap);
    canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

    return blackAndWhiteBitmap;
}

似乎您正在尝试将图像转换为灰色。试试这个...

    public static Bitmap toGrayBitmap(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            return null;
        }
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();

        Bitmap canvasBitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(canvasBitmap);
        canvas.drawARGB(0, 0, 0, 0);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(
                colorMatrix);
        paint.setColorFilter(filter);

        canvas.drawBitmap(bitmap, 0, 0, paint);
        bitmap.recycle(); // do it if still problem exists

        return canvasBitmap;
    }

答案 1 :(得分:2)

在使用Bitmaps时,您应该关注内存消耗(特别是对于具有大屏幕和高分辨率的设备)。

创建位图并在每次要转换图像时回收。

来自android docs:

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

阅读本文:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html