使用Java和HTML获取媒体设备
我有一个检测到摄像机的网页模板,有人可以帮助我添加一个按钮从相机中捕获,并且该捕获显示了文本框中的图像数据URL,我尝试添加此图像,但不能将其集成到JS代码。 这是我尝试插入的内容。也许那是不正确的代码... :(
> <button id="click-photo">Take Photo</button> <div
> id="dataurl-container">
> <canvas id="canvas" width="800%" height="600"></canvas>
> <div id="dataurl-header">Image Data URL</div>
> <textarea id="dataurl" readonly></textarea> </div>
>
> JS -->
>
> click_button.addEventListener('click', function() {
> canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
> let image_data_url = canvas.toDataURL('image/jpeg');
>
> dataurl.value = image_data_url;
> dataurl_container.style.display = 'block';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Camera selection</title>
<style>
html,
body {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
font-family: Helvetica, Arial, sans-serif;
min-height: 100%;
display: flex;
flex-direction: column;
}
header {
background: #f0293e;
color: #fff;
text-align: center;
}
main {
background: #ffffff;
min-height: 80vh;
flex-grow: 1;
}
.controls {
text-align: center;
padding: 0.5em 0;
background: #333e5a;
}
.controls {
color: #fff;
}
.controls p {
margin: 0;
}
.controls > div {
margin-bottom: 0.5em;
}
.controls > div:last-child {
margin-bottom: 0;
}
video {
width: 100%;
max-width: 600px;
display: block;
margin: 0 auto;
}
footer {
background: #333e5a;
color: #fff;
text-align: center;
}
footer a {
color: #fff;
}
</style>
</head>
<body>
<header>
<h1>Camera test</h1>
</header>
<main>
<div class="controls">
<button id="button">Get camera</button>
<select id="select">
<option></option>
</select>
</div>
<video id="video" autoplay playsinline></video>
</main>
<script>
const video = document.getElementById('video');
const button = document.getElementById('button');
const select = document.getElementById('select');
let click_button = document.querySelector("#click-photo");
let canvas = document.querySelector("#canvas");
let dataurl = document.querySelector("#dataurl");
let currentStream;
function stopMediaTracks(stream) {
stream.getTracks().forEach(track => {
track.stop();
});
}
function gotDevices(mediaDevices) {
select.innerHTML = '';
select.appendChild(document.createElement('option'));
let count = 1;
mediaDevices.forEach(mediaDevice => {
if (mediaDevice.kind === 'videoinput') {
const option = document.createElement('option');
option.value = mediaDevice.deviceId;
const label = mediaDevice.label || `Camera ${count++}`;
const textNode = document.createTextNode(label);
option.appendChild(textNode);
select.appendChild(option);
}
});
}
button.addEventListener('click', event => {
if (typeof currentStream !== 'undefined') {
stopMediaTracks(currentStream);
}
const videoConstraints = {};
if (select.value === '') {
videoConstraints.facingMode = 'environment';
} else {
videoConstraints.deviceId = { exact: select.value };
}
const constraints = {
video: videoConstraints,
audio: false
};
navigator.mediaDevices
.getUserMedia(constraints)
.then(stream => {
currentStream = stream;
video.srcObject = stream;
return navigator.mediaDevices.enumerateDevices();
})
.then(gotDevices)
.catch(error => {
console.error(error);
});
});
navigator.mediaDevices.enumerateDevices().then(gotDevices);
</script>
</body>
</html>
I have a web page template that detects cameras, could someone please help me add a button to take capture from the camera, and that capture shows the image data URL in the text box, I tried to add this but can't integrate that to JS code.
Here is what I tried to insert. Maybe that is the incorrect code... :(
> <button id="click-photo">Take Photo</button> <div
> id="dataurl-container">
> <canvas id="canvas" width="800%" height="600"></canvas>
> <div id="dataurl-header">Image Data URL</div>
> <textarea id="dataurl" readonly></textarea> </div>
>
> JS -->
>
> click_button.addEventListener('click', function() {
> canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
> let image_data_url = canvas.toDataURL('image/jpeg');
>
> dataurl.value = image_data_url;
> dataurl_container.style.display = 'block';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Camera selection</title>
<style>
html,
body {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
font-family: Helvetica, Arial, sans-serif;
min-height: 100%;
display: flex;
flex-direction: column;
}
header {
background: #f0293e;
color: #fff;
text-align: center;
}
main {
background: #ffffff;
min-height: 80vh;
flex-grow: 1;
}
.controls {
text-align: center;
padding: 0.5em 0;
background: #333e5a;
}
.controls {
color: #fff;
}
.controls p {
margin: 0;
}
.controls > div {
margin-bottom: 0.5em;
}
.controls > div:last-child {
margin-bottom: 0;
}
video {
width: 100%;
max-width: 600px;
display: block;
margin: 0 auto;
}
footer {
background: #333e5a;
color: #fff;
text-align: center;
}
footer a {
color: #fff;
}
</style>
</head>
<body>
<header>
<h1>Camera test</h1>
</header>
<main>
<div class="controls">
<button id="button">Get camera</button>
<select id="select">
<option></option>
</select>
</div>
<video id="video" autoplay playsinline></video>
</main>
<script>
const video = document.getElementById('video');
const button = document.getElementById('button');
const select = document.getElementById('select');
let click_button = document.querySelector("#click-photo");
let canvas = document.querySelector("#canvas");
let dataurl = document.querySelector("#dataurl");
let currentStream;
function stopMediaTracks(stream) {
stream.getTracks().forEach(track => {
track.stop();
});
}
function gotDevices(mediaDevices) {
select.innerHTML = '';
select.appendChild(document.createElement('option'));
let count = 1;
mediaDevices.forEach(mediaDevice => {
if (mediaDevice.kind === 'videoinput') {
const option = document.createElement('option');
option.value = mediaDevice.deviceId;
const label = mediaDevice.label || `Camera ${count++}`;
const textNode = document.createTextNode(label);
option.appendChild(textNode);
select.appendChild(option);
}
});
}
button.addEventListener('click', event => {
if (typeof currentStream !== 'undefined') {
stopMediaTracks(currentStream);
}
const videoConstraints = {};
if (select.value === '') {
videoConstraints.facingMode = 'environment';
} else {
videoConstraints.deviceId = { exact: select.value };
}
const constraints = {
video: videoConstraints,
audio: false
};
navigator.mediaDevices
.getUserMedia(constraints)
.then(stream => {
currentStream = stream;
video.srcObject = stream;
return navigator.mediaDevices.enumerateDevices();
})
.then(gotDevices)
.catch(error => {
console.error(error);
});
});
navigator.mediaDevices.enumerateDevices().then(gotDevices);
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论