当前位置:网站首页>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>
边栏推荐
猜你喜欢
随机推荐
芭比Q了!新上架的游戏APP,咋分析?
Past and present life of product modular design
天气预报小程序源码 天气类微信小程序源码
2022年低压电工考试试题及答案
burpsuite简单抓包教程[通俗易懂]
An operation tool used by we media professionals who earn 1w+ a month
【深度学习】利用深度学习监控女朋友的微信聊天?
《軟件工程導論(第六版)》 張海藩 複習筆記
There are four ways to write switch, you know
旁路由设置的正确方式
目標檢測——Yolo系列
朋友圈社区程序源码分享
新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码
PWN攻防世界cgpwn2
目标检测——Yolo系列
深度学习 神经网络基础
【级联分类器训练参数】Training Haar Cascades
Internship: gradually moving towards project development
同花顺股票开户选哪个券商好手机开户是安全么?
从20s优化到500ms,我用了这三招