<style>
html,body{position:relative;height:100%;}
.main { display: flex; flex-direction: row; justify-content:space-between;width:100%}
#canvas{background-color:#eee;}
.btnBox { display:flex;align-items:center;justify-content:center; position: absolute; bottom: 0px; left: 0px; width: 100%; height: 60px; background-color: #eee; border-top: solid 1px #ddd; }
#btnSave{margin-left:20px;}
</style>
<div class="main">
<video id="video" autoplay style="width: 640px;height: 480px"></video>
<!--拍照按鈕-->
<!--描繪video截圖-->
<canvas id="canvas" width="640" height="480"></canvas>
</div>
<div class="text-center btnBox">
<button id="capture" class="btn btn-success"><i class="fa fa-camera"></i> 拍照</button>
<button id="btnSave" class="btn btn-success"><i class="fa fa-save"></i> 使用此圖片</button>
</div>
<script>
var qid =@ViewBag.qid;
window.onload = function () {
//訪問(wèn)用戶媒體設(shè)備的兼容方法
function getUserMedia(constraints, success, error) {
if (navigator.mediaDevices.getUserMedia) {
//最新的標(biāo)準(zhǔn)API
navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
} else if (navigator.webkitGetUserMedia) {
//webkit核心瀏覽器
navigator.webkitGetUserMedia(constraints, success, error)
} else if (navigator.mozGetUserMedia) {
//firfox瀏覽器
navigator.mozGetUserMedia(constraints, success, error);
} else if (navigator.getUserMedia) {
//舊版API
navigator.getUserMedia(constraints, success, error);
}
}
let video = document.getElementById('video');
let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
function success(stream) {
//兼容webkit核心瀏覽器
//let CompatibleURL = window.URL || window.webkitURL;
//將視頻流設(shè)置為video元素的源
// console.log(stream);
//video.src = CompatibleURL.createObjectURL(stream);
video.srcObject = stream;
video.play();
}
function error(error) {
console.log(`訪問(wèn)用戶媒體設(shè)備失敗${error.name}, ${error.message}`);
}
if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
console.log("enumerateDevices() not supported.");
return;
}
// 列出攝像頭和麥克風(fēng)
var exArray = [];
navigator.mediaDevices.enumerateDevices()
.then(function (devices) {
devices.forEach(function (device) {
// console.log(device.kind + ": " + device.label +
// " id = " + device.deviceId);
if (device.kind == "videoinput") {
// alert(device.label);
exArray.push(device.deviceId);
}
});
var mediaOpts = { video: { width: 420, height: 120 } };
var mediaOpts =
{
video:
{
deviceId: { exact: exArray[1] }
}
};
//調(diào)用用戶媒體設(shè)備, 訪問(wèn)攝像頭
getUserMedia(mediaOpts, success, error);
})
.catch(function (err) {
console.log(err.name + ": " + err.message);
});
} else {
alert('不支持訪問(wèn)用戶媒體');
}
var img = new Image();
document.getElementById('capture').addEventListener('click', function () {
context.drawImage(video, 0, 0, 640, 480);
//var image_data = context.getImageData(0, 0, 480, 320);
// var string = OCRAD(image_data.data);
// string = _arrayBufferToBase64(image_data);
var image = new Image();
image.src = canvas.toDataURL("image/jpeg");
console.log(image.src);
})
}
</script>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者