eduardolundgren / tracking.js

A modern approach for Computer Vision on the web
http://trackingjs.com
Other
9.43k stars 1.44k forks source link

Android Pie unable to use front camera #369

Open 74jian opened 5 years ago

74jian commented 5 years ago

recommended add camera settings,because mobile devices use Face (camera) need front camera.and the current code does not support Android Pie(9) using the front camera.because Android P use front camera requires getUserMediato force use facingMode: { exact: "user" } or use deviceId to select,this is my solution for use deviceId in Android Pie(9)

tracking.initUserMedia_ = function(element, opt_options) {
    window.navigator.mediaDevices.enumerateDevices().then(function(devices) {
        let cfg={
            video:{'facingMode':'user'}, 
            audio: (opt_options && opt_options.audio) ? true : false,
        };
        devices = devices.filter(function(device) { return device.kind === 'videoinput'});
        if (navigator.userAgent.toLowerCase().indexOf("android") > 0) {
            for (let i = 0; i < devices.length; i++) {
                let device = devices[i];
                if (device.label) {
                    if (device.label.split(',')[1]==' facing front'){
                        cfg={
                            video:{ 
                                deviceId: {'exact':device.deviceId},
                            },
                            audio: (opt_options && opt_options.audio) ? true : false ,
                        };
                        break;
                    }
                }
            }
        }
        window.navigator.mediaDevices.getUserMedia(cfg).then(function(stream) {
            element.srcObject = stream;
        }).catch(function(err) {
            throw Error('Cannot capture user camera.');
        });
    });
  };