1

I want to remove the white background color in a bitmap like in this example image.

This is my first try:

BitmapDrawable drawableImg = getImage();
drawableImg.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
PorterDuff.Mode.DST_OUT));

Second try:

To remove the white colors range by setting fromBgColor and toBgColor, But when I replace the color with android transparent color the image turns into black here is the code:

public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap, 
       int fromBgColor, int toBgColor) {
    Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
    int nWidth = result.getWidth();
    int nHeight = result.getHeight();
    for (int y = 0; y < nHeight; ++y)
        for (int x = 0; x < nWidth; ++x) {
            int nPixelColor = result.getPixel(x, y);
            if (nPixelColor >= fromBgColor && nPixelColor <= toBgColor)
                result.setPixel(x, y, Color.TRANSPARENT);
        }
    return result;
}

Hint I had a hint that bitmap does not support transparent bits and I should use PNG or Gif instead of bitmaps is that right?

Hint 2 It turned out that bitmap in Android has an option to show transparency since API level 1 using Bitmap.Config enum. Check this link in the documentation of Android.

enter image description here

Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
  • hi @khaled, did you found any solution for this. I am searching for the solution and need to convert white background after masking to transparent. – Dory Aug 29 '13 at 05:18
  • Nope. But now I know that to do this I'll need to draw PNG not bitmaps as bitmaps do not support having transparent backgrounds. – Khaled Annajar Sep 01 '13 at 12:24
  • hey @khaled,thanks for reply. But I am not understanding your view how would you draw PNG and not bitmap. If possible could you guide me more how would you do this. As I am stuck with this issue since long,and did not find any solution of it. – Dory Sep 02 '13 at 05:34
  • hey @khaled, I have same issue which is here : http://stackoverflow.com/questions/18502946/remove-white-background-from-masked-image-in-android. Could you please help me in this, Is it possible to remove white background from image in android. – Dory Sep 02 '13 at 06:57

1 Answers1

0

what is beneath your image? Maybe it could be a layout issue. Have you tried setting android:background="@android:color/transparent" in the xml? I see you tried the getBitmapWithTransparentBG described in Android: Make specific (green) color in background image transparent But have you tried also this? How to change colors of a Drawable in Android? About the image being jpg or png, I've managed to work with both after loading and setting it to a imageview (getting the bitmap back from it), and it allows to set the transparency using the getBitmapWithTransparentBG function as quoted above.

Community
  • 1
  • 1
  • in this question http://stackoverflow.com/questions/1309629/how-to-change-colors-of-a-drawable-in-android this answer could work if you want to give it any color but transparent. As bitmaps does not have the concept of transparency. I don't know in the latest SDKs if there is a png drawable or not. But I need PNGs to support transparent color. I shall check and return back to you. – Khaled Annajar Apr 11 '14 at 15:40