当前位置:网站首页>Case of camera opening by tour
Case of camera opening by tour
2022-07-01 21:37:00 【Desperately_ petty thief】
<!doctype html>
<html lang="en">
<head>
<title>js Call the camera to take photos and upload pictures </title>
<meta charset="utf-8">
</head>
<body>
<button onclick="openMedia()"> Turn on camera </button>
<video id="video" width="500px" height="500px" autoplay="autoplay"></video>
<canvas id="canvas" width="500px" height="500px"></canvas>
<button onclick="closeMedia()"> Turn off camera </button>
<script>
let mediaStreamTrack=null; // Video object ( overall situation )
function openMedia() {
let constraints = {
video: { width: 500, height: 500 },
audio: true
};
// get video camera
let video = document.getElementById('video');
let promise = navigator.mediaDevices.getUserMedia(constraints);
promise.then((mediaStream) => {
mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1];
video.srcObject = mediaStream;
video.play();
});
setInterval(sendImage, 3000);
}
// Taking pictures
function takePhoto() {
// get Canvas object
let video = document.getElementById('video');
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, 500, 500);
// toDataURL --- Can be introduced into 'image/png'--- Default , 'image/jpeg'
let img = document.getElementById('canvas').toDataURL();
// there img Is the picture you get
console.log('img-----', img);
document.getElementById('imgTag').src=img;
}
// Turn off camera
function closeMedia() {
mediaStreamTrack.stop();
}
// Send data to the back end
function sendImage(){
// Create asynchronous objects
var xhr = new XMLHttpRequest();
// Set the type of request and url
xhr.open('get', 'http://localhost:8000/sys/test', true);
//post The request must add a request header or an error will be reported
xhr.setRequestHeader("Content-type","application/json");
// Send a request
// xhr.send({image:document.getElementById('canvas').toDataURL()});
xhr.send();
xhr.onreadystatechange = function () {
// This step is to determine whether the server responds correctly
if (xhr.status == 200) {
console.log(xhr.responseText);
}
};
};
</script>
</body>边栏推荐
- 从MLPerf谈起:如何引领AI加速器的下一波浪潮
- Importance of EDA tools to chip industry knowledge popularization
- 能升职加薪?PMP证书含金量浅析
- pytest合集(2)— pytest運行方式
- 天气预报小程序源码 天气类微信小程序源码
- 个人炒股怎样开户?安全吗。
- MQ学习笔记
- Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
- Can I choose to open an account for stock trading on flush? Is it safe?
- 基础—io密集型计算和cpu密集型计算
猜你喜欢
随机推荐
EMC-电路保护器件-防浪涌及冲击电流用
2022熔化焊接与热切割上岗证题目模拟考试平台操作
杰理之、产线装配环节【篇】
NIO与传统IO的区别
个人炒股怎样开户?安全吗。
能升职加薪?PMP证书含金量浅析
leetcode刷题:二叉树01(二叉树的前序遍历)
Internship: gradually moving towards project development
以飞地园区为样本,看雨花与韶山如何奏响长株潭一体化发展高歌
薛定谔的日语学习小程序源码
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
Détection des cibles - série Yolo
【Leetcode】最大连续1的个数
想请教一下,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
Difference and use between require and import
杰理之蓝牙耳机品控和生产技巧【篇】
基础—io密集型计算和cpu密集型计算
联想电脑怎么连接蓝牙耳机?
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
leetcode刷题:栈与队列05(逆波兰表达式求值)









