-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathqr.code.creator.js
1 lines (1 loc) · 11.4 KB
/
qr.code.creator.js
1
adelta=[0,11,15,19,23,27,31,16,18,20,22,24,26,28,20,22,24,24,26,28,28,22,24,24,26,26,28,28,24,24,26,26,26,28,28,24,26,26,26,28,28];vpat=[3220,1468,2713,1235,3062,1890,2119,1549,2344,2936,1117,2583,1330,2470,1667,2249,2028,3780,481,4011,142,3098,831,3445,592,2517,1776,2234,1951,2827,1070,2660,1345,3177];fmtword=[30660,29427,32170,30877,26159,25368,27713,26998,21522,20773,24188,23371,17913,16590,20375,19104,13663,12392,16177,14854,9396,8579,11994,11245,5769,5054,7399,6608,1890,597,3340,2107];eccblocks=[1,0,19,7,1,0,16,10,1,0,13,13,1,0,9,17,1,0,34,10,1,0,28,16,1,0,22,22,1,0,16,28,1,0,55,15,1,0,44,26,2,0,17,18,2,0,13,22,1,0,80,20,2,0,32,18,2,0,24,26,4,0,9,16,1,0,108,26,2,0,43,24,2,2,15,18,2,2,11,22,2,0,68,18,4,0,27,16,4,0,19,24,4,0,15,28,2,0,78,20,4,0,31,18,2,4,14,18,4,1,13,26,2,0,97,24,2,2,38,22,4,2,18,22,4,2,14,26,2,0,116,30,3,2,36,22,4,4,16,20,4,4,12,24,2,2,68,18,4,1,43,26,6,2,19,24,6,2,15,28,4,0,81,20,1,4,50,30,4,4,22,28,3,8,12,24,2,2,92,24,6,2,36,22,4,6,20,26,7,4,14,28,4,0,107,26,8,1,37,22,8,4,20,24,12,4,11,22,3,1,115,30,4,5,40,24,11,5,16,20,11,5,12,24,5,1,87,22,5,5,41,24,5,7,24,30,11,7,12,24,5,1,98,24,7,3,45,28,15,2,19,24,3,13,15,30,1,5,107,28,10,1,46,28,1,15,22,28,2,17,14,28,5,1,120,30,9,4,43,26,17,1,22,28,2,19,14,28,3,4,113,28,3,11,44,26,17,4,21,26,9,16,13,26,3,5,107,28,3,13,41,26,15,5,24,30,15,10,15,28,4,4,116,28,17,0,42,26,17,6,22,28,19,6,16,30,2,7,111,28,17,0,46,28,7,16,24,30,34,0,13,24,4,5,121,30,4,14,47,28,11,14,24,30,16,14,15,30,6,4,117,30,6,14,45,28,11,16,24,30,30,2,16,30,8,4,106,26,8,13,47,28,7,22,24,30,22,13,15,30,10,2,114,28,19,4,46,28,28,6,22,28,33,4,16,30,8,4,122,30,22,3,45,28,8,26,23,30,12,28,15,30,3,10,117,30,3,23,45,28,4,31,24,30,11,31,15,30,7,7,116,30,21,7,45,28,1,37,23,30,19,26,15,30,5,10,115,30,19,10,47,28,15,25,24,30,23,25,15,30,13,3,115,30,2,29,46,28,42,1,24,30,23,28,15,30,17,0,115,30,10,23,46,28,10,35,24,30,19,35,15,30,17,1,115,30,14,21,46,28,29,19,24,30,11,46,15,30,13,6,115,30,14,23,46,28,44,7,24,30,59,1,16,30,12,7,121,30,12,26,47,28,39,14,24,30,22,41,15,30,6,14,121,30,6,34,47,28,46,10,24,30,2,64,15,30,17,4,122,30,29,14,46,28,49,10,24,30,24,46,15,30,4,18,122,30,13,32,46,28,48,14,24,30,42,32,15,30,20,4,117,30,40,7,47,28,43,22,24,30,10,67,15,30,19,6,118,30,18,31,47,28,34,34,24,30,20,61,15,30];glog=[255,0,1,25,2,50,26,198,3,223,51,238,27,104,199,75,4,100,224,14,52,141,239,129,28,193,105,248,200,8,76,113,5,138,101,47,225,36,15,33,53,147,142,218,240,18,130,69,29,181,194,125,106,39,249,185,201,154,9,120,77,228,114,166,6,191,139,98,102,221,48,253,226,152,37,179,16,145,34,136,54,208,148,206,143,150,219,189,241,210,19,92,131,56,70,64,30,66,182,163,195,72,126,110,107,58,40,84,250,133,186,61,202,94,155,159,10,21,121,43,78,212,229,172,115,243,167,87,7,112,192,247,140,128,99,13,103,74,222,237,49,197,254,24,227,165,153,119,38,184,180,124,17,68,146,217,35,32,137,46,55,63,209,91,149,188,207,205,144,135,151,178,220,252,190,97,242,86,211,171,20,42,93,158,132,60,57,83,71,109,65,162,31,45,67,216,183,123,164,118,196,23,73,236,127,12,111,246,108,161,59,82,41,157,85,170,251,96,134,177,187,204,62,90,203,89,95,176,156,169,160,81,11,245,22,235,122,117,44,215,79,174,213,233,230,231,173,232,116,214,244,234,168,80,88,175];gexp=[1,2,4,8,16,32,64,128,29,58,116,232,205,135,19,38,76,152,45,90,180,117,234,201,143,3,6,12,24,48,96,192,157,39,78,156,37,74,148,53,106,212,181,119,238,193,159,35,70,140,5,10,20,40,80,160,93,186,105,210,185,111,222,161,95,190,97,194,153,47,94,188,101,202,137,15,30,60,120,240,253,231,211,187,107,214,177,127,254,225,223,163,91,182,113,226,217,175,67,134,17,34,68,136,13,26,52,104,208,189,103,206,129,31,62,124,248,237,199,147,59,118,236,197,151,51,102,204,133,23,46,92,184,109,218,169,79,158,33,66,132,21,42,84,168,77,154,41,82,164,85,170,73,146,57,114,228,213,183,115,230,209,191,99,198,145,63,126,252,229,215,179,123,246,241,255,227,219,171,75,150,49,98,196,149,55,110,220,165,87,174,65,130,25,50,100,200,141,7,14,28,56,112,224,221,167,83,166,81,162,89,178,121,242,249,239,195,155,43,86,172,69,138,9,18,36,72,144,61,122,244,245,247,243,251,235,203,139,11,22,44,88,176,125,250,233,207,131,27,54,108,216,173,71,142,0];var strinbuf=[],eccbuf=[],qrframe=[],framask=[],rlens=[];var version,width,neccblk1,neccblk2,datablkw,eccblkwid;var ecclevel=1;function setmask(a,c){var b;if(a>c){b=a;a=c;c=b}b=c;b*=c;b+=c;b>>=1;b+=a;framask[b]=1}function putalign(a,c){var b;qrframe[a+width*c]=1;for(b=-2;b<2;b++){qrframe[(a+b)+width*(c-2)]=1;qrframe[(a-2)+width*(c+b+1)]=1;qrframe[(a+2)+width*(c+b)]=1;qrframe[(a+b+1)+width*(c+2)]=1}for(b=0;b<2;b++){setmask(a-1,c+b);setmask(a+1,c-b);setmask(a-b,c-1);setmask(a+b,c+1)}}function modnn(a){while(a>=255){a-=255;a=(a>>8)+(a&255)}return a}var genpoly=[];function appendrs(f,g,d,a){var c,b,e;for(c=0;c<a;c++){strinbuf[d+c]=0}for(c=0;c<g;c++){e=glog[strinbuf[f+c]^strinbuf[d]];if(e!=255){for(b=1;b<a;b++){strinbuf[d+b-1]=strinbuf[d+b]^gexp[modnn(e+genpoly[a-b])]}}else{for(b=d;b<d+a;b++){strinbuf[b]=strinbuf[b+1]}}strinbuf[d+a-1]=e==255?0:gexp[modnn(e+genpoly[0])]}}function ismasked(a,c){var b;if(a>c){b=a;a=c;c=b}b=c;b+=c*c;b>>=1;b+=a;return framask[b]}function applymask(b){var a,e,d,c;switch(b){case 0:for(e=0;e<width;e++){for(a=0;a<width;a++){if(!((a+e)&1)&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 1:for(e=0;e<width;e++){for(a=0;a<width;a++){if(!(e&1)&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 2:for(e=0;e<width;e++){for(d=0,a=0;a<width;a++,d++){if(d==3){d=0}if(!d&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 3:for(c=0,e=0;e<width;e++,c++){if(c==3){c=0}for(d=c,a=0;a<width;a++,d++){if(d==3){d=0}if(!d&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 4:for(e=0;e<width;e++){for(d=0,c=((e>>1)&1),a=0;a<width;a++,d++){if(d==3){d=0;c=!c}if(!c&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 5:for(c=0,e=0;e<width;e++,c++){if(c==3){c=0}for(d=0,a=0;a<width;a++,d++){if(d==3){d=0}if(!((a&e&1)+!(!d|!c))&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 6:for(c=0,e=0;e<width;e++,c++){if(c==3){c=0}for(d=0,a=0;a<width;a++,d++){if(d==3){d=0}if(!(((a&e&1)+(d&&(d==c)))&1)&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break;case 7:for(c=0,e=0;e<width;e++,c++){if(c==3){c=0}for(d=0,a=0;a<width;a++,d++){if(d==3){d=0}if(!(((d&&(d==c))+((a+e)&1))&1)&&!ismasked(a,e)){qrframe[a+e*width]^=1}}}break}return}var N1=3,N2=3,N3=40,N4=10;function badruns(c){var b;var a=0;for(b=0;b<=c;b++){if(rlens[b]>=5){a+=N1+rlens[b]-5}}for(b=3;b<c-1;b+=2){if(rlens[b-2]==rlens[b+2]&&rlens[b+2]==rlens[b-1]&&rlens[b-1]==rlens[b+1]&&rlens[b-1]*3==rlens[b]&&(rlens[b-3]==0||b+3>c||rlens[b-3]*3>=rlens[b]*4||rlens[b+3]*3>=rlens[b]*4)){a+=N3}}return a}function badcheck(){var d,j,g,c,f;var e=0;var i=0;for(j=0;j<width-1;j++){for(d=0;d<width-1;d++){if((qrframe[d+width*j]&&qrframe[(d+1)+width*j]&&qrframe[d+width*(j+1)]&&qrframe[(d+1)+width*(j+1)])||!(qrframe[d+width*j]||qrframe[(d+1)+width*j]||qrframe[d+width*(j+1)]||qrframe[(d+1)+width*(j+1)])){e+=N2}}}for(j=0;j<width;j++){rlens[0]=0;for(g=c=d=0;d<width;d++){if((f=qrframe[d+width*j])==c){rlens[g]++}else{rlens[++g]=1}c=f;i+=c?1:-1}e+=badruns(g)}if(i<0){i=-i}var a=i;count=0;a+=a<<2;a<<=1;while(a>width*width){a-=width*width,count++}e+=count*N4;for(d=0;d<width;d++){rlens[0]=0;for(g=c=j=0;j<width;j++){if((f=qrframe[d+width*j])==c){rlens[g]++}else{rlens[++g]=1}c=f}e+=badruns(g)}return e}function genframe(e){var g,f,b,l,h,d,c,a;l=e.length;version=0;do{version++;b=(ecclevel-1)*4+(version-1)*16;neccblk1=eccblocks[b++];neccblk2=eccblocks[b++];datablkw=eccblocks[b++];eccblkwid=eccblocks[b];b=datablkw*(neccblk1+neccblk2)+neccblk2-3+(version<=9);if(l<=b){break}}while(version<40);width=17+4*version;h=datablkw+(datablkw+eccblkwid)*(neccblk1+neccblk2)+neccblk2;for(l=0;l<h;l++){eccbuf[l]=0}strinbuf=e.slice(0);for(l=0;l<width*width;l++){qrframe[l]=0}for(l=0;l<(width*(width+1)+1)/2;l++){framask[l]=0}for(l=0;l<3;l++){b=0;f=0;if(l==1){b=(width-7)}if(l==2){f=(width-7)}qrframe[(f+3)+width*(b+3)]=1;for(g=0;g<6;g++){qrframe[(f+g)+width*b]=1;qrframe[f+width*(b+g+1)]=1;qrframe[(f+6)+width*(b+g)]=1;qrframe[(f+g+1)+width*(b+6)]=1}for(g=1;g<5;g++){setmask(f+g,b+1);setmask(f+1,b+g+1);setmask(f+5,b+g);setmask(f+g+1,b+5)}for(g=2;g<4;g++){qrframe[(f+g)+width*(b+2)]=1;qrframe[(f+2)+width*(b+g+1)]=1;qrframe[(f+4)+width*(b+g)]=1;qrframe[(f+g+1)+width*(b+4)]=1}}if(version>1){l=adelta[version];f=width-7;for(;;){g=width-7;while(g>l-3){putalign(g,f);if(g<l){break}g-=l}if(f<=l+9){break}f-=l;putalign(6,f);putalign(f,6)}}qrframe[8+width*(width-8)]=1;for(f=0;f<7;f++){setmask(7,f);setmask(width-8,f);setmask(7,f+width-7)}for(g=0;g<8;g++){setmask(g,7);setmask(g+width-8,7);setmask(g,width-8)}for(g=0;g<9;g++){setmask(g,8)}for(g=0;g<8;g++){setmask(g+width-8,8);setmask(8,g)}for(f=0;f<7;f++){setmask(8,f+width-7)}for(g=0;g<width-14;g++){if(g&1){setmask(8+g,6);setmask(6,8+g)}else{qrframe[(8+g)+width*6]=1;qrframe[6+width*(8+g)]=1}}if(version>6){l=vpat[version-7];b=17;for(g=0;g<6;g++){for(f=0;f<3;f++,b--){if(1&(b>11?version>>(b-12):l>>b)){qrframe[(5-g)+width*(2-f+width-11)]=1;qrframe[(2-f+width-11)+width*(5-g)]=1}else{setmask(5-g,2-f+width-11);setmask(2-f+width-11,5-g)}}}}for(f=0;f<width;f++){for(g=0;g<=f;g++){if(qrframe[g+width*f]){setmask(g,f)}}}h=strinbuf.length;for(d=0;d<h;d++){eccbuf[d]=strinbuf.charCodeAt(d)}strinbuf=eccbuf.slice(0);g=datablkw*(neccblk1+neccblk2)+neccblk2;if(h>=g-2){h=g-2;if(version>9){h--}}d=h;if(version>9){strinbuf[d+2]=0;strinbuf[d+3]=0;while(d--){l=strinbuf[d];strinbuf[d+3]|=255&(l<<4);strinbuf[d+2]=l>>4}strinbuf[2]|=255&(h<<4);strinbuf[1]=h>>4;strinbuf[0]=64|(h>>12)}else{strinbuf[d+1]=0;strinbuf[d+2]=0;while(d--){l=strinbuf[d];strinbuf[d+2]|=255&(l<<4);strinbuf[d+1]=l>>4}strinbuf[1]|=255&(h<<4);strinbuf[0]=64|(h>>4)}d=h+3-(version<10);while(d<g){strinbuf[d++]=236;strinbuf[d++]=17}genpoly[0]=1;for(d=0;d<eccblkwid;d++){genpoly[d+1]=1;for(c=d;c>0;c--){genpoly[c]=genpoly[c]?genpoly[c-1]^gexp[modnn(glog[genpoly[c]]+d)]:genpoly[c-1]}genpoly[0]=gexp[modnn(glog[genpoly[0]]+d)]}for(d=0;d<=eccblkwid;d++){genpoly[d]=glog[genpoly[d]]}b=g;f=0;for(d=0;d<neccblk1;d++){appendrs(f,datablkw,b,eccblkwid);f+=datablkw;b+=eccblkwid}for(d=0;d<neccblk2;d++){appendrs(f,datablkw+1,b,eccblkwid);f+=datablkw+1;b+=eccblkwid}f=0;for(d=0;d<datablkw;d++){for(c=0;c<neccblk1;c++){eccbuf[f++]=strinbuf[d+c*datablkw]}for(c=0;c<neccblk2;c++){eccbuf[f++]=strinbuf[(neccblk1*datablkw)+d+(c*(datablkw+1))]}}for(c=0;c<neccblk2;c++){eccbuf[f++]=strinbuf[(neccblk1*datablkw)+d+(c*(datablkw+1))]}for(d=0;d<eccblkwid;d++){for(c=0;c<neccblk1+neccblk2;c++){eccbuf[f++]=strinbuf[g+d+c*eccblkwid]}}strinbuf=eccbuf;g=f=width-1;b=h=1;a=(datablkw+eccblkwid)*(neccblk1+neccblk2)+neccblk2;for(d=0;d<a;d++){l=strinbuf[d];for(c=0;c<8;c++,l<<=1){if(128&l){qrframe[g+width*f]=1}do{if(h){g--}else{g++;if(b){if(f!=0){f--}else{g-=2;b=!b;if(g==6){g--;f=9}}}else{if(f!=width-1){f++}else{g-=2;b=!b;if(g==6){g--;f-=8}}}}h=!h}while(ismasked(g,f))}}strinbuf=qrframe.slice(0);l=0;f=30000;for(b=0;b<8;b++){applymask(b);g=badcheck();if(g<f){f=g;l=b}if(l==7){break}qrframe=strinbuf.slice(0)}if(l!=b){applymask(l)}f=fmtword[l+((ecclevel-1)<<3)];for(b=0;b<8;b++,f>>=1){if(f&1){qrframe[(width-1-b)+width*8]=1;if(b<6){qrframe[8+width*b]=1}else{qrframe[8+width*(b+1)]=1}}}for(b=0;b<7;b++,f>>=1){if(f&1){qrframe[8+width*(width-7+b)]=1;if(b){qrframe[(6-b)+width*8]=1}else{qrframe[7+width*8]=1}}}return qrframe}function makeQRCode(g,e,b,a,doc){if (!doc) doc = document; var d=doc.createElement("canvas");var h=d.getContext("2d");h.canvas.width=g;h.canvas.height=e;h.fillStyle="#eee";h.fillRect(0,0,g,e);qf=genframe(a);h.lineWidth=1;var f,c;px=g;if(e<g){px=e}px/=width+10;px=Math.round(px-0.5);h.clearRect(0,0,g,e);h.fillStyle="#fff";h.fillRect(0,0,px*(width+8),px*(width+8));h.fillStyle="#000";for(f=0;f<width;f++){for(c=0;c<width;c++){if(qf[c*width+f]){h.fillRect(px*(4+f),px*(4+c),px,px)}}}return d};