asifkt
asifkt

Reputation: 623

Making specific color in bitmap transparent

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?

Upvotes: 4

Views: 16422

Answers (2)

steveh
steveh

Reputation: 1372

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

    }

Upvotes: 2

ChrisF
ChrisF

Reputation: 137148

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.

Upvotes: 2

Related Questions