1

当用户触摸它们时,我正在将一些资源转换为黑白。我在我的 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答案中获取了该功能。

怎么了 ?

谢谢

4

2 回答 2

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;
}

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

    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;
    }
于 2014-01-04T09:25:24.503 回答
2

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

每次要转换图像时创建一个位图并回收它。

来自安卓文档:

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

于 2014-01-04T08:36:56.593 回答