From 3a016e4d44b187089f34d7564a1846bfd083712e Mon Sep 17 00:00:00 2001 From: robinlinus Date: Mon, 20 Nov 2017 00:26:45 +0100 Subject: [PATCH] Most hacky prototype --- .gitignore | 3 + index.html | 173 ++++++++++++++++++++++++++++++++++++++++++++++++ jsqrcode.min.js | 1 + qr-scanner.js | 30 +++++++++ 4 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 jsqrcode.min.js create mode 100644 qr-scanner.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4b311b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.DS_Store +tmp \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..972743c --- /dev/null +++ b/index.html @@ -0,0 +1,173 @@ + + + + + + HTML5 Camera QR UI + + + + + + + + + + +
+ +
+ +
+ Test + + + + + + + \ No newline at end of file diff --git a/jsqrcode.min.js b/jsqrcode.min.js new file mode 100644 index 0000000..6c317dc --- /dev/null +++ b/jsqrcode.min.js @@ -0,0 +1 @@ +(function(){function ECB(e,t){this.count=e,this.dataCodewords=t,this.__defineGetter__("Count",function(){return this.count}),this.__defineGetter__("DataCodewords",function(){return this.dataCodewords})}function ECBlocks(e,t,r){this.ecCodewordsPerBlock=e,this.ecBlocks=r?new Array(t,r):new Array(t),this.__defineGetter__("ECCodewordsPerBlock",function(){return this.ecCodewordsPerBlock}),this.__defineGetter__("TotalECCodewords",function(){return this.ecCodewordsPerBlock*this.NumBlocks}),this.__defineGetter__("NumBlocks",function(){for(var e=0,t=0;t6&&(t.setRegion(e-11,0,3,6),t.setRegion(0,e-11,6,3)),t},this.getECBlocksForLevel=function(e){return this.ecBlocks[e.ordinal()]}}function buildVersions(){return new Array(new Version(1,new Array,new ECBlocks(7,new ECB(1,19)),new ECBlocks(10,new ECB(1,16)),new ECBlocks(13,new ECB(1,13)),new ECBlocks(17,new ECB(1,9))),new Version(2,new Array(6,18),new ECBlocks(10,new ECB(1,34)),new ECBlocks(16,new ECB(1,28)),new ECBlocks(22,new ECB(1,22)),new ECBlocks(28,new ECB(1,16))),new Version(3,new Array(6,22),new ECBlocks(15,new ECB(1,55)),new ECBlocks(26,new ECB(1,44)),new ECBlocks(18,new ECB(2,17)),new ECBlocks(22,new ECB(2,13))),new Version(4,new Array(6,26),new ECBlocks(20,new ECB(1,80)),new ECBlocks(18,new ECB(2,32)),new ECBlocks(26,new ECB(2,24)),new ECBlocks(16,new ECB(4,9))),new Version(5,new Array(6,30),new ECBlocks(26,new ECB(1,108)),new ECBlocks(24,new ECB(2,43)),new ECBlocks(18,new ECB(2,15),new ECB(2,16)),new ECBlocks(22,new ECB(2,11),new ECB(2,12))),new Version(6,new Array(6,34),new ECBlocks(18,new ECB(2,68)),new ECBlocks(16,new ECB(4,27)),new ECBlocks(24,new ECB(4,19)),new ECBlocks(28,new ECB(4,15))),new Version(7,new Array(6,22,38),new ECBlocks(20,new ECB(2,78)),new ECBlocks(18,new ECB(4,31)),new ECBlocks(18,new ECB(2,14),new ECB(4,15)),new ECBlocks(26,new ECB(4,13),new ECB(1,14))),new Version(8,new Array(6,24,42),new ECBlocks(24,new ECB(2,97)),new ECBlocks(22,new ECB(2,38),new ECB(2,39)),new ECBlocks(22,new ECB(4,18),new ECB(2,19)),new ECBlocks(26,new ECB(4,14),new ECB(2,15))),new Version(9,new Array(6,26,46),new ECBlocks(30,new ECB(2,116)),new ECBlocks(22,new ECB(3,36),new ECB(2,37)),new ECBlocks(20,new ECB(4,16),new ECB(4,17)),new ECBlocks(24,new ECB(4,12),new ECB(4,13))),new Version(10,new Array(6,28,50),new ECBlocks(18,new ECB(2,68),new ECB(2,69)),new ECBlocks(26,new ECB(4,43),new ECB(1,44)),new ECBlocks(24,new ECB(6,19),new ECB(2,20)),new ECBlocks(28,new ECB(6,15),new ECB(2,16))),new Version(11,new Array(6,30,54),new ECBlocks(20,new ECB(4,81)),new ECBlocks(30,new ECB(1,50),new ECB(4,51)),new ECBlocks(28,new ECB(4,22),new ECB(4,23)),new ECBlocks(24,new ECB(3,12),new ECB(8,13))),new Version(12,new Array(6,32,58),new ECBlocks(24,new ECB(2,92),new ECB(2,93)),new ECBlocks(22,new ECB(6,36),new ECB(2,37)),new ECBlocks(26,new ECB(4,20),new ECB(6,21)),new ECBlocks(28,new ECB(7,14),new ECB(4,15))),new Version(13,new Array(6,34,62),new ECBlocks(26,new ECB(4,107)),new ECBlocks(22,new ECB(8,37),new ECB(1,38)),new ECBlocks(24,new ECB(8,20),new ECB(4,21)),new ECBlocks(22,new ECB(12,11),new ECB(4,12))),new Version(14,new Array(6,26,46,66),new ECBlocks(30,new ECB(3,115),new ECB(1,116)),new ECBlocks(24,new ECB(4,40),new ECB(5,41)),new ECBlocks(20,new ECB(11,16),new ECB(5,17)),new ECBlocks(24,new ECB(11,12),new ECB(5,13))),new Version(15,new Array(6,26,48,70),new ECBlocks(22,new ECB(5,87),new ECB(1,88)),new ECBlocks(24,new ECB(5,41),new ECB(5,42)),new ECBlocks(30,new ECB(5,24),new ECB(7,25)),new ECBlocks(24,new ECB(11,12),new ECB(7,13))),new Version(16,new Array(6,26,50,74),new ECBlocks(24,new ECB(5,98),new ECB(1,99)),new ECBlocks(28,new ECB(7,45),new ECB(3,46)),new ECBlocks(24,new ECB(15,19),new ECB(2,20)),new ECBlocks(30,new ECB(3,15),new ECB(13,16))),new Version(17,new Array(6,30,54,78),new ECBlocks(28,new ECB(1,107),new ECB(5,108)),new ECBlocks(28,new ECB(10,46),new ECB(1,47)),new ECBlocks(28,new ECB(1,22),new ECB(15,23)),new ECBlocks(28,new ECB(2,14),new ECB(17,15))),new Version(18,new Array(6,30,56,82),new ECBlocks(30,new ECB(5,120),new ECB(1,121)),new ECBlocks(26,new ECB(9,43),new ECB(4,44)),new ECBlocks(28,new ECB(17,22),new ECB(1,23)),new ECBlocks(28,new ECB(2,14),new ECB(19,15))),new Version(19,new Array(6,30,58,86),new ECBlocks(28,new ECB(3,113),new ECB(4,114)),new ECBlocks(26,new ECB(3,44),new ECB(11,45)),new ECBlocks(26,new ECB(17,21),new ECB(4,22)),new ECBlocks(26,new ECB(9,13),new ECB(16,14))),new Version(20,new Array(6,34,62,90),new ECBlocks(28,new ECB(3,107),new ECB(5,108)),new ECBlocks(26,new ECB(3,41),new ECB(13,42)),new ECBlocks(30,new ECB(15,24),new ECB(5,25)),new ECBlocks(28,new ECB(15,15),new ECB(10,16))),new Version(21,new Array(6,28,50,72,94),new ECBlocks(28,new ECB(4,116),new ECB(4,117)),new ECBlocks(26,new ECB(17,42)),new ECBlocks(28,new ECB(17,22),new ECB(6,23)),new ECBlocks(30,new ECB(19,16),new ECB(6,17))),new Version(22,new Array(6,26,50,74,98),new ECBlocks(28,new ECB(2,111),new ECB(7,112)),new ECBlocks(28,new ECB(17,46)),new ECBlocks(30,new ECB(7,24),new ECB(16,25)),new ECBlocks(24,new ECB(34,13))),new Version(23,new Array(6,30,54,74,102),new ECBlocks(30,new ECB(4,121),new ECB(5,122)),new ECBlocks(28,new ECB(4,47),new ECB(14,48)),new ECBlocks(30,new ECB(11,24),new ECB(14,25)),new ECBlocks(30,new ECB(16,15),new ECB(14,16))),new Version(24,new Array(6,28,54,80,106),new ECBlocks(30,new ECB(6,117),new ECB(4,118)),new ECBlocks(28,new ECB(6,45),new ECB(14,46)),new ECBlocks(30,new ECB(11,24),new ECB(16,25)),new ECBlocks(30,new ECB(30,16),new ECB(2,17))),new Version(25,new Array(6,32,58,84,110),new ECBlocks(26,new ECB(8,106),new ECB(4,107)),new ECBlocks(28,new ECB(8,47),new ECB(13,48)),new ECBlocks(30,new ECB(7,24),new ECB(22,25)),new ECBlocks(30,new ECB(22,15),new ECB(13,16))),new Version(26,new Array(6,30,58,86,114),new ECBlocks(28,new ECB(10,114),new ECB(2,115)),new ECBlocks(28,new ECB(19,46),new ECB(4,47)),new ECBlocks(28,new ECB(28,22),new ECB(6,23)),new ECBlocks(30,new ECB(33,16),new ECB(4,17))),new Version(27,new Array(6,34,62,90,118),new ECBlocks(30,new ECB(8,122),new ECB(4,123)),new ECBlocks(28,new ECB(22,45),new ECB(3,46)),new ECBlocks(30,new ECB(8,23),new ECB(26,24)),new ECBlocks(30,new ECB(12,15),new ECB(28,16))),new Version(28,new Array(6,26,50,74,98,122),new ECBlocks(30,new ECB(3,117),new ECB(10,118)),new ECBlocks(28,new ECB(3,45),new ECB(23,46)),new ECBlocks(30,new ECB(4,24),new ECB(31,25)),new ECBlocks(30,new ECB(11,15),new ECB(31,16))),new Version(29,new Array(6,30,54,78,102,126),new ECBlocks(30,new ECB(7,116),new ECB(7,117)),new ECBlocks(28,new ECB(21,45),new ECB(7,46)),new ECBlocks(30,new ECB(1,23),new ECB(37,24)),new ECBlocks(30,new ECB(19,15),new ECB(26,16))),new Version(30,new Array(6,26,52,78,104,130),new ECBlocks(30,new ECB(5,115),new ECB(10,116)),new ECBlocks(28,new ECB(19,47),new ECB(10,48)),new ECBlocks(30,new ECB(15,24),new ECB(25,25)),new ECBlocks(30,new ECB(23,15),new ECB(25,16))),new Version(31,new Array(6,30,56,82,108,134),new ECBlocks(30,new ECB(13,115),new ECB(3,116)),new ECBlocks(28,new ECB(2,46),new ECB(29,47)),new ECBlocks(30,new ECB(42,24),new ECB(1,25)),new ECBlocks(30,new ECB(23,15),new ECB(28,16))),new Version(32,new Array(6,34,60,86,112,138),new ECBlocks(30,new ECB(17,115)),new ECBlocks(28,new ECB(10,46),new ECB(23,47)),new ECBlocks(30,new ECB(10,24),new ECB(35,25)),new ECBlocks(30,new ECB(19,15),new ECB(35,16))),new Version(33,new Array(6,30,58,86,114,142),new ECBlocks(30,new ECB(17,115),new ECB(1,116)),new ECBlocks(28,new ECB(14,46),new ECB(21,47)),new ECBlocks(30,new ECB(29,24),new ECB(19,25)),new ECBlocks(30,new ECB(11,15),new ECB(46,16))),new Version(34,new Array(6,34,62,90,118,146),new ECBlocks(30,new ECB(13,115),new ECB(6,116)),new ECBlocks(28,new ECB(14,46),new ECB(23,47)),new ECBlocks(30,new ECB(44,24),new ECB(7,25)),new ECBlocks(30,new ECB(59,16),new ECB(1,17))),new Version(35,new Array(6,30,54,78,102,126,150),new ECBlocks(30,new ECB(12,121),new ECB(7,122)),new ECBlocks(28,new ECB(12,47),new ECB(26,48)),new ECBlocks(30,new ECB(39,24),new ECB(14,25)),new ECBlocks(30,new ECB(22,15),new ECB(41,16))),new Version(36,new Array(6,24,50,76,102,128,154),new ECBlocks(30,new ECB(6,121),new ECB(14,122)),new ECBlocks(28,new ECB(6,47),new ECB(34,48)),new ECBlocks(30,new ECB(46,24),new ECB(10,25)),new ECBlocks(30,new ECB(2,15),new ECB(64,16))),new Version(37,new Array(6,28,54,80,106,132,158),new ECBlocks(30,new ECB(17,122),new ECB(4,123)),new ECBlocks(28,new ECB(29,46),new ECB(14,47)),new ECBlocks(30,new ECB(49,24),new ECB(10,25)),new ECBlocks(30,new ECB(24,15),new ECB(46,16))),new Version(38,new Array(6,32,58,84,110,136,162),new ECBlocks(30,new ECB(4,122),new ECB(18,123)),new ECBlocks(28,new ECB(13,46),new ECB(32,47)),new ECBlocks(30,new ECB(48,24),new ECB(14,25)),new ECBlocks(30,new ECB(42,15),new ECB(32,16))),new Version(39,new Array(6,26,54,82,110,138,166),new ECBlocks(30,new ECB(20,117),new ECB(4,118)),new ECBlocks(28,new ECB(40,47),new ECB(7,48)),new ECBlocks(30,new ECB(43,24),new ECB(22,25)),new ECBlocks(30,new ECB(10,15),new ECB(67,16))),new Version(40,new Array(6,30,58,86,114,142,170),new ECBlocks(30,new ECB(19,118),new ECB(6,119)),new ECBlocks(28,new ECB(18,47),new ECB(31,48)),new ECBlocks(30,new ECB(34,24),new ECB(34,25)),new ECBlocks(30,new ECB(20,15),new ECB(61,16))))}function PerspectiveTransform(e,t,r,n,i,o,s,a,h){this.a11=e,this.a12=n,this.a13=s,this.a21=t,this.a22=i,this.a23=a,this.a31=r,this.a32=o,this.a33=h,this.transformPoints1=function(e){for(var t=e.length,r=this.a11,n=this.a12,i=this.a13,o=this.a21,s=this.a22,a=this.a23,h=this.a31,c=this.a32,d=this.a33,l=0;lMath.abs(r-e);if(i){var o=e;e=t,t=o,o=r,r=n,n=o}for(var s=Math.abs(r-e),a=Math.abs(n-t),h=-s>>1,c=t0){if(f==n)break;f+=c,h-=s}}var v=r-e,g=n-t;return Math.sqrt(v*v+g*g)},this.sizeOfBlackWhiteBlackRunBothWays=function(e,t,r,n){var i=this.sizeOfBlackWhiteBlackRun(e,t,r,n),o=1,s=e-(r-e);s<0?(o=e/(e-s),s=0):s>=qrcode.width&&(o=(qrcode.width-1-e)/(s-e),s=qrcode.width-1);var a=Math.floor(t-(n-t)*o);return o=1,a<0?(o=t/(t-a),a=0):a>=qrcode.height&&(o=(qrcode.height-1-t)/(a-t),a=qrcode.height-1),s=Math.floor(e+(s-e)*o),(i+=this.sizeOfBlackWhiteBlackRun(e,t,s,a))-1},this.calculateModuleSizeOneWay=function(e,t){var r=this.sizeOfBlackWhiteBlackRunBothWays(Math.floor(e.X),Math.floor(e.Y),Math.floor(t.X),Math.floor(t.Y)),n=this.sizeOfBlackWhiteBlackRunBothWays(Math.floor(t.X),Math.floor(t.Y),Math.floor(e.X),Math.floor(e.Y));return isNaN(r)?n/7:isNaN(n)?r/7:(r+n)/14},this.calculateModuleSize=function(e,t,r){return(this.calculateModuleSizeOneWay(e,t)+this.calculateModuleSizeOneWay(e,r))/2},this.distance=function(e,t){var r=e.X-t.X,n=e.Y-t.Y;return Math.sqrt(r*r+n*n)},this.computeDimension=function(e,t,r,n){var i=7+(Math.round(this.distance(e,t)/n)+Math.round(this.distance(e,r)/n)>>1);switch(3&i){case 0:i++;break;case 2:i--;break;case 3:throw"Error"}return i},this.findAlignmentInRegion=function(e,t,r,n){var i=Math.floor(n*e),o=Math.max(0,t-i),s=Math.min(qrcode.width-1,t+i);if(s-o<3*e)throw"Error";var a=Math.max(0,r-i),h=Math.min(qrcode.height-1,r+i);return new AlignmentPatternFinder(this.image,o,a,s-o,h-a,e,this.resultPointCallback).find()},this.createTransform=function(e,t,r,n,i){var o,s,a,h,c=i-3.5;return null!=n?(o=n.X,s=n.Y,a=h=c-3):(o=t.X-e.X+r.X,s=t.Y-e.Y+r.Y,a=h=c),PerspectiveTransform.quadrilateralToQuadrilateral(3.5,3.5,c,3.5,a,h,3.5,c,e.X,e.Y,t.X,t.Y,o,s,r.X,r.Y)},this.sampleGrid=function(e,t,r){return GridSampler.sampleGrid3(e,r,t)},this.processFinderPatternInfo=function(e){var t=e.TopLeft,r=e.TopRight,n=e.BottomLeft,i=this.calculateModuleSize(t,r,n);if(i<1)throw"Error";var o=this.computeDimension(t,r,n,i),s=Version.getProvisionalVersionForDimension(o),a=s.DimensionForVersion-7,h=null;if(s.AlignmentPatternCenters.length>0)for(var c=r.X-t.X+n.X,d=r.Y-t.Y+n.Y,l=1-3/a,w=Math.floor(t.X+l*(c-t.X)),f=Math.floor(t.Y+l*(d-t.Y)),u=4;u<=16;u<<=1){h=this.findAlignmentInRegion(i,w,f,u);break}var C,E=this.createTransform(t,r,n,h,o),B=this.sampleGrid(this.image,E,o);return C=null==h?new Array(n,t,r):new Array(n,t,r,h),new DetectorResult(B,C)},this.detect=function(){var e=(new FinderPatternFinder).findFinderPattern(this.image);return this.processFinderPatternInfo(e)}}function FormatInformation(e){this.errorCorrectionLevel=ErrorCorrectionLevel.forBits(e>>3&3),this.dataMask=7&e,this.__defineGetter__("ErrorCorrectionLevel",function(){return this.errorCorrectionLevel}),this.__defineGetter__("DataMask",function(){return this.dataMask}),this.GetHashCode=function(){return this.errorCorrectionLevel.ordinal()<<3|this.dataMask},this.Equals=function(e){var t=e;return this.errorCorrectionLevel==t.errorCorrectionLevel&&this.dataMask==t.dataMask}}function ErrorCorrectionLevel(e,t,r){this.ordinal_Renamed_Field=e,this.bits=t,this.name=r,this.__defineGetter__("Bits",function(){return this.bits}),this.__defineGetter__("Name",function(){return this.name}),this.ordinal=function(){return this.ordinal_Renamed_Field}}function BitMatrix(e,t){if(t||(t=e),e<1||t<1)throw"Both dimensions must be greater than 0";this.width=e,this.height=t;var r=e>>5;0!=(31&e)&&r++,this.rowSize=r,this.bits=new Array(r*t);for(var n=0;n>5);return 0!=(1&URShift(this.bits[r],31&e))},this.set_Renamed=function(e,t){var r=t*this.rowSize+(e>>5);this.bits[r]|=1<<(31&e)},this.flip=function(e,t){var r=t*this.rowSize+(e>>5);this.bits[r]^=1<<(31&e)},this.clear=function(){for(var e=this.bits.length,t=0;tthis.height||i>this.width)throw"The region must fit inside the matrix";for(var s=t;s>5)]|=1<<(31&h)}}function DataBlock(e,t){this.numDataCodewords=e,this.codewords=t,this.__defineGetter__("NumDataCodewords",function(){return this.numDataCodewords}),this.__defineGetter__("Codewords",function(){return this.codewords})}function BitMatrixParser(e){var t=e.Dimension;if(t<21||1!=(3&t))throw"Error BitMatrixParser";this.bitMatrix=e,this.parsedVersion=null,this.parsedFormatInfo=null,this.copyBit=function(e,t,r){return this.bitMatrix.get_Renamed(e,t)?r<<1|1:r<<1},this.readFormatInformation=function(){if(null!=this.parsedFormatInfo)return this.parsedFormatInfo;for(var e=0,t=0;t<6;t++)e=this.copyBit(t,8,e);e=this.copyBit(7,8,e),e=this.copyBit(8,8,e),e=this.copyBit(8,7,e);for(i=5;i>=0;i--)e=this.copyBit(8,i,e);if(this.parsedFormatInfo=FormatInformation.decodeFormatInformation(e),null!=this.parsedFormatInfo)return this.parsedFormatInfo;var r=this.bitMatrix.Dimension;e=0;for(var n=r-8,t=r-1;t>=n;t--)e=this.copyBit(t,8,e);for(var i=r-7;i>2;if(t<=6)return Version.getVersionForNumber(t);for(var r=0,n=e-11,i=5;i>=0;i--)for(o=e-9;o>=n;o--)r=this.copyBit(o,i,r);if(this.parsedVersion=Version.decodeVersionInformation(r),null!=this.parsedVersion&&this.parsedVersion.DimensionForVersion==e)return this.parsedVersion;r=0;for(var o=5;o>=0;o--)for(i=e-9;i>=n;i--)r=this.copyBit(o,i,r);if(this.parsedVersion=Version.decodeVersionInformation(r),null!=this.parsedVersion&&this.parsedVersion.DimensionForVersion==e)return this.parsedVersion;throw"Error readVersion"},this.readCodewords=function(){var e=this.readFormatInformation(),t=this.readVersion(),r=DataMask.forReference(e.DataMask),n=this.bitMatrix.Dimension;r.unmaskBitMatrix(this.bitMatrix,n);for(var i=t.buildFunctionPattern(),o=!0,s=new Array(t.TotalCodewords),a=0,h=0,c=0,d=n-1;d>0;d-=2){6==d&&d--;for(var l=0;l=Math.floor(r/2);){var d=i,l=s,w=h;if(i=o,s=a,h=c,i.Zero)throw"r_{i-1} was zero";o=d;for(var f=this.field.Zero,u=i.getCoefficient(i.Degree),C=this.field.inverse(u);o.Degree>=i.Degree&&!o.Zero;){var E=o.Degree-i.Degree,B=this.field.multiply(o.getCoefficient(o.Degree),C);f=f.addOrSubtract(this.field.buildMonomial(E,B)),o=o.addOrSubtract(i.multiplyByMonomial(E,B))}a=f.multiply1(s).addOrSubtract(l),c=f.multiply1(h).addOrSubtract(w)}var v=c.getCoefficient(0);if(0==v)throw"ReedSolomonException sigmaTilde(0) was zero";var g=this.field.inverse(v),m=c.multiply2(g),k=o.multiply2(g);return new Array(m,k)},this.findErrorLocations=function(e){var t=e.Degree;if(1==t)return new Array(e.getCoefficient(1));for(var r=new Array(t),n=0,i=1;i<256&&n1&&0==t[0]){for(var n=1;nn.length){var i=r;r=n,n=i}for(var o=new Array(n.length),s=n.length-r.length,a=0;a=e.Degree&&!r.Zero;){var o=r.Degree-e.Degree,s=this.field.multiply(r.getCoefficient(r.Degree),i),a=e.multiplyByMonomial(o,s),h=this.field.buildMonomial(o,s);t=t.addOrSubtract(h),r=r.addOrSubtract(a)}return new Array(t,r)}}function GF256(e){this.expTable=new Array(256),this.logTable=new Array(256);for(var t=1,r=0;r<256;r++)this.expTable[r]=t,(t<<=1)>=256&&(t^=e);for(r=0;r<255;r++)this.logTable[this.expTable[r]]=r;var n=new Array(1);n[0]=0,this.zero=new GF256Poly(this,new Array(n));var i=new Array(1);i[0]=1,this.one=new GF256Poly(this,new Array(i)),this.__defineGetter__("Zero",function(){return this.zero}),this.__defineGetter__("One",function(){return this.one}),this.buildMonomial=function(e,t){if(e<0)throw"System.ArgumentException";if(0==t)return this.zero;for(var r=new Array(e+1),n=0;n=0?e>>t:(e>>t)+(2<<~t)}function FinderPattern(e,t,r){this.x=e,this.y=t,this.count=1,this.estimatedModuleSize=r,this.__defineGetter__("EstimatedModuleSize",function(){return this.estimatedModuleSize}),this.__defineGetter__("Count",function(){return this.count}),this.__defineGetter__("X",function(){return this.x}),this.__defineGetter__("Y",function(){return this.y}),this.incrementCount=function(){this.count++},this.aboutEquals=function(e,t,r){if(Math.abs(t-this.y)<=e&&Math.abs(r-this.x)<=e){var n=Math.abs(e-this.estimatedModuleSize);return n<=1||n/this.estimatedModuleSize<=1}return!1}}function FinderPatternInfo(e){this.bottomLeft=e[0],this.topLeft=e[1],this.topRight=e[2],this.__defineGetter__("BottomLeft",function(){return this.bottomLeft}),this.__defineGetter__("TopLeft",function(){return this.topLeft}),this.__defineGetter__("TopRight",function(){return this.topRight})}function FinderPatternFinder(){this.image=null,this.possibleCenters=[],this.hasSkipped=!1,this.crossCheckStateCount=new Array(0,0,0,0,0),this.resultPointCallback=null,this.__defineGetter__("CrossCheckStateCount",function(){return this.crossCheckStateCount[0]=0,this.crossCheckStateCount[1]=0,this.crossCheckStateCount[2]=0,this.crossCheckStateCount[3]=0,this.crossCheckStateCount[4]=0,this.crossCheckStateCount}),this.foundPatternCross=function(e){for(var t=0,r=0;r<5;r++){var n=e[r];if(0==n)return!1;t+=n}if(t<7)return!1;var i=Math.floor((t<=0&&i[t+a*qrcode.width];)s[2]++,a--;if(a<0)return NaN;for(;a>=0&&!i[t+a*qrcode.width]&&s[1]<=r;)s[1]++,a--;if(a<0||s[1]>r)return NaN;for(;a>=0&&i[t+a*qrcode.width]&&s[0]<=r;)s[0]++,a--;if(s[0]>r)return NaN;for(a=e+1;a=r)return NaN;for(;a=r)return NaN;var h=s[0]+s[1]+s[2]+s[3]+s[4];return 5*Math.abs(h-n)>=2*n?NaN:this.foundPatternCross(s)?this.centerFromEnd(s,a):NaN},this.crossCheckHorizontal=function(e,t,r,n){for(var i=this.image,o=qrcode.width,s=this.CrossCheckStateCount,a=e;a>=0&&i[a+t*qrcode.width];)s[2]++,a--;if(a<0)return NaN;for(;a>=0&&!i[a+t*qrcode.width]&&s[1]<=r;)s[1]++,a--;if(a<0||s[1]>r)return NaN;for(;a>=0&&i[a+t*qrcode.width]&&s[0]<=r;)s[0]++,a--;if(s[0]>r)return NaN;for(a=e+1;a=r)return NaN;for(;a=r)return NaN;var h=s[0]+s[1]+s[2]+s[3]+s[4];return 5*Math.abs(h-n)>=n?NaN:this.foundPatternCross(s)?this.centerFromEnd(s,a):NaN},this.handlePossibleCenter=function(e,t,r){var n=e[0]+e[1]+e[2]+e[3]+e[4],i=this.centerFromEnd(e,r),o=this.crossCheckVertical(t,Math.floor(i),e[2],n);if(!isNaN(o)&&(i=this.crossCheckHorizontal(Math.floor(i),Math.floor(o),e[2],n),!isNaN(i))){for(var s=n/7,a=!1,h=this.possibleCenters.length,c=0;c3){for(var t=0,r=0,n=0;n=0;n--){var h=this.possibleCenters[n];Math.abs(h.EstimatedModuleSize-o)>a&&this.possibleCenters.splice(n,1)}}return this.possibleCenters.length>3&&this.possibleCenters.sort(function(e,t){return e.count>t.count?-1:e.count=CENTER_QUORUM){if(null!=t)return this.hasSkipped=!0,Math.floor((Math.abs(t.X-n.X)-Math.abs(t.Y-n.Y))/2);t=n}}return 0},this.haveMultiplyConfirmedCenters=function(){for(var e=0,t=0,r=this.possibleCenters.length,n=0;n=CENTER_QUORUM&&(e++,t+=i.EstimatedModuleSize)}if(e<3)return!1;for(var o=t/r,s=0,n=0;no[2]&&(s+=c-o[2]-n,h=r-1)}else{do{h++}while(h=r)return!1;return!0},this.crossCheckVertical=function(e,t,r,n){var i=this.image,o=qrcode.height,s=this.crossCheckStateCount;s[0]=0,s[1]=0,s[2]=0;for(var a=e;a>=0&&i[t+a*qrcode.width]&&s[1]<=r;)s[1]++,a--;if(a<0||s[1]>r)return NaN;for(;a>=0&&!i[t+a*qrcode.width]&&s[0]<=r;)s[0]++,a--;if(s[0]>r)return NaN;for(a=e+1;ar)return NaN;for(;ar)return NaN;var h=s[0]+s[1]+s[2];return 5*Math.abs(h-n)>=2*n?NaN:this.foundPatternCross(s)?this.centerFromEnd(s,a):NaN},this.handlePossibleCenter=function(e,t,r){var n=e[0]+e[1]+e[2],i=this.centerFromEnd(e,r),o=this.crossCheckVertical(t,Math.floor(i),2*e[1],n);if(!isNaN(o)){for(var s=(e[0]+e[1]+e[2])/3,a=this.possibleCenters.length,h=0;h>1),a=new Array(0,0,0),h=0;h>1:-(h+1>>1));a[0]=0,a[1]=0,a[2]=0;for(var d=t;d=10&&t<=26?this.dataLengthMode=1:t>=27&&t<=40&&(this.dataLengthMode=2),this.getNextBits=function(e){var t=0;if(e>this.bitPointer-e+1,this.bitPointer-=e,t}if(e>8-(e-(this.bitPointer+1)),this.bitPointer=this.bitPointer-e%8,this.bitPointer<0&&(this.bitPointer=8+this.bitPointer),t}if(e>8-(e-(this.bitPointer+1+8))),this.bitPointer=this.bitPointer-(e-8)%8,this.bitPointer<0&&(this.bitPointer=8+this.bitPointer),t}return 0},this.NextMode=function(){return this.blockPointer>this.blocks.length-this.numErrorCorrectionCode-2?0:this.getNextBits(4)},this.getDataLength=function(e){for(var t=0;;){if(e>>t==1)break;t++}return this.getNextBits(qrcode.sizeOfDataLengthInfo[this.dataLengthMode][t])},this.getRomanAndFigureString=function(e){var t=e,r=0,n="",i=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"," ","$","%","*","+","-",".","/",":");do{if(t>1){var o=(r=this.getNextBits(11))%45;n+=i[Math.floor(r/45)],n+=i[o],t-=2}else 1==t&&(n+=i[r=this.getNextBits(6)],t-=1)}while(t>0);return n},this.getFigureString=function(e){var t=e,r=0,n="";do{t>=3?((r=this.getNextBits(10))<100&&(n+="0"),r<10&&(n+="0"),t-=3):2==t?((r=this.getNextBits(7))<10&&(n+="0"),t-=2):1==t&&(r=this.getNextBits(4),t-=1),n+=r}while(t>0);return n},this.get8bitByteArray=function(e){var t=e,r=0,n=new Array;do{r=this.getNextBits(8),n.push(r),t--}while(t>0);return n},this.getKanjiString=function(e){var t=e,r=0,n="";do{var i=((r=this.getNextBits(13))/192<<8)+r%192,o=0;o=i+33088<=40956?i+33088:i+49472,n+=String.fromCharCode(o),t--}while(t>0);return n},this.parseECIValue=function(){var e=0,t=this.getNextBits(8);return 0==(128&t)&&(e=127&t),128==(192&t)&&(e=(63&t)<<8|this.getNextBits(8)),192==(224&t)&&(e=(31&t)<<16|this.getNextBits(8)),e},this.__defineGetter__("DataByte",function(){for(var e=new Array;;){var t=this.NextMode();if(0==t){if(e.length>0)break;throw"Empty data block"}if(1!=t&&2!=t&&4!=t&&8!=t&&7!=t)throw"Invalid mode: "+t+" in (block:"+this.blockPointer+" bit:"+this.bitPointer+")";if(7==t)s=this.parseECIValue();else{var r=this.getDataLength(t);if(r<1)throw"Invalid data length: "+r;switch(t){case 1:for(var n=this.getFigureString(r),i=new Array(n.length),o=0;or||a<-1||a>n)throw"Error.checkAndNudgePoints ";i=!1,-1==s?(t[o]=0,i=!0):s==r&&(t[o]=r-1,i=!0),-1==a?(t[o+1]=0,i=!0):a==n&&(t[o+1]=n-1,i=!0)}i=!0;for(o=t.length-2;o>=0&&i;o-=2){var s=Math.floor(t[o]),a=Math.floor(t[o+1]);if(s<-1||s>r||a<-1||a>n)throw"Error.checkAndNudgePoints ";i=!1,-1==s?(t[o]=0,i=!0):s==r&&(t[o]=r-1,i=!0),-1==a?(t[o+1]=0,i=!0):a==n&&(t[o+1]=n-1,i=!0)}},GridSampler.sampleGrid3=function(e,t,r){for(var n=new BitMatrix(t),i=new Array(t<<1),o=0;o>1),i[h+1]=a;r.transformPoints1(i),GridSampler.checkAndNudgePoints(e,i);try{for(h=0;h>1,o)}catch(e){throw"Error.checkAndNudgePoints"}}return n},GridSampler.sampleGridx=function(e,t,r,n,i,o,s,a,h,c,d,l,w,f,u,C,E,B){var v=PerspectiveTransform.quadrilateralToQuadrilateral(r,n,i,o,s,a,h,c,d,l,w,f,u,C,E,B);return GridSampler.sampleGrid3(e,t,v)},Version.VERSION_DECODE_INFO=new Array(31892,34236,39577,42195,48118,51042,55367,58893,63784,68472,70749,76311,79154,84390,87683,92361,96236,102084,102881,110507,110734,117786,119615,126325,127568,133589,136944,141498,145311,150283,152622,158308,161089,167017),Version.VERSIONS=buildVersions(),Version.getVersionForNumber=function(e){if(e<1||e>40)throw"ArgumentException";return Version.VERSIONS[e-1]},Version.getProvisionalVersionForDimension=function(e){if(e%4!=1)throw"Error getProvisionalVersionForDimension";try{return Version.getVersionForNumber(e-17>>2)}catch(e){throw"Error getVersionForNumber"}},Version.decodeVersionInformation=function(e){for(var t=4294967295,r=0,n=0;n=FOR_BITS.length)throw"ArgumentException";return FOR_BITS[e]};var L=new ErrorCorrectionLevel(0,1,"L"),M=new ErrorCorrectionLevel(1,0,"M"),Q=new ErrorCorrectionLevel(2,3,"Q"),H=new ErrorCorrectionLevel(3,2,"H"),FOR_BITS=new Array(M,L,H,Q);DataBlock.getDataBlocks=function(e,t,r){if(e.length!=t.TotalCodewords)throw"ArgumentException";for(var n=t.getECBlocksForLevel(r),i=0,o=n.getECBlocks(),s=0;s=0&&a[u].codewords.length!=f;)u--;u++;for(var C=f-n.ECCodewordsPerBlock,E=0,s=0;s7)throw"System.ArgumentException";return DataMask.DATA_MASKS[e]},DataMask.DATA_MASKS=new Array(new DataMask000,new DataMask001,new DataMask010,new DataMask011,new DataMask100,new DataMask101,new DataMask110,new DataMask111),GF256.QR_CODE_FIELD=new GF256(285),GF256.DATA_MATRIX_FIELD=new GF256(301),GF256.addOrSubtract=function(e,t){return e^t};var Decoder={};Decoder.rsDecoder=new ReedSolomonDecoder(GF256.QR_CODE_FIELD),Decoder.correctErrors=function(e,t){for(var r=e.length,n=new Array(r),i=0;i-1&&(r=[{sourceId:e.deviceId}]),console.log(e.kind+": "+e.label+" id = "+e.deviceId)})})}catch(e){console.log(e)}else console.log("no navigator.mediaDevices.enumerateDevices");t.getUserMedia?t.getUserMedia({video:r,audio:!1},qrcode.vidSuccess,qrcode.vidError):t.webkitGetUserMedia?(qrcode.webkit=!0,t.webkitGetUserMedia({video:r,audio:!1},qrcode.vidSuccess,qrcode.vidError)):t.mozGetUserMedia&&(qrcode.moz=!0,t.mozGetUserMedia({video:r,audio:!1},qrcode.vidSuccess,qrcode.vidError))},qrcode.decode=function(e){if(0==arguments.length){if(qrcode.canvas_qr2)var t=qrcode.canvas_qr2,r=qrcode.qrcontext2;else r=(t=document.getElementById("qr-canvas")).getContext("2d");return qrcode.width=t.width,qrcode.height=t.height,qrcode.imagedata=r.getImageData(0,0,qrcode.width,qrcode.height),qrcode.result=qrcode.process(r),null!=qrcode.callback&&qrcode.callback(qrcode.result),qrcode.result}var n=new Image;n.crossOrigin="Anonymous",n.onload=function(){var e=document.getElementById("out-canvas");if(null!=e){var t=e.getContext("2d");t.clearRect(0,0,320,240),t.drawImage(n,0,0,320,240)}var r=document.createElement("canvas"),i=r.getContext("2d"),o=n.height,s=n.width;if(n.width*n.height>qrcode.maxImgSize){var a=n.width/n.height;s=a*(o=Math.sqrt(qrcode.maxImgSize/a))}r.width=s,r.height=o,i.drawImage(n,0,0,r.width,r.height),qrcode.width=r.width,qrcode.height=r.height;try{qrcode.imagedata=i.getImageData(0,0,r.width,r.height)}catch(e){return qrcode.result="Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!",void(null!=qrcode.callback&&qrcode.callback(qrcode.result))}try{qrcode.result=qrcode.process(i)}catch(e){console.log(e),qrcode.result="error decoding QR Code"}null!=qrcode.callback&&qrcode.callback(qrcode.result)},n.onerror=function(){null!=qrcode.callback&&qrcode.callback("Failed to load the image")},n.src=e},qrcode.isUrl=function(e){return/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(e)},qrcode.decode_url=function(e){var t="";try{t=escape(e)}catch(r){console.log(r),t=e}var r="";try{r=decodeURIComponent(t)}catch(e){console.log(e),r=t}return r},qrcode.decode_utf8=function(e){return qrcode.isUrl(e)?qrcode.decode_url(e):e},qrcode.process=function(e){var t=(new Date).getTime(),r=qrcode.grayScaleToBitmap(qrcode.grayscale());if(qrcode.debug){for(i=0;in[w][l][1]&&(n[w][l][1]=h)}}for(var c=new Array(4),d=0;d<4;d++)c[d]=new Array(4);for(var l=0;l<4;l++)for(var w=0;w<4;w++)c[w][l]=Math.floor((n[w][l][0]+n[w][l][1])/2);return c},qrcode.grayScaleToBitmap=function(e){for(var t=qrcode.getMiddleBrightnessPerArea(e),r=t.length,n=Math.floor(qrcode.width/r),i=Math.floor(qrcode.height/r),o=new ArrayBuffer(qrcode.width*qrcode.height),s=new Uint8Array(o),a=0;a=o&&s>=a?(n=e[0],r=e[1],i=e[2]):a>=s&&a>=o?(n=e[1],r=e[0],i=e[2]):(n=e[2],r=e[0],i=e[1]),function(e,t,r){var n=t.x,i=t.y;return(r.x-n)*(e.y-i)-(r.y-i)*(e.x-n)}(r,n,i)<0){var h=r;r=i,i=h}e[0]=r,e[1]=n,e[2]=i};window.qrscanner=qrcode}()) \ No newline at end of file diff --git a/qr-scanner.js b/qr-scanner.js new file mode 100644 index 0000000..87c3a4a --- /dev/null +++ b/qr-scanner.js @@ -0,0 +1,30 @@ +class QrScanner extends XElement { + onCreate() { + console.log('scanner init',this) + this.$video = this.$('video'); + this.$canvas = this.$('canvas'); + this.$context = this.$canvas.getContext('2d'); + this.$video.addEventListener('play', () => this._drawOnCanvas(), false); + navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } }) + .then(stream => this.$video.srcObject = stream) + .catch(console.error); + } + + _drawOnCanvas() { + if (this.$video.paused || this.$video.ended) return false; + this.$context.drawImage(this.$video, -(this.$video.clientWidth - 320) / 2, -(this.$video.clientHeight - 320) / 2); + this._decode(); + requestAnimationFrame(() => this._drawOnCanvas()); + } + + _decode() { + try { + var decoded = qrscanner.decode(); + this.fire('x-decoded', decoded); + // console.log(decoded); + } catch (e) { + // no qr-code in this frame + } + } +} +