在Bitmap更换颜色 [英] Replacing a color in a Bitmap

查看:212
本文介绍了在Bitmap更换颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在我的应用程序显示图像。他们从网上下载。这些图像是对象上近似为矩形,白色背景图片。我想这样的背景为白色(#FFFFFF)。我想,如果我看像素0,0(这应该永远是灰白色),我可以得到的颜色值,并替换每一个像素具有白色该值在影像中。

I have images which I display in my app. They are downloaded from the web. These images are pictures of objects on an almost-white background. I want this background to be white (#FFFFFF). I figure, if I look at pixel 0,0 (which should always be off-white), I can get the color value and replace every pixel in the image having that value with white.

这个问题已经被问过,答案似乎是这样的:

This question has been asked before and the answer seems to be this:

int intOldColor = bmpOldBitmap.getPixel(0,0);

Bitmap bmpNewBitmap = Bitmap.createBitmap(bmpOldBitmap.getWidth(), bmpOldBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpNewBitmap);
Paint paint = new Paint();

ColorFilter filter = new LightingColorFilter(intOldColor, Color.WHITE);
paint.setColorFilter(filter);
c.drawBitmap(bmpOriginal, 0, 0, paint);

然而,这是行不通的。

However, this isn't working.

运行此code后,在全部图片好像是我想删除的颜色​​。如,整个图像是1纯色吧。

After running this code, the entire image seems to be the color I was wanting to remove. As in, the entire image is 1 solid color now.

我还希望通过在整个图像中的每个像素不必环

I was also hoping to not have to loop through every pixel in the entire image.

任何想法?

推荐答案

下面是我创建的,可以更换一个特定的颜色为你想要的一种方法。请注意,所有的像素将得到scaned的位图,只有那些都是平等的将被替换为你想要的。

Here is a method I created for you to replace a specific color for the one you want. Note that all the pixels will get scaned on the Bitmap and only the ones that are equal will be replaced for the one you want.

     private Bitmap changeColor(Bitmap src, int colorToReplace, int colorThatWillReplace) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] pixels = new int[width * height];
        // get pixel array from source
        src.getPixels(pixels, 0, width, 0, 0, width, height);

        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());

        int A, R, G, B;
        int pixel;

         // iteration through pixels
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                // get current index in 2D-matrix
                int index = y * width + x;
                pixel = pixels[index];
                if(pixel == colorToReplace){
                    //change A-RGB individually
                    A = Color.alpha(pixel);
                    R = Color.red(pixel);
                    G = Color.green(pixel);
                    B = Color.blue(pixel);
                    pixels[index] = Color.argb(A,R,G,B); 
                    /*or change the whole color
                    pixels[index] = colorThatWillReplace;*/
                }
            }
        }
        bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
        return bmOut;
    }

我希望帮助:)

I hope that helped :)

这篇关于在Bitmap更换颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆