23

I have an Android app that loads an image as a bitmap and displays it in an ImageView. The problem is that the image appears to have a transparent background; this causes some of the black text on the image to disappear against the black background.

If I set the ImageView background to white, that sort of works, but I get ugly big borders on the image where it is stretched to fit the parent (the actual image is scaled in the middle).

So - I want to convert the transparent pixels in the Bitmap to a solid colour - but I cannot figure out how to do it!

Any help would be appreciate!

Thanks Chris

ccbunney
  • 2,282
  • 4
  • 26
  • 42
  • If you had a screen shot that would really help. It sounds to me like you are trying to solve this the wrong way. I would try and layer views on top of each other to get the effect you desire. That way you have a main backgound color view, then overlay it with an image view with your white background and source image. – Nathan Jan 26 '13 at 00:00

2 Answers2

37

If you are including the image as a resource, it is easiest to just edit the image yourself in a program like gimp. You can add your background there, and be sure of what it is going to look like and don't have use to processing power modifying the image each time it is loaded.

If you do not have control over the image yourself, you can modify it by doing something like, assuming your Bitmap is called image.

Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.WHITE);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle();  // clear out old image 
iagreen
  • 31,470
  • 8
  • 76
  • 90
7

You can loop through each pixel and check if it is transparent.

Something like this. (Untested)

        Bitmap b = ...;
        for(int x = 0; x<b.getWidth(); x++){
            for(int y = 0; y<b.getHeight(); y++){
                if(b.getPixel(x, y) == Color.TRANSPARENT){
                    b.setPixel(x, y, Color.WHITE);
                }
            }
        }
MrZander
  • 3,031
  • 1
  • 26
  • 50
  • 2
    If you're going with this strategy, don't use `getPixel`. Any time you iterate through every pixel in the `Bitmap` you should favor [`getPixels`](http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int[],%20int,%20int,%20int,%20int,%20int,%20int%29) instead. – kabuko Jan 25 '13 at 23:20
  • 1
    @MrZander - I tried this method and it did not work. Adding a debug statement showed that it was finding the TRANSPARENT pixels, but setting them to WHITE (or RED or any other colour) has not effect. The image still seems to have transparency.... weird??? – ccbunney Jan 25 '13 at 23:23
  • @KernowBunney Probably because your bitmap is not mutable. Try to perform `Bitmap b = yourbitmap.copy(Bitmap.Config.ARGB_8888, true)` and setting the new bitmap as the image. Also, this method is untested and I don't know if it the most efficient method. @kabuko may have a point. – MrZander Jan 25 '13 at 23:27
  • 3
    Also, another method you could try is creating a new bitmap that is pure white and then drawing your bitmap over the white one. (This way you don't have a border and you don't have to iterate over the entire bitmap. – MrZander Jan 25 '13 at 23:29
  • @MrZander I did perform the operation on a copy of the bitmap; I had the same problem. I think your last suggestion would have worked as in iagreen's answer below. Thanks. – ccbunney Jan 25 '13 at 23:56
  • how can i replace #ffffff to #000000 – Ashish Sahu May 24 '14 at 00:32