mnagy
mnagy

Reputation: 1085

Android screen record with secured layers

I use the MediaProjection API to record a screen in Android. I want to hide views from the recording so I made a surface view and made a secure view. The problem is the view appears in the recording with black background and not hidden.

Is there a way to hide a view completely from the recording.. as it is invisible?

Upvotes: 1

Views: 483

Answers (1)

maff
maff

Reputation: 955

I was trying to do the same thing, but unfortunately I couldn't find a clean way of doing it. My solution requires post-processing of the recorded frames and only works if the layer you want to hide is a translucent overlay (all pixels must have alpha < 1).

The idea is to invert the alpha blending performed by the Android view compositor.

Let Cred, Cgreen, Cblue and Calpha be the color components for your overlay and let Bred, Bgreen, Bblue be the color components for whatever is in the background. The final color that will show up in your frame for each pixel is given by the following formula:

Ored = Bred * (1 - Calpha) + Cred * Calpha
Ogreen = Bgreen * (1 - Calpha) + Cgreen * Calpha
Oblue = Bblue * (1 - Calpha) + Cblue * Calpha

If you have the exact bitmap of the overlay layer you want to hide, you can reconstruct the background colors like this:

Bred = (Ored - Cred * Calpha) / (1 - Calpha)
Bgreen = (Ogreen - Cgreen * Calpha) / (1 - Calpha)
Bblue = (Oblue - Cblue * Calpha) / (1 - Calpha)

Of course some information will be lost in the process, because colors are ultimately converted to 8 bit integers. The reconstruction can therefore produce visual artifacts, but it should always look better than a black overlay. The artifacts will be more or less pronounced depending on the alpha channel of your overlay (alpha values closer to 0 will allow you to reconstruct the background color more accurately).

This will only work with the full resolution images. Any kind of scaling or lossy compression will break this technique. Because of storage limitations, this post-processing is probably only viable if performed in real-time using a SurfaceTexture in a shader pass.

Upvotes: 0

Related Questions