zduny
zduny

Reputation: 2541

Android - How to create invisible camera preview for video recording?

I want to create hidden SurfaceView for camera preview. I tried method recommended in answer to another question:

WindowManager wm = (WindowManager) mCtx.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSLUCENT);        
wm.addView(surfaceview, params);

surfaceview.setZOrderOnTop(true);
mHolder.setFormat(PixelFormat.TRANSPARENT);

but it isn't invisible, in fact it takes the whole screen. I tried setting alpha or resizing it but it doesn't work. Is there a way to making it hidden or at least resizing it to 1x1 size?

EDIT:

I would also add that simply placing 1x1 SurfaceView in activity layout wouldn't work in my case because I use multiple layouts and I believe Views are destroyed on layout change...

Upvotes: 1

Views: 2433

Answers (1)

Alan
Alan

Reputation: 608

The SurfaceView is taking up the whole screen because your code defines the width/height to be WRAP_CONTENT. Try using

new LayoutParams(1, 1, LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT)

To make hidden camera or similar stuff, you should not use the Context from your current Activity because your activity needs to be visible during recording (so this is not hidden...). When the current activity to put to background, the context will be null and the recording will stop or crash.

You may try running a Service (non-visible but also has a context) and create a SurfaceView from code in that service.

Upvotes: 1

Related Questions