Is there a way to set a static picture as the photo been taken by the emulator camera? I would like to test ir with zxing barcode reader on emulator.
3 Answers
If you are running the emulator on linux you can create a mock webcam showing an image (e.g. QRcode) with v4l2loopback and gstreamer.
Install v4l2loopback:
$ wget https://github.com/umlaeute/v4l2loopback/archive/master.zip
$ unzip master.zip
$ cd v4l2loopback
$ make
$ sudo make install
Check how many cameras you already have (I only had /dev/video0) and init the next one:
$ sudo modprobe v4l2loopback video_nr=1 card_label="mockCam"
Stream an image (for example a QR from googlecharts) to the mockCam. This requres :
$ wget "https://chart.googleapis.com/chart?chs=600x340&cht=qr&chl=testing" -O qr.png
$ gst-launch-0.10 filesrc location=qr.png ! pngdec ! freeze ! v4l2sink device=/dev/video1
You can check if your mock camera is picked up by the emulator:
$ ./emulator -avd yourAVD -webcam-list
If so, you can start the emulator with the mock webcam:
$ ./emulator -avd yourAVD -camera-back webcam1
You can also change the AVD setting to webcam1. Hope this helps.

- 442
- 5
- 5
-
4If someone are trying to use this with `gst-launch-1.0` the `freeze` changed to `imagefreeze` – Elias Soares Aug 10 '17 at 14:21
-
1today it was "cd v4l2loopback-master" and later on "sudo depmod -a" before the modprobe :) – Arno Teigseth Nov 17 '18 at 22:00
-
1had problems streaming with gst-launch, was able tod othat with ffmpeg: ffmpeg -loop 1 -stream_loop -1 -framerate 10 -i binary-file.png -pix_fmt yuv420p -s 320x240 -f v4l2 /dev/video1 – ufk Jan 06 '20 at 10:29
-
I also needed to specify `-camera-front none`. Setting only `-camera-back` option resulted in the following error: `_camera_client_query_stop: Camera '/dev/video2' is not started` – xonya Jun 12 '22 at 22:09
This can be solved by adding a classic abstraction layer such as this gentleman has done in this sample source code:
http://www.tomgibara.com/android/camera-source
Specifically, sounds like you may want to have some test pictures and use the BitmapCamera

- 46,929
- 26
- 130
- 185
-
Can you elaborate? I don't understand how CameraSource is implemented. – John Giotta Nov 21 '11 at 16:13
-
CameraSource is just an abstract class (http://www.tomgibara.com/android/CameraSource.java). During testing in the emulator, you can instantiate a bitmapcamera in order to test with a specific image that you've provided. However, for running on an actual device, you can instantiate a GenuineCamera class, which uses the device's camera: http://www.tomgibara.com/android/GenuineCamera.java – Joel Martinez Nov 21 '11 at 16:44
-
In the Google IO Session was said it could use webcam. But I did not find out how to do this: https://developers.google.com/events/io/sessions/gooio2012/104/ Perhaps they were talking about the next version?! – Mark Jul 02 '12 at 09:48
-
This might work for the basic "take a picture" use case, but does not implement Camera.PreviewCallback. – tomwhipple Sep 13 '12 at 20:54
-
I'm looking at the BitmapCamera code, but it's not obvious what it does or how it does it. Does this code allow us to provide an image instead of loading the camera? – Ciaran Gallagher Mar 19 '13 at 19:35
-
1Yes, that is correct ... in this way, you can do all your testing with, what is essentially a mock class that provides a static bitmap instead of using the Camera APIs. – Joel Martinez Mar 20 '13 at 14:59
You can also use MockCamera for Android. See the detailed reference to answer. https://stackoverflow.com/a/38456086/1053097
-
1I tried the mock camera and it is showing a video. Worked. But what if I want to a static image? – fangmobile Nov 17 '16 at 19:29
-
Although it's late, but each frame of video is converted into a texture and loaded onto the surface. Similarly, you can pass image as a texture and load it. – muneikh Mar 17 '17 at 09:20