Replace specific color in bitmap with transparency

2019-04-10 11:42发布

问题:

I have a method to replace a pixel of one color with transparency

public Bitmap createTransparentBitmapFromBitmap(Bitmap bitmap,
          int replaceThisColor) {
        if (bitmap != null) {
          int picw = bitmap.getWidth(); 
          int pich = bitmap.getHeight();
          int[] pix = new int[picw * pich];
          bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

          int sr = (replaceThisColor >> 16) & 0xff;
          int sg = (replaceThisColor >> 8) & 0xff;
          int sb = replaceThisColor & 0xff;

          for (int y = 0; y < pich; y++) {   
            for (int x = 0; x < picw; x++) {
              int index = y * picw + x;
            /*  int r = (pix[index] >> 16) & 0xff;
              int g = (pix[index] >> 8) & 0xff;
              int b = pix[index] & 0xff;*/

              if (pix[index] == replaceThisColor) {

                if(x<topLeftHole.x) topLeftHole.x = x;  
                if(y<topLeftHole.y) topLeftHole.y = y;
                if(x>bottomRightHole.x) bottomRightHole.x = x;
                if(y>bottomRightHole.y)bottomRightHole.y = y;

                pix[index] = Color.TRANSPARENT;   
              } else {
                //break;
              }
            }
          }

          Bitmap bm = Bitmap.createBitmap(pix, picw, pich,
              Bitmap.Config.ARGB_8888);  

          return bm;
        }
        return null;
      }

Its called like this

backgroundBitmap = createTransparentBitmapFromBitmap(backgroundBitmap , Color.argb(255,255,255, 0));

The same color i have in a png file, where i want the transparent hole.The problem is it only replaces part of the color not all. See screenshot https://docs.google.com/document/d/18aH43sFmsuuRu0QNfMTD1zek8sqWwH_pTauFofDZeIw/edit

回答1:

It looks like your picture has JPEG artifacts in it. Detecting an exact color will only really work if you only save your picture in a lossless format. PNG is a lossless format, but did you save an intermediate version of your picture as a JPEG (or in some other lossy format) after drawing the circle?

Also; just a tip: you don't need two loops, just loop through the array in one for loop.

Edit: This new yellow looks like it's been caused by anti-aliasing. Due to that, the edges of the circle aren't the exact color you're looking for and your code misses them. The only way around this is to turn off anti-aliasing while drawing the circle. Of course, this way you also won't get a nice anti-aliased edge for your transparent hole.

If you want that, you'll probably have to use a separate mask for the hole (a JPEG for color and an 8-bit PNG for transparency should be a quite efficient combination - oh how much I wish there was an image format that readily allowed this in web browsers)



回答2:

very good, I needed this, then beyond using I improved a little, the code:

public static Bitmap repleceIntervalColor(Bitmap bitmap,int redStart,int redEnd,int greenStart, int greenEnd,int blueStart, int blueEnd,int colorNew) {
    if (bitmap != null) {
        int picw = bitmap.getWidth();
        int pich = bitmap.getHeight();
        int[] pix = new int[picw * pich];
        bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
        for (int y = 0; y < pich; y++) {
            for (int x = 0; x < picw; x++) {
                int index = y * picw + x;
                    if (
                        ((Color.red(pix[index]) >= redStart)&&(Color.red(pix[index]) <= redEnd))&&
                        ((Color.green(pix[index]) >= greenStart)&&(Color.green(pix[index]) <= greenEnd))&&
                        ((Color.blue(pix[index]) >= blueStart)&&(Color.blue(pix[index]) <= blueEnd))
                    ){
                        pix[index] = colorNew;
                    }
                }
            }
        Bitmap bm = Bitmap.createBitmap(pix, picw, pich,Bitmap.Config.ARGB_8888);
        return bm;
    }
    return null;
}