当前位置:网站首页>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>边栏推荐
猜你喜欢
随机推荐
Can I choose to open an account for stock trading on flush? Is it safe?
Talking from mlperf: how to lead the next wave of AI accelerator
【深度学习】利用深度学习监控女朋友的微信聊天?
旁路由设置的正确方式
leetcode刷题:栈与队列07(滑动窗口最大值)
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
leetcode刷题:栈与队列01(用栈实现队列)
php反射型xss,反射型XSS测试及修复
股票手机开户哪个app好,安全性较高的
Slf4j打印异常的堆栈信息
中通笔试题:翻转字符串,例如abcd打印出dcba
物联网rfid等
极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
杰理之关于长按开机检测抬起问题【篇】
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
目标检测——Yolo系列
月入1W+的自媒体达人都会用到的运营工具
PMP与NPDP之间的区别是什么?
能升职加薪?PMP证书含金量浅析









