1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| <script> export default { props: { showVideo: { default: false } }, data() { return { internalShowVideo: this.showVideo, internalIsAfresh: false }; }, watch: { showVideo: { immediate: true, handler(value) { if (value) { this.getUserMedia() } } }
},
mounted() {
}, methods: { getUserMedia() { const _this = this; _this.internalShowVideo = true navigator.mediaDevices.getUserMedia({ video: { width: 1280, height: 720 } }) .then(function(stream) { _this.success(stream) }) .catch(function(err) { _this.$toast(err); }); }, success(stream) { this.$refs.video.srcObject = stream; this.$refs.video.play(); }, capture() { if (this.internalIsAfresh) { this.getUserMedia() this.internalIsAfresh = false } else { var videoElement = this.$refs.video; console.log(videoElement); this.internalShowVideo = false; this.$nextTick(() => { const videoWidth = videoElement.videoWidth; const videoHeight = videoElement.videoHeight; const canvas = this.$refs.canvas; const context = canvas.getContext('2d'); canvas.width = videoWidth; canvas.height = videoHeight; const x = (videoWidth - 900) / 2; const y = (videoHeight - 546) / 2; context.drawImage(videoElement, 0, 0, videoWidth, videoHeight, x, y, 900, 546); this.internalIsAfresh = true }) }
}, confirmImage() { const base64Url = this.$refs.canvas.toDataURL() const imgFile = this.convertBase64UrlToImgFile(base64Url, 'image/jpg')
this.internalShowVideo = false this.internalIsAfresh = false this.$emit('confirmImage', imgFile) }, convertBase64UrlToImgFile(urlData, fileType) { const imgData = urlData.split('base64,').splice(-1)[0] const bytes = window.atob(imgData)
const ab = new ArrayBuffer(bytes.length) const ia = new Int8Array(ab)
for (let i = 0; i < bytes.length; i++) { ia[i] = bytes.charCodeAt(i) }
const blob = new Blob([ab], { type: fileType })
const tempURL = URL.createObjectURL(blob);
blob.lastModifiedDate = new Date() blob.url = tempURL return blob } } } </script>
|