js調(diào)用網(wǎng)絡(luò)攝像頭的方法
不支持IE瀏覽器(需要使用flash插件), 支持移動(dòng)端, 未經(jīng)過(guò)完全測(cè)試
PC端使用的時(shí)候, HTML頁(yè)面需要預(yù)留video標(biāo)簽, canvas標(biāo)簽
移動(dòng)端使用的時(shí)候, HTML頁(yè)面需要預(yù)留file標(biāo)簽, canvas標(biāo)簽, img標(biāo)簽
(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: 是否啟用麥克風(fēng)
* 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 控件無(wú)效');
return;
}
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件無(wú)效');
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 控件無(wú)效');
return;
}
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件無(wú)效');
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 控件無(wú)效');
return;
}
fileDom.setAttribute('accept', 'image/*');
fileDom.setAttribute('capture', 'camera');
let canvasDom = document.getElementById(_options.canvasID);
if (!canvasDom) {
alert('Canvas 控件無(wú)效');
return;
}
canvasDom.setAttribute('width', _options.photoWidth + 'px');
canvasDom.setAttribute('height', _options.photoHeight + 'px');
let imageDom = document.getElementById(_options.imageID);
if (!imageDom) {
alert('Image 控件無(wú)效');
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);
以上就是js調(diào)用網(wǎng)絡(luò)攝像頭的方法的詳細(xì)內(nèi)容,更多關(guān)于js調(diào)用網(wǎng)絡(luò)攝像頭的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- js實(shí)現(xiàn)web調(diào)用攝像頭 js截取視頻畫(huà)面
- JS調(diào)用安卓手機(jī)攝像頭掃描二維碼
- js調(diào)用設(shè)備攝像頭的方法
- JS實(shí)現(xiàn)調(diào)用本地?cái)z像頭功能示例
- Vue2.0實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能 exif.js實(shí)現(xiàn)圖片上傳功能
- JavaScript 如何在瀏覽器中使用攝像頭
- 微信JSSDK實(shí)現(xiàn)打開(kāi)攝像頭拍照再將相片保存到服務(wù)器
- javascript實(shí)現(xiàn)攝像頭拍照預(yù)覽
相關(guān)文章
javascript數(shù)組中的concat方法和splice方法
這篇文章主要介紹了javascript數(shù)組中的concat方法和splice方法,concat方法作用合并數(shù)組,可以合并一個(gè)或多個(gè)數(shù)組,會(huì)返回合并數(shù)組之后的數(shù)據(jù),不會(huì)改變?cè)瓉?lái)的數(shù)組,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-03-03
利用NodeJS和PhantomJS抓取網(wǎng)站頁(yè)面信息以及網(wǎng)站截圖
這篇文章主要介紹了利用NodeJS和PhantomJS抓取網(wǎng)站頁(yè)面信息以及網(wǎng)站截圖的方法,提供實(shí)例代碼供大家參考2013-11-11
原生JS實(shí)現(xiàn)動(dòng)態(tài)加載js文件并在加載成功后執(zhí)行回調(diào)函數(shù)的方法
這篇文章主要介紹了原生JS實(shí)現(xiàn)動(dòng)態(tài)加載js文件并在加載成功后執(zhí)行回調(diào)函數(shù)的方法,涉及javascript動(dòng)態(tài)創(chuàng)建頁(yè)面元素、事件監(jiān)聽(tīng)、回調(diào)函數(shù)使用等相關(guān)操作技巧,需要的朋友可以參考下2018-09-09
一文總結(jié)JavaScript中常見(jiàn)的設(shè)計(jì)模式
在程序設(shè)計(jì)中有很多實(shí)用的設(shè)計(jì)模式,而其中大部分語(yǔ)言的實(shí)現(xiàn)都是基于“類(lèi)”。在程序設(shè)計(jì)中有很多實(shí)用的設(shè)計(jì)模式,而其中大部分語(yǔ)言的實(shí)現(xiàn)都是基于“類(lèi)”。,本文將總結(jié)了JavaScript中常見(jiàn)的十五種設(shè)計(jì)模式,感興趣的朋友可以參考下2023-05-05

