let QRCode={}; (function(){ function unicodeFormat8(code){ var c0,c1,c2; if(code<128){ return[code]; }else if(code<2048){ c0=192+(code>>6); c1=128+(code&63); return[c0,c1]; }else{ c0=224+(code>>12); c1=128+(code>>6&63); c2=128+(code&63); return[c0,c1,c2]; } } function getUTF8Bytes(string){ var utf8codes=[]; for(var i=0;i=7){ this.setupTypeNumber(true); } this.mapData(this.dataCache,maskPattern); }, setupPositionProbePattern:function(row,col){ for(var r=-1;r<=7;r++){ if(row+r<=-1||this.moduleCount<=row+r)continue; for(var c=-1;c<=7;c++){ if(col+c<=-1||this.moduleCount<=col+c)continue; if((0<=r&&r<=6&&(c==0||c==6))||(0<=c&&c<=6&&(r==0||r==6))||(2<=r&&r<=4&&2<=c&&c<=4)){ this.modules[row+r][col+c]=true; }else{ this.modules[row+r][col+c]=false; } } } }, createQrcode:function(){ var minLostPoint=0; var pattern=0; var bestModules=null; for(var i=0;i<8;i++){ this.makeImpl(i); var lostPoint=QRUtil.getLostPoint(this); if(i==0||minLostPoint>lostPoint){ minLostPoint=lostPoint; pattern=i; bestModules=this.modules; } } this.modules=bestModules; this.setupTypeInfo(false,pattern); if(this.typeNumber>=7){ this.setupTypeNumber(false); } }, setupTimingPattern:function(){ for(var r=8;r>i)&1)==1); this.modules[Math.floor(i/3)][i%3+this.moduleCount-8-3]=mod; this.modules[i%3+this.moduleCount-8-3][Math.floor(i/3)]=mod; } }, setupTypeInfo:function(test,maskPattern){ var data=(QRErrorCorrectLevel[this.errorCorrectLevel]<<3)|maskPattern; var bits=QRUtil.getBCHTypeInfo(data); for(var i=0;i<15;i++){ var mod=(!test&&((bits>>i)&1)==1); if(i<6){ this.modules[i][8]=mod; }else if(i<8){ this.modules[i+1][8]=mod; }else{ this.modules[this.moduleCount-15+i][8]=mod; } var mod=(!test&&((bits>>i)&1)==1); if(i<8){ this.modules[8][this.moduleCount-i-1]=mod; }else if(i<9){ this.modules[8][15-i-1+1]=mod; }else{ this.modules[8][15-i-1]=mod; } } this.modules[this.moduleCount-8][8]=(!test); }, createData:function(){ var buffer=new QRBitBuffer(); var lengthBits=this.typeNumber>9?16:8; buffer.put(4,4); buffer.put(this.utf8bytes.length,lengthBits); for(var i=0,l=this.utf8bytes.length;i=this.totalDataCount*8){ break; } buffer.put(QRCodeAlg.PAD0,8); if(buffer.length>=this.totalDataCount*8){ break; } buffer.put(QRCodeAlg.PAD1,8); } return this.createBytes(buffer); }, createBytes:function(buffer){ var offset=0; var maxDcCount=0; var maxEcCount=0; var length=this.rsBlock.length/3; var rsBlocks=new Array(); for(var i=0;i=0)?modPoly.get(modIndex):0; } } var data=new Array(this.totalDataCount); var index=0; for(var i=0;i0;col-=2){ if(col==6)col--; while(true){ for(var c=0;c<2;c++){ if(this.modules[row][col-c]==null){ var dark=false; if(byteIndex>>bitIndex)&1)==1); } var mask=QRUtil.getMask(maskPattern,row,col-c); if(mask){ dark=!dark; } this.modules[row][col-c]=dark; bitIndex--; if(bitIndex==-1){ byteIndex++; bitIndex=7; } } } row+=inc; if(row<0||this.moduleCount<=row){ row-=inc; inc=-inc; break; } } } } }; QRCodeAlg.PAD0=0xEC; QRCodeAlg.PAD1=0x11; var QRErrorCorrectLevel=[1,0,3,2]; var QRMaskPattern={ PATTERN000:0, PATTERN001:1, PATTERN010:2, PATTERN011:3, PATTERN100:4, PATTERN101:5, PATTERN110:6, PATTERN111:7 }; var QRUtil={ PATTERN_POSITION_TABLE:[ [], [6,18], [6,22], [6,26], [6,30], [6,34], [6,22,38], [6,24,42], [6,26,46], [6,28,50], [6,30,54], [6,32,58], [6,34,62], [6,26,46,66], [6,26,48,70], [6,26,50,74], [6,30,54,78], [6,30,56,82], [6,30,58,86], [6,34,62,90], [6,28,50,72,94], [6,26,50,74,98], [6,30,54,78,102], [6,28,54,80,106], [6,32,58,84,110], [6,30,58,86,114], [6,34,62,90,118], [6,26,50,74,98,122], [6,30,54,78,102,126], [6,26,52,78,104,130], [6,30,56,82,108,134], [6,34,60,86,112,138], [6,30,58,86,114,142], [6,34,62,90,118,146], [6,30,54,78,102,126,150], [6,24,50,76,102,128,154], [6,28,54,80,106,132,158], [6,32,58,84,110,136,162], [6,26,54,82,110,138,166], [6,30,58,86,114,142,170] ], G15:(1<<10)|(1<<8)|(1<<5)|(1<<4)|(1<<2)|(1<<1)|(1<<0), G18:(1<<12)|(1<<11)|(1<<10)|(1<<9)|(1<<8)|(1<<5)|(1<<2)|(1<<0), G15_MASK:(1<<14)|(1<<12)|(1<<10)|(1<<4)|(1<<1), getBCHTypeInfo:function(data){ var d=data<<10; while(QRUtil.getBCHDigit(d)-QRUtil.getBCHDigit(QRUtil.G15)>=0){ d^=(QRUtil.G15<<(QRUtil.getBCHDigit(d)-QRUtil.getBCHDigit(QRUtil.G15))); } return((data<<10)|d)^QRUtil.G15_MASK; }, getBCHTypeNumber:function(data){ var d=data<<12; while(QRUtil.getBCHDigit(d)-QRUtil.getBCHDigit(QRUtil.G18)>=0){ d^=(QRUtil.G18<<(QRUtil.getBCHDigit(d)-QRUtil.getBCHDigit(QRUtil.G18))); } return(data<<12)|d; }, getBCHDigit:function(data){ var digit=0; while(data!=0){ digit++; data>>>=1; } return digit; }, getPatternPosition:function(typeNumber){ return QRUtil.PATTERN_POSITION_TABLE[typeNumber-1]; }, getMask:function(maskPattern,i,j){ switch(maskPattern){ case QRMaskPattern.PATTERN000: return(i+j)%2==0; case QRMaskPattern.PATTERN001: return i%2==0; case QRMaskPattern.PATTERN010: return j%3==0; case QRMaskPattern.PATTERN011: return(i+j)%3==0; case QRMaskPattern.PATTERN100: return(Math.floor(i/2)+Math.floor(j/3))%2==0; case QRMaskPattern.PATTERN101: return(i*j)%2+(i*j)%3==0; case QRMaskPattern.PATTERN110: return((i*j)%2+(i*j)%3)%2==0; case QRMaskPattern.PATTERN111: return((i*j)%3+(i+j)%2)%2==0; default: throw new Error("bad maskPattern:"+maskPattern); } }, getErrorCorrectPolynomial:function(errorCorrectLength){ var a=new QRPolynomial([1],0); for(var i=0;i3){ if(qrCode.modules[row][col-1]&&qrCode.modules[row][col-2]&&qrCode.modules[row][col-3]&&qrCode.modules[row][col-4]){ lostPoint+=40; } } } } if((row=5){ lostPoint+=(3+sameCount-5); } sameCount=1; } if(current){ darkCount++; } } } for(var col=0;col3){ if(qrCode.modules[row-1][col]&&qrCode.modules[row-2][col]&&qrCode.modules[row-3][col]&&qrCode.modules[row-4][col]){ lostPoint+=40; } } } } if(head^current){ sameCount++; }else{ head=current; if(sameCount>=5){ lostPoint+=(3+sameCount-5); } sameCount=1; } } } var ratio=Math.abs(100*darkCount/moduleCount/moduleCount-50)/5; lostPoint+=ratio*10; return lostPoint; } }; var QRMath={ glog:function(n){ if(n<1){ throw new Error("glog("+n+")"); } return QRMath.LOG_TABLE[n]; }, gexp:function(n){ while(n<0){ n+=255; } while(n>=256){ n-=255; } return QRMath.EXP_TABLE[n]; }, EXP_TABLE:new Array(256), LOG_TABLE:new Array(256) }; for(var i=0;i<8;i++){ QRMath.EXP_TABLE[i]=1<=el){ var ratio=QRMath.glog(num[0])-QRMath.glog(e.get(0)); for(var i=0;i9?2:1; if(this.utf8bytes.length+lengthBytes>>(7-index%8))&1); }, put:function(num,length){ for(var i=0;i>>(length-i-1))&1)); } }, putBit:function(bit){ var bufIndex=Math.floor(this.length/8); if(this.buffer.length<=bufIndex){ this.buffer.push(0); } if(bit){ this.buffer[bufIndex]|=(0x80>>>(this.length%8)); } this.length++; } }; let qrcodeAlgObjCache=[]; QRCode=function(opt){ this.options={ text:'', size:256, correctLevel:3, background:'#ffffff', foreground:'#000000', pdground:'#000000', image:'', imageSize:30, canvasId:opt.canvasId, context:opt.context, usingComponents:opt.usingComponents, showLoading:opt.showLoading, loadingText:opt.loadingText, }; if(typeof opt==='string'){ opt={ text:opt }; } if(opt){ for(var i in opt){ this.options[i]=opt[i]; } } var qrCodeAlg=null; for(var i=0,l=qrcodeAlgObjCache.length;i1&&config.row<5&&config.col>1&&config.col<5)|| (config.row>(config.count-6)&&config.row<(config.count-2)&&config.col>1&&config.col<5)|| (config.row>1&&config.row<5&&config.col>(config.count-6)&&config.col<(config.count-2)) )){ return options.pdground; } return options.foreground; } let createCanvas=function(options){ if(options.showLoading){ uni.showLoading({ title:options.loadingText, mask:true }); } var ctx=uni.createCanvasContext(options.canvasId,options.context); var count=qrCodeAlg.getModuleCount(); var ratioSize=options.size; var ratioImgSize=options.imageSize; var tileW=(ratioSize/count).toPrecision(4); var tileH=(ratioSize/count).toPrecision(4); for(var row=0;row{ ctx.draw(true,()=>{ setTimeout(()=>{ uni.canvasToTempFilePath({ width:options.width, height:options.height, destWidth:options.width, destHeight:options.height, canvasId:options.canvasId, quality:Number(1), success:function(res){ if(options.cbResult){ options.cbResult(res.tempFilePath) } }, fail:function(res){ if(options.cbResult){ options.cbResult(res) } }, complete:function(){ if(options.showLoading){ uni.hideLoading(); } }, },options.context); },options.text.length+100); }); },options.usingComponents?0:150); } createCanvas(this.options); let empty=function(v){ let tp=typeof v, rt=false; if(tp=="number"&&String(v)==""){ rt=true }else if(tp=="undefined"){ rt=true }else if(tp=="object"){ if(JSON.stringify(v)=="{}"||JSON.stringify(v)=="[]"||v==null)rt=true }else if(tp=="string"){ if(v==""||v=="undefined"||v=="null"||v=="{}"||v=="[]")rt=true }else if(tp=="function"){ rt=false } return rt } }; QRCode.prototype.clear=function(fn){ var ctx=uni.createCanvasContext(this.options.canvasId,this.options.context) ctx.clearRect(0,0,this.options.size,this.options.size) ctx.draw(false,()=>{ if(fn){ fn() } }) }; })() export default QRCode