Skip to content

Instantly share code, notes, and snippets.

@edisonthk
Last active May 7, 2021 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edisonthk/528449e8baef70b72d04fda87bb14cfb to your computer and use it in GitHub Desktop.
Save edisonthk/528449e8baef70b72d04fda87bb14cfb to your computer and use it in GitHub Desktop.
access to webcam and capture image, show it on browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Media Device Access</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
video, img {
max-width:100%;
}
</style>
</head>
<body>
<video autoplay></video>
<script>
(function() {
var video = document.querySelector('video')
// use MediaDevices API
// docs: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
if (navigator.mediaDevices) {
// access the web cam
navigator.mediaDevices.getUserMedia({video: true})
// permission granted:
.then(function(stream) {
video.srcObject = stream;
})
// permission denied:
.catch(function(error) {
document.body.textContent = 'Could not access the camera. Error: ' + error.name;
});
}
})();
</script>
</body>
</html>
@edisonthk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment