4

I have an Android application to display an image on another image, such that second image's white colour is transparent. To do this, I have used two ImageViews, with the original image to be overlaid as bitmap1 and the image to be made transparent as bitmap2. When I run this, I get some exceptions at the setPixel method.

Here's my code:

Bitmap bitmap2 = null;
int width = imViewOverLay.getWidth();
int height = imViewOverLay.getHeight();
for(int x = 0; x < width; x++)
{
    for(int y = 0; y < height; y++)
    {
        if(bitMap1.getPixel(x, y) == Color.WHITE)
        {
            bitmap2.setPixel(x, y, Color.TRANSPARENT);
        }
        else
        {
            bitmap2.setPixel(x, y, bitMap1.getPixel(x, y));
        }
    }
}

imViewOverLay is the ImageView of the overlay image. Any idea what might be going wrong in the above code?

Ry-
  • 218,210
  • 55
  • 464
  • 476
asifkt
  • 623
  • 2
  • 9
  • 21
  • the code is Bitmap bitmap2 = null; int width = imViewOverLay.getWidth(); int height = imViewOverLay.getHeight(); for(int x=0;x – asifkt Oct 08 '10 at 10:38
  • Try editing your own post to add the code to the body of the question... – Frosty840 Oct 08 '10 at 11:06
  • 1
    What exception do you get? Other than the fact that in the example you're not actually creating `bitmap2`. – ChrisF Oct 08 '10 at 11:22

2 Answers2

2

i think you need to make it mutable Loading a resource to a mutable bitmap

i did this

 BitmapFactory.Options bitopt=new BitmapFactory.Options();

 bitopt.inMutable=true;

 mSnareBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.snare, bitopt);

also, i found i needed to set alpha to something less than 255 for it to render the image with transparent background.

 mPaint.setAlpha(250);
 canvas.drawBitmap(mSnareBitmap, 0, 30, mPaint);

by the way, using white as your transparent color isn't a great idea because you will get aliasing problems at the edges of your opaque objects. i use green because my overlay images don't have any green in (like a green screen in the movies) then i can remove the green inside the loop and set the alpha value based on the inverse of the green value.

private void loadBitmapAndSetAlpha(int evtype, int id) {

        BitmapFactory.Options bitopt=new BitmapFactory.Options();
        bitopt.inMutable=true;
        mOverlayBitmap[evtype] = BitmapFactory.decodeResource(getResources(), id, bitopt);
        Bitmap bm = mOverlayBitmap[evtype];

        int width = bm.getWidth();
        int height = bm.getHeight();
        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                int argb = bm.getPixel(x, y);
                int green = (argb&0x0000ff00)>>8;
                if(green>0)
                {
                    int a = green;
                    a = (~green)&0xff;
                    argb &= 0x000000ff; // save only blue
                    argb |= a;      // put alpha back in
                    bm.setPixel(x, y, argb);
                }
            }
        }

    }
Community
  • 1
  • 1
steveh
  • 1,352
  • 2
  • 27
  • 41
2

The most obvious error is that you're not creating bitmap2 - unless you've not posted all the code of course.

You declare it and set it to null, but then don't do anything else until you try to call bitmap2.setPixel.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • friends, i fixed the issue. removed bitmap2 and set the bitmap1 mutable.. let me know is there any method to speed up the loop operation?? in my application it takes a long time – asifkt Oct 08 '10 at 11:35
  • when i displayin above image.. it shows only the second(bitmap1);I need it to display on another bitmap(bitmap3). anybody plz help me with some hints. am using imageview to display image – asifkt Oct 08 '10 at 11:36
  • @asifkt - by necessity you have to loop over every pixel in the image, which will take a while if you have a large image. Either reduce the size of the image or use an image with a colour palette so you can just change a single value in the palette. – ChrisF Oct 08 '10 at 11:47
  • thank you chrisf, Can u help me to display a bitmap on another??. Already mentioned in above question that the second image to display on another image is transparent for specific colour – asifkt Oct 08 '10 at 11:55
  • asifkt - I'm not familiar with the Android display pipeline so I couldn't answer that I'm afraid. – ChrisF Oct 08 '10 at 12:01
  • Its amazing to me that Android cannot draw a simple transparent bitmap with an arbitrary color value for the transparency! Physically changing the colors or the Alpha channel isn't the right method if the transparency color needs to change at run time. Perhaps Google could add a drawTransparentBitmap(bitmap, x, y, transpencyColor) type of method to the canvas class please? Windows and directFB and all other 2D graphics libraries I have encountered always have this capability. If I have missed something somewhere please reply. Thanks. – Batdude Jul 20 '12 at 15:47