62

我是这个网站的新手,我有一个关于 Android 的问题。

有没有办法将位图转换为灰度?我知道如何绘制灰度位图(使用画布操作: http: //www.mail-archive.com/android-developers@googlegroups.com/msg38890.html)但我真的需要灰色的实际位图(或至少可以稍后转换为位图的东西)。我必须手动实现它(逐个像素操作)吗?

我搜索了很多,仍然找不到。任何人都知道一种简单/有效的方法吗?

非常感谢!

4

5 回答 5

162

哦,是的,确实如此。我用错了,谢谢指点。(对不起这个无用的问题)这是结束代码(主要基于链接的那个),因为它可能对某人有所帮助:

public Bitmap toGrayscale(Bitmap bmpOriginal)
{        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

非常欢迎任何评论或评论。

谢谢

于 2010-08-02T19:24:05.603 回答
20

如果您要在ImageView. 然后,您可以尝试以下代码,而不是将位图转换为灰度:

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);

ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageview.setColorFilter(filter);

以供参考

于 2015-04-18T18:04:12.190 回答
16

这不正是您链接到的代码的作用吗?它需要一个彩色位图(“bmp”),创建一个重复的位图(“bm”),然后使用过滤器将彩色位图绘制成“bm”,将其转换为灰度。从那时起,您可以使用“bm”作为实际的灰度位图,并使用它做任何您想做的事情。

您需要稍微调整一下样本(它使用硬编码的大小,您可能只想克隆原始位图的大小),但除此之外,这似乎是随时可用的,取决于你想要什么。

于 2010-07-30T19:22:13.450 回答
14

我想提一下,这种方法必须考虑一个重要方面。Android 上的 BitMap 存储在 NativeHeap 中。通过仅仅“创建位图”,你最终会阻塞内存,得到一个OutOfMemoryException(OOM)。

因此,位图必须始终为.recycled().

于 2011-10-11T13:54:32.957 回答
3

这是一种更有效的方法,我已经使用它来支持所有版本的 Android:

    //    https://xjaphx.wordpress.com/2011/06/21/image-processing-grayscale-image-on-the-fly/
    @JvmStatic
    fun getGrayscaledBitmapFallback(src: Bitmap, redVal: Float = 0.299f, greenVal: Float = 0.587f, blueVal: Float = 0.114f): Bitmap {
        // create output bitmap
        val bmOut = Bitmap.createBitmap(src.width, src.height, src.config)
        // pixel information
        var A: Int
        var R: Int
        var G: Int
        var B: Int
        var pixel: Int
        // get image size
        val width = src.width
        val height = src.height
        // scan through every single pixel
        for (x in 0 until width) {
            for (y in 0 until height) {
                // get one pixel color
                pixel = src.getPixel(x, y)
                // retrieve color of all channels
                A = Color.alpha(pixel)
                R = Color.red(pixel)
                G = Color.green(pixel)
                B = Color.blue(pixel)
                // take conversion up to one single value
                B = (redVal * R + greenVal * G + blueVal * B).toInt()
                G = B
                R = G
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B))
            }
        }
        // return final image
        return bmOut
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @JvmStatic
    fun getGrayscaledBitmap(context: Context, src: Bitmap): Bitmap {
//        https://gist.github.com/imminent/cf4ab750104aa286fa08
//        https://en.wikipedia.org/wiki/Grayscale
        val redVal = 0.299f
        val greenVal = 0.587f
        val blueVal = 0.114f
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
            return getGrayscaledBitmapFallback(src, redVal, greenVal, blueVal)
        val render = RenderScript.create(context)
        val matrix = Matrix4f(floatArrayOf(-redVal, -redVal, -redVal, 1.0f, -greenVal, -greenVal, -greenVal, 1.0f, -blueVal, -blueVal, -blueVal, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f))
        val result = src.copy(src.config, true)
        val input = Allocation.createFromBitmap(render, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT)
        val output = Allocation.createTyped(render, input.type)
        // Inverts and do grayscale to the image
        @Suppress("DEPRECATION")
        val inverter =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
                    ScriptIntrinsicColorMatrix.create(render)
                else
                    ScriptIntrinsicColorMatrix.create(render, Element.U8_4(render))
        inverter.setColorMatrix(matrix)
        inverter.forEach(input, output)
        output.copyTo(result)
        src.recycle()
        render.destroy()
        return result
    }
于 2019-04-20T20:31:35.860 回答