改变位图的颜色

change color of bitmap

我正在尝试创建一个函数来获取位图和命运颜色以及 returns 彩色位图(不使用绘画)。我发现很少有方法可以做到这一点,但没有什么能像我想要的那样工作。

我能找到的最接近的解决方案是:

    public static Bitmap changeImageColor(Bitmap srcBmp, int dstColor) {

        int width = srcBmp.getWidth();
        int height = srcBmp.getHeight();

        float srcHSV[] = new float[3];
        float dstHSV[] = new float[3];

        Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                Color.colorToHSV(srcBmp.getPixel(col, row), srcHSV);
                Color.colorToHSV(dstColor, dstHSV);

                // If it area to be painted set only value of original image
                dstHSV[2] = srcHSV[2];  // value
                int color2=Color.HSVToColor(dstHSV);;
                dstBitmap.setPixel(col, row, Color.HSVToColor(dstHSV));
            }
        }

        return dstBitmap;
    }

但它在透明图像上效果不佳,如下所示(之前和之后):

有人有任何其他解决方案(同样完全不使用油漆)吗?

这是更改位图颜色的示例代码:

private BitmapDrawable getColoredBitmap(int color, Context context,
            int drawableId) {
        Bitmap source = BitmapFactory.decodeResource(context.getResources(),
                drawableId);
        final Bitmap bitmap = Bitmap.createBitmap(source.getWidth(),
                source.getHeight(), Bitmap.Config.ARGB_8888);
        for (int i = 0; i < source.getWidth(); i++) {
            for (int j = 0; j < source.getHeight(); j++) {
                int pixel = source.getPixel(i, j);

                // if (pixel == Color.TRANSPARENT) {
                //
                // } else
                if (pixel == Color.WHITE) {
                    pixel = Color.argb(Color.alpha(pixel),
                            Color.red(Color.WHITE), Color.green(Color.WHITE),
                            Color.blue(Color.WHITE));
                } else {
                    pixel = Color.argb(Color.alpha(pixel), Color.red(color),
                            Color.green(color), Color.blue(color));
                }
                bitmap.setPixel(i, j, pixel);
            }
        }
        return new BitmapDrawable(context.getResources(), bitmap);
    }

你这样做:

int alpha=srcBmp.getPixel(col, row);
dstBitmap.setPixel(col, row, Color.HSVToColor(dstHSV));

您在其中计算了一个 alpha(从该代码的外观来看可能不正确),然后不使用它。您可能必须使用 HSVToColor 创建颜色,然后设置该颜色的 alpha,然后在 setPixel 中使用它。而且您可能必须以类似的方式获得 alpha,因为我发现很难相信 getPixel 函数仅 returns alpha :p

您只需要提取 alpha 并在转换后重新应用它。并使用 ARGB_8888;

编辑您的代码以包含 alpha:

public Bitmap colorize(Bitmap srcBmp, int dstColor) {

        int width = srcBmp.getWidth();
        int height = srcBmp.getHeight();

        float srcHSV[] = new float[3];
        float dstHSV[] = new float[3];

        Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                int pixel = srcBmp.getPixel(col, row);
                int alpha = Color.alpha(pixel);
                Color.colorToHSV(pixel, srcHSV);
                Color.colorToHSV(dstColor, dstHSV);

                // If it area to be painted set only value of original image
                dstHSV[2] = srcHSV[2];  // value
                dstBitmap.setPixel(col, row, Color.HSVToColor(alpha, dstHSV));
            }
        }

        return dstBitmap;
}