能否通过浏览器访问(苹果设备上)内置的相机?
最优解决方案是使用客户端javascript。希望避免使用Java或Flash。
能否通过浏览器访问(苹果设备上)内置的相机?
最优解决方案是使用客户端javascript。希望避免使用Java或Flash。
video
和标准的Javascript WebRTC 访问它们。var video = document.createElement('video');
video.setAttribute('playsinline', '');
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
video.style.width = '200px';
video.style.height = '200px';
/* Setting up the constraint */
var facingMode = "user"; // Can be 'user' or 'environment' to access back or front camera (NEAT!)
var constraints = {
audio: false,
video: {
facingMode: facingMode
}
};
/* Stream it to video element */
navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
video.srcObject = stream;
});
玩一玩吧。
有一个非常酷的解决方案,来自丹尼·马尔科夫。它使用navigator.getUserMedia方法,在现代浏览器中应该可以工作。我已经成功地在Firefox和Chrome上进行了测试。IE无法工作:
这是一个演示:
https://tutorialzine.github.io/pwa-photobooth/
链接到Danny Markov的描述页面:
http://tutorialzine.com/2016/09/everything-you-should-know-about-progressive-web-apps/
链接到GitHub:
是的,可以从浏览器访问相机。以下是对我有效的代码:
<html><head>
</head><body>
<video src="" ></video>
<br />
<button id='flipCamera'>Flip</button>
</body>
<script>
var front = false;
var video = document.querySelector('video');
document.getElementById('flipCamera').onclick = function() { front = !front; };
var constraints = { video: { facingMode: (front? "user" : "environment"), width: 640, height: 480 } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
video.srcObject = mediaStream;
video.onloadedmetadata = function(e) {
video.play();
};
})
.catch(function(err) { console.log(err.name + ": " + err.message); })
</script></html>
<video autoplay></video>
<script>
var onFailSoHard = function(e) {
console.log('Reeeejected!', e);
};
// Not showing vendor prefixes.
navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
// See crbug.com/110938.
video.onloadedmetadata = function(e) {
// Ready to go. Do some stuff.
};
}, onFailSoHard);
</script>
<style type="text/css">
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement {
width: 500px;
height: 375px;
background-color: #777;
}
</style>
<div id="container">
<video autoplay="true" id="videoElement"></video>
</div>
<script type="text/javascript">
var video = document.querySelector("#videoElement");
navigator.getUserMedia = navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia||navigator.oGetUserMedia;
if(navigator.getUserMedia) {
navigator.getUserMedia({video:true}, handleVideo, videoError);
}
function handleVideo(stream) {
video.srcObject=stream;
video.play();
}
function videoError(e) {
}
</script>
function loadCamera ()
{
let v = document.getElementById ("myvideo");
navigator.mediaDevices.getUserMedia({video:{width:v.width, height:v.height}})
.then((mediaStream) => { v.srcObject = mediaStream; })
.catch(e => { console.error(`${e.name}: ${e.message}`); });
v.play();
}
loadCamera ();
function makeVideo (height, width)
{
let video = document.createElement ("video");
video.height = height;
video.width = width;
video.autoplay = true;
video.playsInline = true;
video.muted = true;
video.loop = true;
return video;
}
async function loadCamera (height, width)
{
let v = makeVideo (height, width);
navigator.mediaDevices.getUserMedia({video:{width:width, height:height}})
.then((mediaStream) => { v.srcObject = mediaStream; })
.catch(e => { console.error(`${e.name}: ${e.message}`); });
v.play();
return v;
}
then
处理程序中添加任何处理loadCamera (180, 320).then(video => { document.body.appendChild(video); });
**SIMPLE JAVASCRIPT VANILLA **
var video = document.querySelector("#videoElement");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (err0r) {
console.log("Something went wrong!");
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
#container{
align-self: center;
margin-left: 350px;
align-items: center;
justify-content: center;
position: relative;
width: 1000px;
height: 1000px;
background-color: black;
padding: 3px;
}
#videoElement{
transform: rotate(90deg);
align-self: center;
height: 50a0px;
left: 20;
width: 700px;
position:absolute;
padding: 1px;
top: 120px;
}
</style>
</head>
<body>
<div id="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<script src="index.js">
</script>
</body>
</html>
geolocation
对象,它会先请求权限。 - Ayush