| 12
 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
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 
 | Vue.component('scanner-box',{props: {
 
 viewportWidth:{
 type: Number
 },
 viewportHeight:{
 type: Number
 },
 
 scanWidth:{
 type: Number
 },
 scanHeight:{
 type: Number
 }
 },
 data(){
 return {
 
 cameraWidth: 0,
 cameraHeight: 0,
 canvas1Width: 0,
 canvas1Height: 0
 }
 },
 methods: {
 handleImageSize(video,item1,item2){
 let videoWidth = this.cameraWidth,
 videoHeight = this.cameraHeight,
 viewWidth = this.viewportWidth,
 viewHeight = this.viewportHeight,
 scanW = this.scanWidth,
 scanH = this.scanHeight,
 w,h;
 let context1 = item1.getContext("2d"),
 context2 = item2.getContext("2d");
 if (viewWidth/viewHeight > videoWidth/videoHeight){
 
 w = viewWidth,h = parseInt(w/(videoWidth/videoHeight));
 }else if (viewWidth/viewHeight < videoWidth/videoHeight){
 
 h = viewHeight,w = parseInt(h*(videoWidth/videoHeight));
 }else {
 
 w = viewWidth,h = viewHeight;
 }
 this.canvas1Width = w;
 this.canvas1Height = h;
 this.$nextTick(() => {
 context1.drawImage(video,0,0,videoWidth,videoHeight,0,0,w,h);
 context2.drawImage(item1,(w/2)-(scanW/2),(h/2)-(scanH/2),scanW,scanH,0,0,scanW,scanH);
 })
 
 },
 initVideo(constrains){
 let _this = this;
 if(navigator.mediaDevices.getUserMedia){
 
 navigator.mediaDevices.getUserMedia(constrains).then(_this.videoSuccess).catch(_this.videoError);
 } else if (navigator.webkitGetUserMedia){
 
 navigator.webkitGetUserMedia(constrains).then(_this.videoSuccess).catch(_this.videoError);
 } else if (navigator.mozGetUserMedia){
 
 navagator.mozGetUserMedia(constrains).then(_this.videoSuccess).catch(_this.videoError);
 } else if (navigator.getUserMedia){
 
 navigator.getUserMedia(constrains).then(_this.videoSuccess).catch(_this.videoError);
 }
 },
 videoSuccess(stream){
 let video = this.$refs.video,
 _this = this;
 
 video.srcObject = stream;
 
 video.play();
 video.oncanplay = function () {
 
 console.log('摄像头分辨率');
 console.log(video.videoWidth + 'x' + video.videoHeight);
 
 console.log('视口分辨率');
 console.log(window.innerWidth + 'x' + window.innerHeight);
 _this.cameraWidth = video.videoWidth;
 _this.cameraHeight = video.videoHeight;
 
 _this.readImg();
 };
 },
 videoError(error){
 console.log("访问用户媒体设备失败:",error.name,error.message);
 },
 readImg(){
 let video = this.$refs.video,
 canvas1 = this.$refs.canvas1,
 context = canvas1.getContext("2d"),
 canvas2 = this.$refs.canvas2,
 context2 = canvas2.getContext("2d"),
 _this = this;
 
 let timer = setInterval(function () {
 _this.handleImageSize(video,canvas1,canvas2);
 
 let imgUri = canvas2.toDataURL();
 _this.readBarcode(imgUri,timer);
 
 
 let imageData = context2.getImageData(0, 0, _this.scanWidth, _this.scanHeight);
 _this.readQrcode(imageData.data,timer);
 },500)
 },
 readBarcode(imgBase64,timer){
 let _this = this;
 Quagga.decodeSingle({
 decoder: {
 readers: ["code_128_reader"]
 },
 
 locate: false,
 src: imgBase64
 }, function(result){
 if (result){
 if(result.codeResult){
 
 
 _this.$emit('ondata',{ type: 'barcode', result:  result.codeResult.code});
 }else {
 console.log("正在扫条形码...not detected");
 }
 }else {
 console.log("正在扫条形码...not detected");
 }
 });
 },
 readQrcode(data,timer){
 let _this = this;
 let code = jsQR(data, _this.scanWidth, _this.scanHeight, {
 
 inversionAttempts: "dontInvert",
 });
 
 if (code){
 
 _this.$emit('ondata',{ type: 'qrcode', result:  code.data});
 
 }else {
 console.log('正在扫二维码...');
 }
 }
 },
 mounted(){
 
 if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia){
 
 this.initVideo({
 video:{
 width: window.innerWidth*2,
 facingMode: {
 
 
 }
 }
 });
 } else {
 alert("你的浏览器不支持访问用户媒体设备");
 }
 
 Common.getOrientationChange(function (res) {
 console.log(res);
 if (res){
 window.location.reload();
 }
 });
 },
 template: `<div id="scanner">
 <div class="model">
 <div class="scanner-view">
 <div class="scanner-view-arrow arrow1"></div>
 <div class="scanner-view-arrow arrow2"></div>
 <div class="scanner-view-arrow arrow3"></div>
 <div class="scanner-view-arrow arrow4"></div>
 <div class="scanner-line"></div>
 </div>
 <div class="scanner-text">放入框内,自动扫描</div>
 </div>
 <audio id="audio" ref="audio" src="./css/success.mp3" style="width: 0px;height: 0px"></audio>
 <video class="video-view" ref="video" autoplay playsinline="true" webkit-playsinline="true"></video>
 <canvas ref="canvas1" :width="canvas1Width" :height="canvas1Height" style="display: none"></canvas>
 <canvas ref="canvas2" :width="scanWidth" :height="scanHeight" style="display: none"></canvas>
 </div>`
 });
 
 
 |