当前位置:网站首页>JS调用摄像头
JS调用摄像头
2022-07-06 09:25:00 【星时代曹波涛】
<!doctype html>
<html lang="en">
<head>
<title>GET VIDEO</title>
<meta charset="utf-8">
</head>
<body>
<input type="button" title="开启摄像头" value="开启摄像头" onclick="getMedia()" />
<video id="video" width="500px" height="500px" autoplay="autoplay"></video>
<canvas id="canvas" width="500px" height="500px"></canvas>
<button id="snap" onclick="takePhoto()">拍照</button>
<script> //获得video摄像头区域 let video = document.getElementById("video"); function getMedia() {
let constraints = {
video: {
width: 500, height: 500}, audio: true }; /* 这里介绍新的方法:H5新媒体接口 navigator.mediaDevices.getUserMedia() 这个方法会提示用户是否允许媒体输入,(媒体输入主要包括相机,视频采集设备,屏幕共享服务,麦克风,A/D转换器等) 返回的是一个Promise对象。 如果用户同意使用权限,则会将 MediaStream对象作为resolve()的参数传给then() 如果用户拒绝使用权限,或者请求的媒体资源不可用,则会将 PermissionDeniedError作为reject()的参数传给catch() */ let promise = navigator.mediaDevices.getUserMedia(constraints); promise.then(function (MediaStream) {
video.srcObject = MediaStream; video.play(); }).catch(function (PermissionDeniedError) {
console.log(PermissionDeniedError); }) } function takePhoto() {
//获得Canvas对象 let canvas = document.getElementById("canvas"); let ctx = canvas.getContext('2d'); ctx.drawImage(video, 0, 0, 500, 500); } </script>
</body>
</html>
边栏推荐
- 区间和------离散化
- 力扣刷题记录
- Leetcode notes - dynamic planning -day7
- Market trend report, technical innovation and market forecast of lip care products in China and Indonesia
- China's salt water membrane market trend report, technological innovation and market forecast
- Learning record: USART serial communication
- ucorelab4
- China's earthwork tire market trend report, technical dynamic innovation and market forecast
- Accounting regulations and professional ethics [2]
- Visual analysis of data related to crawling cat's eye essays "sadness flows upstream into a river" | the most moving film of Guo Jingming's five years
猜你喜欢
Lab 8 文件系统
LeetCode#62. Different paths
ucore lab 2
Lab 8 file system
学习记录:理解 SysTick系统定时器,编写延时函数
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
Leetcode notes - dynamic planning -day7
Eslint--- error: newline required at end of file but not found (EOL last) solution
The wechat red envelope cover designed by the object is free! 16888
UCORE Lab 1 system software startup process
随机推荐
学习记录:使用STM32外部输入中断
入门C语言基础问答
Stm32 dossiers d'apprentissage: saisie des applications
JS --- JS function and scope (II)
Cost accounting [14]
LeetCode#36. Effective Sudoku
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
LeetCode#118. Yanghui triangle
Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
ucore lab5
C4D quick start tutorial - creating models
学习记录:STM32F103 时钟系统概述工作原理
C 基本语法
Path problem before dynamic planning
JS --- detailed explanation of JS facing objects (VI)
LeetCode#204. Count prime
Cost accounting [18]
MATLAB综合练习:信号与系统中的应用
ucorelab3
Learning record: use stm32f1 watchdog