I am trying to get access to webcam and display video stream for capturing photo. I am not even getting consent for camera access i.e. popup message .I searched a lot but couldn’t find anything.
Below is the code for it:
document.addEventListener("DOMContentLoaded", function() {
console.log("DOM fully loaded and parsed");
navigator.permissions.query({ name: 'camera' }).then(function(permission) {
if (permission.state === 'denied') {
alert("Camera access is denied. Please allow camera access and reload the page.");
}
});
const video = document.getElementById('video');
if (!video) {
console.error("Video element not found");
return;
} else {
console.log("Video element found");
}
const canvas = document.getElementById('canvas');
if (!canvas) {
console.error("Canvas element not found");
return;
} else {
console.log("Canvas element found");
}
const captureButton = document.getElementById('capture');
if (!captureButton) {
console.error("Capture button not found");
return;
} else {
console.log("Capture button found");
}
const photoData = document.getElementById('photo_data');
if (!photoData) {
console.error("Photo data input not found");
return;
} else {
console.log("Photo data input found");
}
// Get access to the camera
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
console.log("Camera stream obtained");
video.srcObject = stream;
video.play();
}).catch(function(error) {
console.log("Error accessing camera: " + error);
alert("Could not access the camera. Please check permissions.");
});
} else {
alert("getUserMedia is not supported by your browser.");
}
// Capture photo
captureButton.addEventListener("click", function() {
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0, 320, 240);
const dataURL = canvas.toDataURL('image/png');
photoData.value = dataURL;
canvas.style.display = 'block';
});
});
function submitForm() {
const photoData = document.getElementById('photo_data').value;
if (!photoData) {
alert("Please capture a photo before submitting the form.");
return false;
}
return true; // Ensures the form is submitted
}
i tried a lot and searched different articles but couldn’t find anything.Kindly guide me.
2
Answers
The
navigator.permissions.query({ name: 'camera' })
is not universally supported in all browsers. Relying on this might not be effective. Instead, use a more straightforward check withnavigator.mediaDevices.getUserMedia()
directly.The
navigator.permissions.query
method doesn’t prompt for permission; it only checks if permission has already been granted or denied. This means that the browser won’t show a camera access prompt based on this query alone. ThegetUserMedia
API should handle this prompt when it’s called.Check the below code.
Also, some browsers only allow access to the camera via secure origins (HTTPS). If you’re testing locally, try running it on a localhost server or deploying the code to an HTTPS server.
And their could be previous saved browser settings to deny the access to the camera. Please check on it.