android - 如何对位图图像应用灰度效果?

标签 android c++ bitmap android-ndk grayscale

我想将位图图像转换为灰度图像。为此,我正在使用 NDK 来提升应用程序性能。我已成功应用其他效果。

问题::

我用来应用灰度的代码取自 C##。所以,我想将它转换成 NDK。我无法完成那部分..

作为引用,我正在放置我如何将青色效果应用于图像的代码。

下面粘贴的代码工作正常。

void applyCyano(Bitmap* bitmap) {
    //Cache to local variables
    unsigned char* red = (*bitmap).red;
    unsigned char* green = (*bitmap).green;
    unsigned char* blue = (*bitmap).blue;

    unsigned int length = (*bitmap).width * (*bitmap).height;
    register unsigned int i;
    register unsigned char grey, r, g, b;
    for (i = length; i--;) {
        grey = ((red[i] * 0.222f) + (green[i] * 0.222f) + (blue[i] * 0.222f));
        r = componentCeiling(61.0f + grey);
        g = componentCeiling(87.0f + grey);
        b = componentCeiling(136.0f + grey);

        grey = blackAndWhite(red[i], green[i], blue[i]);
        red[i] = overlayPixelComponents(grey, r, 0.9f);
        green[i] = overlayPixelComponents(grey, g, 0.9f);
        blue[i] = overlayPixelComponents(grey, b, 0.9f);
    }
}

应用灰度效果的代码(取自网上的C##例子)::

void applyGrayscaleNatively(Bitmap* original)
{
    //create an empty bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

    //lock the original bitmap in memory
    BitmapData originalData = original.LockBits(
            new Rectangle(0, 0, original.Width, original.Height),
            ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

    //lock the new bitmap in memory
    BitmapData newData = newBitmap.LockBits(
            new Rectangle(0, 0, original.Width, original.Height),
            ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

    //set the number of bytes per pixel
    int pixelSize = 3;

    for (int y = 0; y < original.Height; y++)
    {
        //get the data from the original image
        byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

        //get the data from the new image
        byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

        for (int x = 0; x < original.Width; x++)
        {
            //create the grayscale version
            byte grayScale =
                    (byte)((oRow[x * pixelSize] * .11) + //B
                            (oRow[x * pixelSize + 1] * .59) +  //G
                            (oRow[x * pixelSize + 2] * .3)); //R

            //set the new image's pixel to the grayscale version
            nRow[x * pixelSize] = grayScale; //B
            nRow[x * pixelSize + 1] = grayScale; //G
            nRow[x * pixelSize + 2] = grayScale; //R
        }
    }

    //unlock the bitmaps
    newBitmap.UnlockBits(newData);
    original.UnlockBits(originalData);
}

我想做什么::

我从 here 中获取了这个项目为图像设置了不同的效果,但没有为灰度设置。那么如何将灰度应用于代码,以便项目的所有其他功能不会停止。

如果您需要我的帮助,请告诉我。

提前致谢..

请帮助我解决这个问题,因为我无法在我的项目中取得进一步进展。

最佳答案

使用以下函数将位图转换为 Android 中的等效灰度图,而不是转换 C# 版本

public Bitmap toGrayscale(Bitmap bmpOriginal){        

int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();    

Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
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;
}

关于android - 如何对位图图像应用灰度效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23053933/

相关文章:

java - 由 Bitnami 提供支持的 Android 版 Parse(好友列表)

c++ - 关于在C++中将数据从字符串转换为 float 的查询

android - 无法在 Android 4.2 的 appwidget 中将位图写入 parcel blob

c++ - 如何对 Boost Spirit Parser 进行基准测试?

c++ - 在 Linux 上使用 poco 卸载库失败

vb.net - 在 VB.Net 中从头开始创建位图图像时,质量很差?

android - Bitmap.compress 不会减少字节数

android - Fragment addToBackStack 关闭 App

java - 如何在 android intent 中传递 arraylist 对象的 arraylist?

java - 可以将一半的文本以不同的颜色绘制到 Canvas 上吗?