当前位置:网站首页>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>
边栏推荐
- Cost accounting [19]
- C语言必背代码大全
- Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
- 学习记录:使用STM32外部输入中断
- Market trend report, technical innovation and market forecast of lip care products in China and Indonesia
- Accounting regulations and professional ethics [1]
- Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction
- Unpleasant error typeerror: cannot perform 'ROR_‘ with a dtyped [float64] array and scalar of type [bool]
- LeetCode#118. Yanghui triangle
- D - Function(HDU - 6546)女生赛
猜你喜欢
STM32 learning record: play with keys to control buzzer and led
LeetCode#19. Delete the penultimate node of the linked list
ucorelab3
csapp shell lab
12306: mom, don't worry about me getting the ticket any more (1)
Learning record: STM32F103 clock system overview working principle
Scoring system based on 485 bus
学习记录:STM32F103 时钟系统概述工作原理
Learning record: understand systick system timer and write delay function
Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
随机推荐
洛谷P1102 A-B数对(二分,map,双指针)
学习记录:如何进行PWM 输出
China's earthwork equipment market trend report, technical dynamic innovation and market forecast
Cost accounting [14]
cs零基础入门学习记录
Perinatal Software Industry Research Report - market status analysis and development prospect forecast
Brief introduction to libevent
ucorelab3
Printing quality inspection and verification system Industry Research Report - market status analysis and development prospect forecast
FSM and I2C experiment report
Crawler series (9): item+pipeline data storage
学习记录:使用STM32F1看门狗
F - Birthday Cake(山东省赛)
学习记录:TIM—电容按键检测
Research Report on market supply and demand and strategy of China's land incineration plant industry
Learning record: STM32F103 clock system overview working principle
ArrayList set
Determine the Photo Position
0 - 1 problème de sac à dos (1)
JS --- all knowledge of JS objects and built-in objects (III)