Camera Access in Browser
by Sumesh KP
HTML
<button onclick="MyFunction2()">
Scan
</button>
<script>
function hasGetUserMedia() {
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
function myFunction() {
if (hasGetUserMedia()) {
var errorCallback = function(e) {
alert('Reeeejected!, cant get data', e);
};
// Not showing vendor prefixes.
navigator.getUserMedia({
video: true
}, function(localMediaStream) {
alert('good to go');
}, errorCallback);
} else {
alert('cant get data');
}
}
function MyFunction2() {
var detectPermissionDialog = function(allowed) {
alert("allowed",allowed);
};
var successCallback = function(error) {
detectPermissionDialog(true);
};
var errorCallback = function(error) {
if ((error.name == 'NotAllowedError') ||
(error.name == 'PermissionDismissedError')) {
detectPermissionDialog(false);
}
};
navigator.mediaDevices.getUserMedia()
.then(successCallback, errorCallback);
}
</script>