admin管理员组

文章数量:1287535

I'm using this jQuery webcam plugin in a website I'm working on. If you go to the website, you'll notice it uses flash, and you have to click 'accept' in order to get it to work.

I want to determine if the webcam is active (on) or not. How can I do this with javascript/jquery?

In other words, what do I substitute in the if statement here?

function capture_image(){
    if(webcam.muted == true) {alert("hey");}

I'm far from a pro in javascript and jquery, so real code would be much appreciated.

Also, if someone could tell me how to capture the event of the webcam being activated, it would also be much appreciated. If you can't figure it out, don't worry about it.

I'm using this jQuery webcam plugin in a website I'm working on. If you go to the website, you'll notice it uses flash, and you have to click 'accept' in order to get it to work.

I want to determine if the webcam is active (on) or not. How can I do this with javascript/jquery?

In other words, what do I substitute in the if statement here?

function capture_image(){
    if(webcam.muted == true) {alert("hey");}

I'm far from a pro in javascript and jquery, so real code would be much appreciated.

Also, if someone could tell me how to capture the event of the webcam being activated, it would also be much appreciated. If you can't figure it out, don't worry about it.

Share Improve this question asked May 15, 2012 at 0:26 varatisvaratis 14.8k24 gold badges75 silver badges115 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4 +50

Add the following function to the debug option of the webcam.

$("#camera").webcam({
    width: 320,
    // other options etc.
    debug: function(type, message) { 
        if (message === "Camera started") { window.webcam.started = true; } 
    }
});

We cannot test for inactivity to be true (i.e. muted === true) but now we can test for webcam.started being true or undefined/false:

function capture_image(){
    if( !webcam.started ) { alert("hey, camera not started"); }
}

How to capture the event

There is not an event per se, other than the debug event. You could make one...

First do the following:

$("#camera").webcam({
    width: 320,
    // other options etc.
    debug: function(type, string) { 
        if (string === "Camera started") { 
            window.webcam.started = true; 
            if (window.webcam.onStarted) { window.webcam.onStarted(); } 
        } 
    }
});

Next add a function to our new event webcam.onStarted:

window.webcam.onStarted = function () {
    alert("Whey, the webcam started");
};

You could use the method .getCameraList() to get all cameras available.

onLoad The onLoad callback is called as soon as the registration of the interface is done. In the example above, I use the callback to get a list of all cameras available:

onLoad: function() {
    var cams = webcam.getCameraList();
    for(var i in cams) {
        jQuery("#cams").append("<li>" + cams[i] + "</li>");
    } 
}

I think you will need to use the ExternalInterface API to tell the JavaScript on your page when the privacy setting has been agreed to.

However I don't know if you will be able to get to the "image information" you want because flash has control of the camera, so the JavaScript will only be able to tell flash to "capture" and image through the ExternalInterface, you will probably have to use flash (ActionScript) to send the image data to a webservice before the javascript on the page can do anything with it.

HTML5 has an <input type="file" accept="image/*" capture="camera" id="capture"> that might suffice, instead of the flash implementation.

Not sure about all these plugins etc. This works for me, though we're still testing:

webcamActive = false;
webcamActive = $("#video").attr('src');

If anyone here finding the solution for detecting when the webcam is loaded and he is using webcam.js then here is the way to detect when the webcam is loaded:

Webcam.on( 'live', function() {
    // camera is live, showing preview image
    // (and user has allowed access)
} );
navigator.getMedia({video: true}, function() {
  // webcam is available
}, function() {
  // webcam is not available
});

This is the simplest way ive found. This asks permission to access and turns camera on you can alter to your needs.

本文标签: javascriptjQuery webcamflash How to detect if webcam is activeStack Overflow