How can I check if user has a webcam or not? [clos

2019-01-23 22:26发布

问题:

I need to know if there's a way to know if the user has a webcam on his computer using javascript or maybe php.

回答1:

There is a plugin:

http://www.xarg.org/project/jquery-webcam-plugin/

if(webcam.getCameraList().length == 0){  
   alert('You don\'t have a web camera');  
}


回答2:

if(confirm('Do you have a webcam?')) {
    //they said yes :-)
} else {
    //they said no :-(
}


回答3:

Muhammet was right. First need to add the plugin http://www.xarg.org/project/jquery-webcam-plugin/ Then you'll need to start the plugin:

$("#camera").webcam({
            width: 320,
            height: 240,
            mode: "callback",
            swffile: "/lorran/jscam_canvas_only.swf",
            onTick: function() {},
            onSave: function() {},
            onCapture: function() {},
            debug: function() {},
            onLoad: function() {}
        });

Then you add the script that check if user has a webcam.

var test;
        test = function(){
            var tester = false;
            try{
                if(webcam.getCameraList().length == 0){  
                   alert('You dont have a camera');  
                                            return;
                }else{
                    alert("cam");
                                            return;

                }
                tester = true;
            }catch(e){
                tester = false;
                setTimeout(test,1000);
            }
        }
        setTimeout(test,1000);

This try and catch is needed for the flash that have a delay to start, so you need to keep trying till the method webcam.getCameraList() exist.