Android - Selective focus on bitmap

Currently, I can blur the whole bitmap (by resizing it, for example, for example).

The effect I'm trying to accomplish is selective blurring: the bitmap of the result will be blurry, minus the round / oval portion, which will still be sharp:

Example image

The tricky part is that the sharp oval part can be smaller or larger and must be movable (its coordinates are not always the center of the original bitmap image).

I have already found a solution, but I do not consider it a good solution:

  • Copy the original bitmap to two different bitmaps (background and foreground).
  • Blur background.
  • Trim the foreground to the desired shape (round or oval)
  • Delete the foreground borders a bit (to avoid too sharp a difference between the foreground and background images)
  • Return two images together
  • Export it as a bitmap

Possible solution image

Another solution may be to recreate the blur algorithm that will run through each pixel of the original bitmap and apply the amount of blur higher or lower depending on the portion of the bitmap.

+5
source share
1 answer

Finally, I decided to follow my first idea using @DerGolem links. Here is the updated version of the chart:

Updated chart

The algorithm is pretty simple:

  • We create two copies of the bitmap: the first will serve as the background, and the other will be used as the sharpest part of the image. So that the second is not too sharp, we will use the prepared mask (stored in the folder with pictures)
  • We blur the first as much as we want
  • We apply the mask to the second bitmap
  • We create a bitmap from these two previous steps.

I created a sample demo application hosted on BitBucket. You can clone the project and try it, the indicators are much better than I expected!

To achieve this, I used the following resources:

As the readme says, the provided code is far from perfect, but it works.

+3
source

Source: https://habr.com/ru/post/1210044/


All Articles