Camera Access Through Browser

How does Camera Access work in HTML and JS?

The code you show is simply implementing backward compatibility for older browsers.

Before the navigator.getUserMedia() function was standardized, each of the major browsers had their own function, and it was conventional for them to name their extension functions with a browser prefix: webkit for WebKit-based browsers (Chrome and Safari), moz for Mozilla Firefox, and ms for Microsoft browsers.

The logical-OR operator || evaluates its operands from left to right, returning the first one that has a truthy value.

So what this code does is try to use navigator.getUserMedia() if it exists (which it would in a modern browser). If not, it then tries each of the older browser-specific functions as a fallback.

How to allow Chrome to access my camera on localhost?

Are you accessing the webpage that is served over HTTP, on localhost? If you are accessing it from http://localhost/... , Chrome will ask for your permission to use the camera.

Chrome accessing camera on localhost

If you are opening the web page directly, i.e. /Users/Methuz/Documents/index.html then it will not work, even if you explicitly allows it permission

Chrome cannot access camera over file protocol


In this case, the workaround I use is to host the HTML file on a webserver. A quick hack is to use python, in the folder where the HTML page is lcoated: python -m SimpleHTTPServer



Related Topics



Leave a reply



Submit