Search code examples
javascriptwindows-8camerawinjs

Windows 8 how to choose which camera to initialize


I am developing a Windows Store App and I am using the Camera and Microphone capabilities. I would like the back camera to be initialized but the examples that I have found always initialize the front camera. Here's the code that I have:

Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture)
    .done(function (devices) {
        if (devices.length > 0) {
            // Using Windows.Media.Capture.MediaCapture APIs to stream from webcam 
            mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
            mediaCaptureMgr.initializeAsync().done(initializeComplete, initializeError);
        } else {
            var div = document.createElement('div');
            div.innerHTML = "No Camera found";
            document.body.appendChild(div);
        }
    });

In this case mediaCaptureMgr refers to the front camera. I went through the documentation and it says that I have provide a videoDeviceId to the MediaCapture() function like this:

mediaCaptureMgr = new Windows.Media.Capture.MediaCapture({
    videoDeviceId: devices[1].id
});

However still the front camera is initialized. I am writing and testing this on a Surface. Could you help me with this?


Solution

  • You have to create a MediaCaptureInitializationSettings object:

    var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
    settings.videoDeviceId = devices[1].id;
    mediaCaptureMgr.initializeAsync(settings).done(initializeComplete, initializeError);