1''
1''

Reputation: 27125

How to use webcam in Android emulator?

I'm connecting a webcam to my emulator by setting the front camera to "webcam0" in the AVD Manager. When I start the emulator's camera application, I get the error

CameraService::connect X (pid 702) rejected (invalid cameraId 0).

Here's the relevant portion of the Android source code:

sp<ICamera> CameraService::connect(
    const sp<ICameraClient>& cameraClient, int cameraId) {

    int callingPid = getCallingPid();

    [...]

    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
        LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
            callingPid, cameraId);
        return NULL;
    }

    [...]
}

The webcam has been correctly assigned an ID of 0 because there's only 1 camera. However, mNumberOfCameras is presumably still 0. This means that the camera is being registered by the emulator, but it hasn't bothered to update the number of connected cameras.

How can I connect a webcam so that it will be properly recognized by the emulator?

The command emulator -webcam-list -avd <name of your AVD> in \android-sdks\tools gives the result:

List of web cameras connected to the computer:
Camera `webcam0` is connected to device `AndroidEmulatorVC0` on channel 0 using pixel format `BGR4`

When I launch the webcam from Eclipse's AVD manager or using emulator -camera-front webcam0 -avd <name of your AVD>, I get the following window:

enter image description here

This seems to be a bug in the emulator. The suggested answer tells you what to do to set up the camera, but doesn't solve the problem for me. I ultimately solved it by using a laptop with a built-in webcam. Perhaps another USB webcam might have worked as well.

Upvotes: 124

Views: 205534

Answers (3)

Brandon Minnick
Brandon Minnick

Reputation: 15410

On macOS

Explanation

Adding to @nurnachman's answer above, on macOS you'll also need to grant the emulator elevated permissions by launching it from the Terminal using sudo.

Steps

1. Use the Android Device Manager to set the Camera settings to Webcam0

UPDATE

In Android Studio AVD:

  1. Open AVD Manager:

AVD menu

  1. Add/Edit AVD:

Specific AVD strip

  1. Click Advanced Settings in the bottom of the screen:

AVD Advanced Settings

  1. Set your camera of choice as the front/back cameras:

AVD Camera Settings

2. Launch the macOS Terminal

3. In the Terminal, enter the following command to list the names of your installed Android Emulators

Emulators Installed via Android Studio:

~/Library/Android/sdk/emulator/emulator -list-avds

Emulators Installed via Visual Studio for Mac:

~/Library/Developer/Xamarin/android-sdk-macosx/emulator emulator -list-avds

4. Launch the Android Emulator with elevated privileges:

Emulators Installed via Android Studio:

sudo ~/Library/Android/sdk/emulator/emulator -avd [Your Emulator Name]

Note: Replace [Your Emulator Name] with the name of your emulator discovered in Step 3

e.g. sudo ~/Library/Android/sdk/emulator/emulator -avd pixel_5_-api_33

Emulators Installed via Visual Studio for Mac:

~/Library/Developer/Xamarin/android-sdk-macosx/emulator -avd [Your Emulator Name]

Note: Replace [Your Emulator Name] with the name of your emulator discovered in Step 3

e.g. sudo ~/Library/Developer/Xamarin/android-sdk-macosx/emulator -avd pixel_5_-api_33

5. When the Emulator launches and when your app access the Camera, accept macOS request to grant Camera permissions


Troubleshooting

Ensure Camera Permission Enabled for Terminal

If the Android Camera is still not working after launching it with elevated permissions via the Terminal, in System Settings.app, navigate to Privacy & Security -> Camera and ensure the Camera Permission for Terminal is enabled

enter image description here

Upvotes: 8

nurnachman
nurnachman

Reputation: 4565

UPDATE

In Android Studio AVD:

  1. Open AVD Manager:

AVD menu

  1. Add/Edit AVD:

Specific AVD strip

  1. Click Advanced Settings in the bottom of the screen:

AVD Advanced Settings

  1. Set your camera of choice as the front/back cameras:

AVD Camera Settings

Upvotes: 186

Lalith B
Lalith B

Reputation: 12269

Follow the below steps in Eclipse.

  1. Goto -> AVD Manager
  2. Create/Edit the AVD.
  3. Hardware > New:
  4. Configures camera facing back
  5. Click on the property value and choose = "webcam0".
  6. Once done all the above the webcam should be connected. If it doesnt then you need to check your WebCam drivers.

Check here for more information : How to use web camera in android emulator to capture a live image?

enter image description here

Upvotes: 48

Related Questions