当前位置:网站首页>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>
边栏推荐
- FSM and I2C experiment report
- Cost accounting [15]
- JS --- all basic knowledge of JS (I)
- LeetCode#19. Delete the penultimate node of the linked list
- Scoring system based on 485 bus
- 数据在内存中的存储&载入内存,让程序运行起来
- 51 lines of code, self-made TX to MySQL software!
- ucorelab3
- HDU-6025-Coprime Sequence(女生赛)
- STM32學習記錄:輸入捕獲應用
猜你喜欢

学习记录:USART—串口通讯

ucorelab3

学习记录:TIM—电容按键检测

MATLAB综合练习:信号与系统中的应用

STM32学习记录:LED灯闪烁(寄存器版)

ucorelab4

MATLAB实例:阶跃函数的两种表达方式

Learning record: understand systick system timer and write delay function

UCORE Lab 1 system software startup process
![[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)](/img/3a/aadde60352c42199ba287a6997acfa.jpg)
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
随机推荐
Accounting regulations and professional ethics [2]
力扣刷题记录--完全背包问题(一)
51 lines of code, self-made TX to MySQL software!
LeetCode#36. Effective Sudoku
Alice and Bob (2021牛客暑期多校训练营1)
LeetCode#412. Fizz Buzz
Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction
ucorelab3
Accounting regulations and professional ethics [1]
Cost accounting [14]
ucore lab5
Learning record: how to perform PWM output
LeetCode#268. Missing numbers
VS2019初步使用
1010 things that college students majoring in it must do before graduation
0 - 1 problème de sac à dos (1)
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
学习记录:TIM—基本定时器
基于485总线的评分系统
FSM和i2c实验报告