javaandroidbitmap

What is the easiest way to convert all the pixels of a specific color to transparent in a BMP?


Looking for the least painful way to grab a Bitmap and translate every pixel of a specific color to transparent.

In particular, I have set all the pixels I want to be transparent to 0xFF00FF, but I am not seeing any native method that do this.

Do I have to actually loop through all the pixels comparing values? And if so, can I change them in place or do I need to make a new bitmap array and create a new bitmap?


Solution

  • Bitmaps don't have alpha channels like PNG/GIF. It's up to the application to use a transparency mask.

    Correction: bitmaps can support alpha channels (see: https://stackoverflow.com/a/29585717/738924)

    You could iterate through the array and test and convert each pixel. Take a look at BufferedImage docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/… I believe it can find pixels of a certain rgb and convert them.