本文介绍了位图与透明替换特定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法来代替一种颜色与透明度像素

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));

同色我有一个PNG文件,在这里我要透明hole.The问题是,只替换颜色的一部分不是全部。见截图的

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

推荐答案

它看起来像你的图片有JPEG文物吧。检测精确的颜色只有真正的工作,如果你只是保存你的照片无损格式。 PNG是一种无损格式,但你画的圈子后,您的图片的中间版本保存为JPEG(或其他一些有损格式)?

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?

此外,只是一个提示:你不通过一个for循环阵列需要两个循环,只是环

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

编辑:这种新的黄色看起来像它已经引起抗锯齿。由于是,圆的边缘是不是你要找的精确的颜色和你的code想念他们。解决这个问题的唯一方法是关闭抗锯齿,同时绘制圆。当然,这样一来你也不会得到一个很好的消除锯齿边缘为您的透明窟窿。

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.

如果你想要的,你可能需要使用一个单独的掩码孔(JPEG作为彩色和8位PNG透明度应该是一个非常有效的组合 - 有多少哦,我希望有一个图像格式很容易让这个在网络浏览器)

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)

这篇关于位图与透明替换特定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:12
查看更多