Home > Tutorials > Image Processing – Highlight Image On The Fly

Image Processing – Highlight Image On The Fly

Today was a fun day at  work, because I need to find a way to highlight an image in Android. I’ve tried to ask Mr. Google and he doesn’t work for me. So sadly!

Ooops, somehow, one of my colleague  found that the build-in Android already has this function. I took a look around and finally get the idea how to make it.

For a given image, well any like this as sample:

Normal Image

Normal Image

After highlighting, it would be like this:

Highlighted Image

Highlighted Image

So, what did I do to make this work?

It might be easier if I just need to explain by the code:

	public static Bitmap doHighlightImage(Bitmap src) {
		// create new bitmap, which will be painted and becomes result image
		Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
		// setup canvas for painting
		Canvas canvas = new Canvas(bmOut);
		// setup default color
		canvas.drawColor(0, PorterDuff.Mode.CLEAR);

		// create a blur paint for capturing alpha
		Paint ptBlur = new Paint();
		ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
		int[] offsetXY = new int[2];
		// capture alpha into a bitmap
		Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
		// create a color paint
		Paint ptAlphaColor = new Paint();
		// paint color for captured alpha region (bitmap)
		canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
		// free memory

		// paint the image source
		canvas.drawBitmap(src, 0, 0, null);

		// return out final image
		return bmOut;

The trick here is to capture the alpha channel from original image the fill color. Bang! It works like a charm!

Well, you might take a look at the original implementation of Google Android: View the Original

The above code is the one I customized from the original code.

Feel free to learn from me and share to me, buddies!



Pete Houston


  1. Fareeha
    April 9, 2017 at 2:05 pm

    Hey…can you give me the complete source code i am new to android

  2. Ahsan Farooq
    August 17, 2016 at 11:35 am

    sir where to do programming i am beginner ,, can u send me complete guide

  3. Hepzi
    May 17, 2016 at 12:54 pm

    hw to design for dis code.. am s beginner

  4. hardik
    December 21, 2015 at 11:26 pm

    hello sir this is very useful for me but i am new in android so i need one complete demo to apply effect can you please give me demo code to apply effect on image. please help me else please send me code on h.k.lakhani5055@gmail.com

  5. mohamed
    June 11, 2013 at 7:55 pm

    Mr . Pete
    you have helpful tutorial but if you can help me by Source Code

  6. November 1, 2012 at 1:39 am

    really great great tutorial..

  7. Mohsin Hussain
    June 4, 2012 at 8:12 pm

    its really awesome! i loved your work but i didnt get why my result bitmap image came a lot smaller then the original image.. can you tell me why this happened?

    • June 4, 2012 at 9:38 pm

      eh, the inverted image should be bigger than the original image. As you can see, I added 96 pixels in both width and height.

      • Mohsin Hussain
        June 5, 2012 at 12:09 am

        i have a 300dp by 300dp square bitmap image and when function takes it in the returned image is almost half of the original size.. i am also confused that why is this happening..

  8. ketan mehta
    March 23, 2012 at 1:15 pm

    superb dude

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: