uni-app小程序生成二维码传参
uni-app⼩程序⽣成⼆维码传参
<template>
<div>
// 显⽰⼆维码的容器
<canvas class='canvas'canvas-id="myQrcode"></canvas>
<button bindtap='tapHandler'data-code='555444333222111'>转为⼆维码</button>
</div>
</template>
<script>
// 导⼊外部JS库
import QRCode from'./../Utils/weapp-qrcode.js'
我国四大古典名著
export default{
data(){
return{
src:""
}
},
onLoad(){
setTimeout(()=>{
},500)
},
methods:{
// ⼆维码⽣成⼯具
couponQrCode(){
new QRCode('myQrcode',{
text:"www.baidu",
width:100,
height:100,
colorDark:"#333333",
colorLight:"#FFFFFF",
correctLevel: QRCode.CorrectLevel.H
})
}
}
}
</script>
<style>
.canvas{
width: 200upx;height: 200upx;
}
</style>
weapp-qrcode.js 复制后可⽤
var QRCode;
(function(){
/**
* Get the type by string length
*
* @private
* @param {String} sText
* @param {Number} nCorrectLevel
* @return {Number} type
*/
function_getTypeNumber(sText, nCorrectLevel){
var nType =1;
var length =_getUTF8Length(sText);
for(var i =0, len = QRCodeLimitLength.length; i <= len; i++){
var nLimit =0;
switch(nCorrectLevel){
case QRErrorCorrectLevel.L:
nLimit = QRCodeLimitLength[i][0];
break;
case QRErrorCorrectLevel.M:
nLimit = QRCodeLimitLength[i][1];
break;
case QRErrorCorrectLevel.Q:
nLimit = QRCodeLimitLength[i][2];
break;
case QRErrorCorrectLevel.H:tcpip协议详解
nLimit = QRCodeLimitLength[i][3];
break;
}
if(length <= nLimit){
break;
}else{
nType++;
}
}
if(nType > QRCodeLimitLength.length){
throw new Error("Too long data");
}
return nType;
}
function_getUTF8Length(sText){
var replacedText =encodeURI(sText).toString().replace(/\%[0-9a-fA-F]{2}/g,'a');
return replacedText.length +(replacedText.length != sText ?3:0);
}有志者事竟成举三个例子
function QR8bitByte(data){
this.data = data;
this.parsedData =[];
// Added to support UTF-8 Characters
for(var i =0, l =this.data.length; i < l; i++){
var byteArray =[];
var code =this.data.charCodeAt(i);
if(code >0x10000){
byteArray[0]=0xF0|((code &0x1C0000)>>>18);
byteArray[1]=0x80|((code &0x3F000)>>>12);
byteArray[2]=0x80|((code &0xFC0)>>>6);
byteArray[3]=0x80|(code &0x3F);
}else if(code >0x800){
中国著名的桥byteArray[0]=0xE0|((code &0xF000)>>>12);
byteArray[1]=0x80|((code &0xFC0)>>>6);
byteArray[2]=0x80|(code &0x3F);
}else if(code >0x80){
byteArray[0]=0xC0|((code &0x7C0)>>>6);
byteArray[1]=0x80|(code &0x3F);
}else{
byteArray[0]= code;
}
this.parsedData.push(byteArray);
}
this.parsedData = at.apply([],this.parsedData);
if(this.parsedData.length !=this.data.length){
this.parsedData.unshift(191);
this.parsedData.unshift(187);
this.parsedData.unshift(239);
}
}
}
QR8bitByte.prototype ={
getLength:function(buffer){
return this.parsedData.length;
我做了一项小实验300字},
write:function(buffer){
for(var i =0, l =this.parsedData.length; i < l; i++){
buffer.put(this.parsedData[i],8);
}
}
};
// QRCodeModel
function QRCodeModel(typeNumber, errorCorrectLevel){
this.dataCache =null;
this.dataList =[];
}
QRCodeModel.prototype ={
addData:function(data){var newData =new QR8bitByte(data);this.dataList.push(newData);this.dataCache =null;}, isDark:function(row, col){ if(row <0||duleCount <= row || col <0||duleCount <= col){throw new Error(row +","+ col);}
dules[row][col];
}, getModuleCount:function(){duleCount;}, make:function(){this.makeImpl(BestMaskPattern());}, makeImpl:function( test, maskPattern){
if(this.dataCache ==null){this.dataCache = CorrectLevel,this.dataList);}
this.mapData(this.dataCache, maskPattern);
}, setupPositionProbePattern:function(row, col){for(var r =-1; r <=7; r++){if(row + r <=-1||duleCount <= row + r)continue;for(var c =-1;
c <=7; c++){if(col + c <=-1||duleCount <= 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)){dules[row + r][col + c]=true;}dules[row + r][col + c]=false;}}}}, getBestMaskPattern:funct ion(){
var minLostPoint =0;var pattern =0;for(var i =0; i <8; i++){this.makeImpl(true, i);var lostPoint = LostPoint(this);if(i ==0|| minLostPo int > lostPoint){ minLostPoint = lostPoint; pattern = i;}}
return pattern;
}, createMovieClip:function(target_mc, instance_name, depth){
var qr_mc = ateEmptyMovieClip(instance_name, depth);var cs =1;this.make();for(var row =0; row &dules.length; row++){ var y = row * cs;for(var col =0; col &dules[row].length; col++){var x = col * cs;var dark =dules[row][col];if(dark){ qr_mc.beginFill(0,100); veTo(x, y); qr_mc.lineTo(x + cs, y); qr_mc.lineTo(x + cs, y + cs); qr_mc.lineTo(x, y + cs); dFill();}}}
return qr_mc;
}, setupTimingPattern:function(){
for(var r =8; r &duleCount -8; r++){
dules[r][6]!=null){continue;}
}
for(var c =8; c &duleCount -8; c++){
dules[6][c]!=null){continue;}
}
}, setupPositionAdjustPattern:function(){
var pos = peNumber);for(var i =0; i < pos.length; i++){
for(var j =0; j < pos.length; j++){
var row = pos[i];var col = pos[j];dules[row][col]!=null){continue;}
for(var r =-2; r <=2; r++){for(var c =-2; c <=2; c++){if(r ==-2|| r ==2|| c ==-2|| c ==2||(r ==0&& c ==0)){dules[row + r][col + c]=true;}dules[row + r][col + c]=false;}}}
}
}
}
}, setupTypeNumber:function(test){
var bits = peNumber);for(var i =0; i <18; i++){var mod =(!test &&((bits >> i)&1)==1);dules[Math.flo or(i /3)][i %duleCount -8-3]= mod;}
for(var i =0; i <18; i++){var mod =(!test &&((bits >> i)&1)==1);dules[i %duleCount -8-3][Math.floor(i /3)]= mod;} }, setupTypeInfo:function(test, maskPattern){
var data =(CorrectLevel <<3)| maskPattern;var bits = BCHTypeInfo(data);for(var i
=0; i <15; i++){var mod =(!test &&((bits >> i)&1)==1);if(i <6){dules[i][8]= mod;}else if(i <8){dules[i +1][8]= mod;}duleCount -15+ i][8]= mod;} }
for(var i =0; i <15; i++){var mod =(!test &&((bits >> i)&1)==1);if(i <8){dules[8][duleCount - i -1]= mod;}else if(i <9){this. modules[8][15- i -1+1]= mod;}dules[8][15- i -1]= mod;}}
}, mapData:function(data, maskPattern){
var inc =-1;var row =duleCount -1;var bitIndex =7;var byteIndex =0;for(var col =duleCount -1; col >0; col -=2){
if(col ==6) col--;while(true){
for(var c =0; c <2; c++){
dules[row][col - c]==null){
var dark =false;if(byteIndex < data.length){ dark =(((data[byteIndex]>>> bitIndex)&1)==1);}
var mask = Mask(maskPattern, row, col - c);if(mask){ dark =!dark;}
}
}
row += inc;if(row <0||duleCount <= row){ row -= inc; inc =-inc;break;}
}
}
}
};
QRCodeModel.PAD0=0xEC;
QRCodeModel.PAD1=0x11;
var rsBlocks = RSBlocks(typeNumber, errorCorrectLevel);var buffer =new QRBitBuffer();for(var i =0; i < dataList.length; i++){var d ata = dataList[i]; buffer.de,4); buffer.Length(), de, typeNumber)); data.write(buffer);} var totalDataCount =0;for(var i =0; i < rsBlocks.length; i++){ totalDataCount += rsBlocks[i].dataCount;}
LengthInBits()> totalDataCount *8){
throw new Error("code length overflow. ("
+ LengthInBits()
+">"
+ totalDataCount *8
+")");
}
LengthInBits()+4<= totalDataCount *8){ buffer.put(0,4);}
LengthInBits()%8!=0){ buffer.putBit(false);}
while(true){
LengthInBits()>= totalDataCount *8){break;}
buffer.put(QRCodeModel.PAD0,8);LengthInBits()>= totalDataCount *8){break;}
buffer.put(QRCodeModel.PAD1,8);
}
ateBytes(buffer, rsBlocks);
};
var offset =0;var maxDcCount =0;var maxEcCount =0;var dcdata =new Array(rsBlocks.length);var ecdata =new Array(rsBlocks.length);for(var r =0; r < rsBlocks.length; r++){
var dcCount = rsBlocks[r].dataCount;var ecCount = rsBlocks[r].totalCount - dcCount; maxDcCount = Math.max(maxDcCount, dcCount); maxEcCou nt = Math.max(maxEcCount, ecCount); dcdata[r]=new Array(dcCount);for(var i =0; i < dcdata[r].length; i++){ dcdata[r][i]=0xff& buffer.buffer[i + offset]; }
offset += dcCount;var rsPoly = ErrorCorrectPolynomial(ecCount);var rawPoly =new QRPolynomial(dcdata[r], Length()-1);va r modPoly = d(rsPoly); ecdata[r]=new Length()-1);for(var i =0; i < ecdata[r].length; i++){var modIndex = i + Le ngth()- ecdata[r].length; ecdata[r][i]=(modIndex >=0)? (modIndex):0;}
}
var totalCodeCount =0;for(var i =0; i < rsBlocks.length; i++){ totalCodeCount += rsBlocks[i].totalCount;}
var data =new Array(totalCodeCount);var index =0;for(var i =0; i < maxDcCount; i++){for(var r =0; r < rsBlocks.length; r++){if(i < dcdata[r].lengt h){ data[index++]= dcdata[r][i];}}}
for(var i =0; i < maxEcCount; i++){for(var r =0; r < rsBlocks.length; r++){if(i < ecdata[r].length){ data[index++]= ecdata[r][i];}}}
return data;
};
var QRMode ={MODE_NUMBER:1<<0,MODE_ALPHA_NUM:1<<1,MODE_8BIT_BYTE:1<<2,MODE_KANJI:1<<3};
var QRMode ={MODE_NUMBER:1<<0,MODE_ALPHA_NUM:1<<1,MODE_8BIT_BYTE:1<<2,MODE_KANJI:1<<3};
var QRErrorCorrectLevel ={L:1,M:0,Q:3,H:2};
var QRMaskPattern ={PATTERN000:0,PATTERN001:1,PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PA TTERN111: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,6 2],[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,5 0,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),G1 5_MASK:(1<<14)|(1<<12)|(1<<10)|(1<<4)|(1<<1), getBCHTypeInfo:function(data){
var d = data <<10;BCHDigit(d)- BCHDigit(QRUtil.G15)>=0){ d ^=(QRUtil.G15<<(BCHDigit(d)- BCHDigit(QRUtil.G15)));}
return((data <<10)| d)^ QRUtil.G15_MASK;
}, getBCHTypeNumber:function(data){
var d = data <<12;BCHDigit(d)- BCHDigit(QRUtil.G18)>=0){ d ^=(QRUtil.G18<<(BCHDigit(d)- BCHDigit(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){s witch(maskPattern){case QRMaskPattern.PATTERN000:return(i + j)%2==0;case QRMaskPattern.PATTERN001:return i %2==0;case QRMaskPatt ern.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:"+ mask Pattern);}}, getErrorCorrectPolynomial:function(errorCorrectLength){
var a =new QRPolynomial([1],0);for(var i =0; i < errorCorrectLength; i++){ a = a.multiply(new QRPolynomial([1, p(i)],0));}
高云翔案件return a;
}, getLengthInBits:function(mode, type){if(1<= type && type <10){switch(mode){case QRMode.MODE_NUMBER:return10;case QRMode.MO DE_ALPHA_NUM:return9;case QRMode.MODE_8BIT_BYTE:return8;case QRMode.MODE_KANJI:return8;default:throw new Error("mode:"+ mode) ;}}else if(type <27){switch(mode){case QRMode.MODE_NUMBER:return12;case QRMode.MODE_ALPHA_NUM:return11;case QRMode.MODE_ 8BIT_BYTE:return16;case QRMode.MODE_KANJI:return10;default:throw new Error("mode:"+ mode);}}else if(type <41){switch(mode){case QR Mode.MODE_NUMBER:return14;case QRMode.MODE_ALPHA_NUM:return13;case QRMode.MODE_8BIT_BYTE:return16;case QRMode.MODE_K ANJI:return12;default:throw new Error("mode:"+ mode);}}else{throw new Error("type:"+ type);}}, getLostPoint:function(qrCode){ var moduleCount = ModuleCount();var lostPoint =0;for(var row =0; row < moduleCount; row++){
for(var col =0; col < moduleCount; col++){
var sameCount =0;var dark = qrCode.isDark(row, col);for(var r =-1; r <=1; r++){
if(row + r <0|| moduleCount <= row + r){continue;}
for(var c =-1; c <=1; c++){
if(col + c <0|| moduleCount <= col + c){continue;}
if(r ==0&& c ==0){continue;}
if(dark == qrCode.isDark(row + r, col + c)){ sameCount++;}
}
}
if(sameCount >5){ lostPoint +=(3+ sameCount -5);}
}
}
for(var row =0; row < moduleCount -1; row++){for(var col =0; col < moduleCount -1; col++){var count
=0;if(qrCode.isDark(row, col)) count++ ;if(qrCode.isDark(row +1, col)) count++;if(qrCode.isDark(row, col +1)) count++;if(qrCode.isDark(row +1, col +1)) count++;if(count ==0|| count ==4) { lostPoint +=3;}}}
for(var row =0; row < moduleCount; row++){for(var col =0; col < moduleCount -6; col++){if(qrCode.isDark(row, col)&&!qrCode.isDark(row, co l +1)&& qrCode.isDark(row, col +2)&& qrCode.isDark(row, col +3)&& qrCode.isDark(row, col +4)&&!qrCode.isDark(row, col +5)&& qrCode.isDark(ro w, col +6)){ lostPoint +=40;}}}
for(var col =0; col < moduleCount; col++){for(var row =0; row < moduleCount -6; row++){if(qrCode.isDark(row, col)&&!qrCode.isDark(row + 1, col)&& qrCode.isDark(row +2, col)&& qrCode.isDark(row +3, col)&& qrCode.isDark(row +4, col)&&!qrCode.isDark(row +5, col)&& qrCode.isDark(ro w +6, col)){ lostPoint +=40;}}}
var darkCount =0;for(var col =0; col < moduleCount; col++){for(var row =0; row < moduleCount; row++){if(qrCode.isDark(row, col)){ darkCo unt++;}}}
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 +")");}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。