Last active
May 7, 2021 03:26
-
-
Save edisonthk/528449e8baef70b72d04fda87bb14cfb to your computer and use it in GitHub Desktop.
access to webcam and capture image, show it on browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refer from https://gist.github.com/anantn/1852070#gistcomment-1983484