本篇文章给大家分享的是有关网络摄像头怎么利用JavaScript进行调用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
不支持IE浏览器(需要使用flash插件), 支持移动端, 未经过完全测试
PC端使用的时候, HTML页面需要预留video标签, canvas标签
移动端使用的时候, HTML页面需要预留file标签, canvas标签, img标签
(function (window, document) {
window.camera = {
init: function (options) {
/**
* options 属性示例
* videoID: video控件ID
* canvasID: canvas控件ID
* fileID: type为file的input控件的ID
* imageID: img控件的ID
* videoEnable: 是否启用摄像头
* audioEnable: 是否启用麦克风
* videoWidth: 视频宽度
* videoHeight: 视频高度
* photoWidth: 拍照宽度
* photoHeight: 拍照高度
*/
_options = options;
if (isMobileTerminal()) {
initMobileTerminal();
} else {
initComputerTerminal();
}
},
photo: function () {
if (isMobileTerminal()) {
photoMobileTerminal();
} else {
photoComputerTerminal();
}
}
};
let _options = null;
function initComputerTerminal() {
let videoDom = document.getElementById(_options.videoID);
if (!videoDom) {
alert('Video 控件无效');
return;
}
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件无效');
return;
}
canvasDom.setAttribute('width', _options.photoWidth + 'px');
canvasDom.setAttribute('height', _options.photoHeight + 'px');
let parameters = {
video: _options.videoEnable ? {
width: _options.videoWidth,
height: _options.videoHeight
} : false,
audio: _options.audioEnable
};
navigator.mediaDevices.getUserMedia(parameters)
.then(function (MediaStream) {
video.srcObject = MediaStream;
video.play();
}).catch(function (reason) {
console.log(reason);
alert(reason);
});
}
function photoComputerTerminal() {
let videoDom = document.getElementById(_options.videoID);
if (!videoDom) {
alert('Video 控件无效');
return;
}
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件无效');
return;
}
let context = canvasDom.getContext('2d');
context.drawImage(videoDom, 0, 0, _options.photoWidth, _options.photoHeight);
}
function initMobileTerminal() {
let fileDom = document.getElementById(_options.fileID);
if (!fileDom) {
alert('File 控件无效');
return;
}
fileDom.setAttribute('accept', 'image/*');
fileDom.setAttribute('capture', 'camera');
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件无效');
return;
}
canvasDom.setAttribute('width', _options.photoWidth + 'px');
canvasDom.setAttribute('height', _options.photoHeight + 'px');
let imageDom = document.getElementById(_options.imageID);
if (!imageDom) {
alert('Image 控件无效');
return;
}
fileDom.addEventListener('change', function () {
let file = fileDom.files[0];
let reader = new FileReader();
reader.onloadend = function () {
imageDom.setAttribute('src', reader.result);
setTimeout(function () {
let context = canvas.getContext("2d");
context.drawImage(imageDom, 0, 0, _options.photoWidth, _options.photoHeight);
}, 300);
};
reader.readAsDataURL(file);
});
}
function photoMobileTerminal() {
let fileDom = document.getElementById(_options.fileID);
fileDom.click();
}
function isMobileTerminal() {
if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || /Mobile/.test(navigator.userAgent) || /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))
return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
return false;
}
})(window, document);
以上就是网络摄像头怎么利用JavaScript进行调用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。