Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Ported to JavaScript by Lazar Laszlo 2011
  3.  
  4.   lazarsoft@gmail.com, www.lazarsoft.info
  5.  
  6. */
  7.  
  8. /*
  9. *
  10. * Copyright 2007 ZXing authors
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. *      http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24.  
  25.  
  26. var GridSampler = {};
  27.  
  28. GridSampler.checkAndNudgePoints=function( image,  points)
  29.         {
  30.             var width = qrcode.width;
  31.             var height = qrcode.height;
  32.             // Check and nudge points from start until we see some that are OK:
  33.             var nudged = true;
  34.             for (var offset = 0; offset < points.length && nudged; offset += 2)
  35.             {
  36.                 var x = Math.floor (points[offset]);
  37.                 var y = Math.floor( points[offset + 1]);
  38.                 if (x < - 1 || x > width || y < - 1 || y > height)
  39.                 {
  40.                     throw "Error.checkAndNudgePoints ";
  41.                 }
  42.                 nudged = false;
  43.                 if (x == - 1)
  44.                 {
  45.                     points[offset] = 0.0;
  46.                     nudged = true;
  47.                 }
  48.                 else if (x == width)
  49.                 {
  50.                     points[offset] = width - 1;
  51.                     nudged = true;
  52.                 }
  53.                 if (y == - 1)
  54.                 {
  55.                     points[offset + 1] = 0.0;
  56.                     nudged = true;
  57.                 }
  58.                 else if (y == height)
  59.                 {
  60.                     points[offset + 1] = height - 1;
  61.                     nudged = true;
  62.                 }
  63.             }
  64.             // Check and nudge points from end:
  65.             nudged = true;
  66.             for (var offset = points.length - 2; offset >= 0 && nudged; offset -= 2)
  67.             {
  68.                 var x = Math.floor( points[offset]);
  69.                 var y = Math.floor( points[offset + 1]);
  70.                 if (x < - 1 || x > width || y < - 1 || y > height)
  71.                 {
  72.                     throw "Error.checkAndNudgePoints ";
  73.                 }
  74.                 nudged = false;
  75.                 if (x == - 1)
  76.                 {
  77.                     points[offset] = 0.0;
  78.                     nudged = true;
  79.                 }
  80.                 else if (x == width)
  81.                 {
  82.                     points[offset] = width - 1;
  83.                     nudged = true;
  84.                 }
  85.                 if (y == - 1)
  86.                 {
  87.                     points[offset + 1] = 0.0;
  88.                     nudged = true;
  89.                 }
  90.                 else if (y == height)
  91.                 {
  92.                     points[offset + 1] = height - 1;
  93.                     nudged = true;
  94.                 }
  95.             }
  96.         }
  97.    
  98.  
  99.  
  100. GridSampler.sampleGrid3=function( image,  dimension,  transform)
  101.         {
  102.             var bits = new BitMatrix(dimension);
  103.             var points = new Array(dimension << 1);
  104.             for (var y = 0; y < dimension; y++)
  105.             {
  106.                 var max = points.length;
  107.                 var iValue =  y + 0.5;
  108.                 for (var x = 0; x < max; x += 2)
  109.                 {
  110.                     points[x] =  (x >> 1) + 0.5;
  111.                     points[x + 1] = iValue;
  112.                 }
  113.                 transform.transformPoints1(points);
  114.                 // Quick check to see if points transformed to something inside the image;
  115.                 // sufficient to check the endpoints
  116.                 GridSampler.checkAndNudgePoints(image, points);
  117.                 try
  118.                 {
  119.                     for (var x = 0; x < max; x += 2)
  120.                     {
  121.                         //var xpoint = (Math.floor( points[x]) * 4) + (Math.floor( points[x + 1]) * qrcode.width * 4);
  122.                         var bit = image[Math.floor( points[x])+ qrcode.width* Math.floor( points[x + 1])];
  123.                         //qrcode.imagedata.data[xpoint] = bit?255:0;
  124.                         //qrcode.imagedata.data[xpoint+1] = bit?255:0;
  125.                         //qrcode.imagedata.data[xpoint+2] = 0;
  126.                         //qrcode.imagedata.data[xpoint+3] = 255;
  127.                         //bits[x >> 1][ y]=bit;
  128.                         if(bit)
  129.                             bits.set_Renamed(x >> 1, y);
  130.                     }
  131.                 }
  132.                 catch ( aioobe)
  133.                 {
  134.                     // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  135.                     // transform gets "twisted" such that it maps a straight line of points to a set of points
  136.                     // whose endpoints are in bounds, but others are not. There is probably some mathematical
  137.                     // way to detect this about the transformation that I don't know yet.
  138.                     // This results in an ugly runtime exception despite our clever checks above -- can't have
  139.                     // that. We could check each point's coordinates but that feels duplicative. We settle for
  140.                     // catching and wrapping ArrayIndexOutOfBoundsException.
  141.                     throw "Error.checkAndNudgePoints";
  142.                 }
  143.             }
  144.             return bits;
  145.         }
  146.  
  147. GridSampler.sampleGridx=function( image,  dimension,  p1ToX,  p1ToY,  p2ToX,  p2ToY,  p3ToX,  p3ToY,  p4ToX,  p4ToY,  p1FromX,  p1FromY,  p2FromX,  p2FromY,  p3FromX,  p3FromY,  p4FromX,  p4FromY)
  148. {
  149.     var transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
  150.            
  151.     return GridSampler.sampleGrid3(image, dimension, transform);
  152. }
  153.  
  154. /*
  155.   Ported to JavaScript by Lazar Laszlo 2011
  156.  
  157.   lazarsoft@gmail.com, www.lazarsoft.info
  158.  
  159. */
  160.  
  161. /*
  162. *
  163. * Copyright 2007 ZXing authors
  164. *
  165. * Licensed under the Apache License, Version 2.0 (the "License");
  166. * you may not use this file except in compliance with the License.
  167. * You may obtain a copy of the License at
  168. *
  169. *      http://www.apache.org/licenses/LICENSE-2.0
  170. *
  171. * Unless required by applicable law or agreed to in writing, software
  172. * distributed under the License is distributed on an "AS IS" BASIS,
  173. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  174. * See the License for the specific language governing permissions and
  175. * limitations under the License.
  176. */
  177.  
  178.  
  179.  
  180. function ECB(count,  dataCodewords)
  181. {
  182.     this.count = count;
  183.     this.dataCodewords = dataCodewords;
  184.    
  185.     this.__defineGetter__("Count", function()
  186.     {
  187.         return this.count;
  188.     });
  189.     this.__defineGetter__("DataCodewords", function()
  190.     {
  191.         return this.dataCodewords;
  192.     });
  193. }
  194.  
  195. function ECBlocks( ecCodewordsPerBlock,  ecBlocks1,  ecBlocks2)
  196. {
  197.     this.ecCodewordsPerBlock = ecCodewordsPerBlock;
  198.     if(ecBlocks2)
  199.         this.ecBlocks = new Array(ecBlocks1, ecBlocks2);
  200.     else
  201.         this.ecBlocks = new Array(ecBlocks1);
  202.    
  203.     this.__defineGetter__("ECCodewordsPerBlock", function()
  204.     {
  205.         return this.ecCodewordsPerBlock;
  206.     });
  207.    
  208.     this.__defineGetter__("TotalECCodewords", function()
  209.     {
  210.         return  this.ecCodewordsPerBlock * this.NumBlocks;
  211.     });
  212.    
  213.     this.__defineGetter__("NumBlocks", function()
  214.     {
  215.         var total = 0;
  216.         for (var i = 0; i < this.ecBlocks.length; i++)
  217.         {
  218.             total += this.ecBlocks[i].length;
  219.         }
  220.         return total;
  221.     });
  222.    
  223.     this.getECBlocks=function()
  224.             {
  225.                 return this.ecBlocks;
  226.             }
  227. }
  228.  
  229. function Version( versionNumber,  alignmentPatternCenters,  ecBlocks1,  ecBlocks2,  ecBlocks3,  ecBlocks4)
  230. {
  231.     this.versionNumber = versionNumber;
  232.     this.alignmentPatternCenters = alignmentPatternCenters;
  233.     this.ecBlocks = new Array(ecBlocks1, ecBlocks2, ecBlocks3, ecBlocks4);
  234.    
  235.     var total = 0;
  236.     var ecCodewords = ecBlocks1.ECCodewordsPerBlock;
  237.     var ecbArray = ecBlocks1.getECBlocks();
  238.     for (var i = 0; i < ecbArray.length; i++)
  239.     {
  240.         var ecBlock = ecbArray[i];
  241.         total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords);
  242.     }
  243.     this.totalCodewords = total;
  244.    
  245.     this.__defineGetter__("VersionNumber", function()
  246.     {
  247.         return  this.versionNumber;
  248.     });
  249.    
  250.     this.__defineGetter__("AlignmentPatternCenters", function()
  251.     {
  252.         return  this.alignmentPatternCenters;
  253.     });
  254.     this.__defineGetter__("TotalCodewords", function()
  255.     {
  256.         return  this.totalCodewords;
  257.     });
  258.     this.__defineGetter__("DimensionForVersion", function()
  259.     {
  260.         return  17 + 4 * this.versionNumber;
  261.     });
  262.    
  263.     this.buildFunctionPattern=function()
  264.         {
  265.             var dimension = this.DimensionForVersion;
  266.             var bitMatrix = new BitMatrix(dimension);
  267.            
  268.             // Top left finder pattern + separator + format
  269.             bitMatrix.setRegion(0, 0, 9, 9);
  270.             // Top right finder pattern + separator + format
  271.             bitMatrix.setRegion(dimension - 8, 0, 8, 9);
  272.             // Bottom left finder pattern + separator + format
  273.             bitMatrix.setRegion(0, dimension - 8, 9, 8);
  274.            
  275.             // Alignment patterns
  276.             var max = this.alignmentPatternCenters.length;
  277.             for (var x = 0; x < max; x++)
  278.             {
  279.                 var i = this.alignmentPatternCenters[x] - 2;
  280.                 for (var y = 0; y < max; y++)
  281.                 {
  282.                     if ((x == 0 && (y == 0 || y == max - 1)) || (x == max - 1 && y == 0))
  283.                     {
  284.                         // No alignment patterns near the three finder paterns
  285.                         continue;
  286.                     }
  287.                     bitMatrix.setRegion(this.alignmentPatternCenters[y] - 2, i, 5, 5);
  288.                 }
  289.             }
  290.            
  291.             // Vertical timing pattern
  292.             bitMatrix.setRegion(6, 9, 1, dimension - 17);
  293.             // Horizontal timing pattern
  294.             bitMatrix.setRegion(9, 6, dimension - 17, 1);
  295.            
  296.             if (this.versionNumber > 6)
  297.             {
  298.                 // Version info, top right
  299.                 bitMatrix.setRegion(dimension - 11, 0, 3, 6);
  300.                 // Version info, bottom left
  301.                 bitMatrix.setRegion(0, dimension - 11, 6, 3);
  302.             }
  303.            
  304.             return bitMatrix;
  305.         }
  306.     this.getECBlocksForLevel=function( ecLevel)
  307.     {
  308.         return this.ecBlocks[ecLevel.ordinal()];
  309.     }
  310. }
  311.  
  312. Version.VERSION_DECODE_INFO = new Array(0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6, 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78, 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683, 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB, 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250, 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B, 0x2542E, 0x26A64, 0x27541, 0x28C69);
  313.  
  314. Version.VERSIONS = buildVersions();
  315.  
  316. Version.getVersionForNumber=function( versionNumber)
  317. {
  318.     if (versionNumber < 1 || versionNumber > 40)
  319.     {
  320.         throw "ArgumentException";
  321.     }
  322.     return Version.VERSIONS[versionNumber - 1];
  323. }
  324.  
  325. Version.getProvisionalVersionForDimension=function(dimension)
  326. {
  327.     if (dimension % 4 != 1)
  328.     {
  329.         throw "Error getProvisionalVersionForDimension";
  330.     }
  331.     try
  332.     {
  333.         return Version.getVersionForNumber((dimension - 17) >> 2);
  334.     }
  335.     catch ( iae)
  336.     {
  337.         throw "Error getVersionForNumber";
  338.     }
  339. }
  340.  
  341. Version.decodeVersionInformation=function( versionBits)
  342. {
  343.     var bestDifference = 0xffffffff;
  344.     var bestVersion = 0;
  345.     for (var i = 0; i < Version.VERSION_DECODE_INFO.length; i++)
  346.     {
  347.         var targetVersion = Version.VERSION_DECODE_INFO[i];
  348.         // Do the version info bits match exactly? done.
  349.         if (targetVersion == versionBits)
  350.         {
  351.             return this.getVersionForNumber(i + 7);
  352.         }
  353.         // Otherwise see if this is the closest to a real version info bit string
  354.         // we have seen so far
  355.         var bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion);
  356.         if (bitsDifference < bestDifference)
  357.         {
  358.             bestVersion = i + 7;
  359.             bestDifference = bitsDifference;
  360.         }
  361.     }
  362.     // We can tolerate up to 3 bits of error since no two version info codewords will
  363.     // differ in less than 4 bits.
  364.     if (bestDifference <= 3)
  365.     {
  366.         return this.getVersionForNumber(bestVersion);
  367.     }
  368.     // If we didn't find a close enough match, fail
  369.     return null;
  370. }
  371.  
  372. function buildVersions()
  373. {
  374.     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))),
  375.     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))),
  376.     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))),
  377.     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))),
  378.     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))),
  379.     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))),
  380.     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))),
  381.     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))),
  382.     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))),
  383.     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))),
  384.     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))),
  385.     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))),
  386.     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))),
  387.     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))),
  388.     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))),
  389.     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))),
  390.     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))),
  391.     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))),
  392.     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))),
  393.     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))),
  394.     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))),
  395.     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))),
  396.     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))),
  397.     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))),
  398.     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))),
  399.     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))),
  400.     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))),
  401.     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))),
  402.     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))),
  403.     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))),
  404.     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))),
  405.     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))),
  406.     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))),
  407.     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))),
  408.     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))),
  409.     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))),
  410.     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))),
  411.     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))),
  412.     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))),
  413.     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))));
  414. }
  415.  
  416. /*
  417.   Ported to JavaScript by Lazar Laszlo 2011
  418.  
  419.   lazarsoft@gmail.com, www.lazarsoft.info
  420.  
  421. */
  422.  
  423. /*
  424. *
  425. * Copyright 2007 ZXing authors
  426. *
  427. * Licensed under the Apache License, Version 2.0 (the "License");
  428. * you may not use this file except in compliance with the License.
  429. * You may obtain a copy of the License at
  430. *
  431. *      http://www.apache.org/licenses/LICENSE-2.0
  432. *
  433. * Unless required by applicable law or agreed to in writing, software
  434. * distributed under the License is distributed on an "AS IS" BASIS,
  435. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  436. * See the License for the specific language governing permissions and
  437. * limitations under the License.
  438. */
  439.  
  440.  
  441. function PerspectiveTransform( a11,  a21,  a31,  a12,  a22,  a32,  a13,  a23,  a33)
  442. {
  443.     this.a11 = a11;
  444.     this.a12 = a12;
  445.     this.a13 = a13;
  446.     this.a21 = a21;
  447.     this.a22 = a22;
  448.     this.a23 = a23;
  449.     this.a31 = a31;
  450.     this.a32 = a32;
  451.     this.a33 = a33;
  452.     this.transformPoints1=function( points)
  453.         {
  454.             var max = points.length;
  455.             var a11 = this.a11;
  456.             var a12 = this.a12;
  457.             var a13 = this.a13;
  458.             var a21 = this.a21;
  459.             var a22 = this.a22;
  460.             var a23 = this.a23;
  461.             var a31 = this.a31;
  462.             var a32 = this.a32;
  463.             var a33 = this.a33;
  464.             for (var i = 0; i < max; i += 2)
  465.             {
  466.                 var x = points[i];
  467.                 var y = points[i + 1];
  468.                 var denominator = a13 * x + a23 * y + a33;
  469.                 points[i] = (a11 * x + a21 * y + a31) / denominator;
  470.                 points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
  471.             }
  472.         }
  473.     this. transformPoints2=function(xValues, yValues)
  474.         {
  475.             var n = xValues.length;
  476.             for (var i = 0; i < n; i++)
  477.             {
  478.                 var x = xValues[i];
  479.                 var y = yValues[i];
  480.                 var denominator = this.a13 * x + this.a23 * y + this.a33;
  481.                 xValues[i] = (this.a11 * x + this.a21 * y + this.a31) / denominator;
  482.                 yValues[i] = (this.a12 * x + this.a22 * y + this.a32) / denominator;
  483.             }
  484.         }
  485.  
  486.     this.buildAdjoint=function()
  487.         {
  488.             // Adjoint is the transpose of the cofactor matrix:
  489.             return new PerspectiveTransform(this.a22 * this.a33 - this.a23 * this.a32, this.a23 * this.a31 - this.a21 * this.a33, this.a21 * this.a32 - this.a22 * this.a31, this.a13 * this.a32 - this.a12 * this.a33, this.a11 * this.a33 - this.a13 * this.a31, this.a12 * this.a31 - this.a11 * this.a32, this.a12 * this.a23 - this.a13 * this.a22, this.a13 * this.a21 - this.a11 * this.a23, this.a11 * this.a22 - this.a12 * this.a21);
  490.         }
  491.     this.times=function( other)
  492.         {
  493.             return new PerspectiveTransform(this.a11 * other.a11 + this.a21 * other.a12 + this.a31 * other.a13, this.a11 * other.a21 + this.a21 * other.a22 + this.a31 * other.a23, this.a11 * other.a31 + this.a21 * other.a32 + this.a31 * other.a33, this.a12 * other.a11 + this.a22 * other.a12 + this.a32 * other.a13, this.a12 * other.a21 + this.a22 * other.a22 + this.a32 * other.a23, this.a12 * other.a31 + this.a22 * other.a32 + this.a32 * other.a33, this.a13 * other.a11 + this.a23 * other.a12 +this.a33 * other.a13, this.a13 * other.a21 + this.a23 * other.a22 + this.a33 * other.a23, this.a13 * other.a31 + this.a23 * other.a32 + this.a33 * other.a33);
  494.         }
  495.  
  496. }
  497.  
  498. PerspectiveTransform.quadrilateralToQuadrilateral=function( x0,  y0,  x1,  y1,  x2,  y2,  x3,  y3,  x0p,  y0p,  x1p,  y1p,  x2p,  y2p,  x3p,  y3p)
  499. {
  500.    
  501.     var qToS = this.quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
  502.     var sToQ = this.squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);
  503.     return sToQ.times(qToS);
  504. }
  505.  
  506. PerspectiveTransform.squareToQuadrilateral=function( x0,  y0,  x1,  y1,  x2,  y2,  x3,  y3)
  507. {
  508.     var dy2 = y3 - y2;
  509.     var dy3 = y0 - y1 + y2 - y3;
  510.     if (dy2 == 0.0 && dy3 == 0.0)
  511.     {
  512.         return new PerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, y2 - y1, y0, 0.0, 0.0, 1.0);
  513.     }
  514.     else
  515.     {
  516.         var dx1 = x1 - x2;
  517.         var dx2 = x3 - x2;
  518.         var dx3 = x0 - x1 + x2 - x3;
  519.         var dy1 = y1 - y2;
  520.         var denominator = dx1 * dy2 - dx2 * dy1;
  521.         var a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
  522.         var a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
  523.         return new PerspectiveTransform(x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0, a13, a23, 1.0);
  524.     }
  525. }
  526.  
  527. PerspectiveTransform.quadrilateralToSquare=function( x0,  y0,  x1,  y1,  x2,  y2,  x3,  y3)
  528. {
  529.     // Here, the adjoint serves as the inverse:
  530.     return this.squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint();
  531. }
  532.  
  533. function DetectorResult(bits,  points)
  534. {
  535.     this.bits = bits;
  536.     this.points = points;
  537. }
  538.  
  539.  
  540. function Detector(image)
  541. {
  542.     this.image=image;
  543.     this.resultPointCallback = null;
  544.    
  545.     this.sizeOfBlackWhiteBlackRun=function( fromX,  fromY,  toX,  toY)
  546.         {
  547.             // Mild variant of Bresenham's algorithm;
  548.             // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  549.             var steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  550.             if (steep)
  551.             {
  552.                 var temp = fromX;
  553.                 fromX = fromY;
  554.                 fromY = temp;
  555.                 temp = toX;
  556.                 toX = toY;
  557.                 toY = temp;
  558.             }
  559.            
  560.             var dx = Math.abs(toX - fromX);
  561.             var dy = Math.abs(toY - fromY);
  562.             var error = - dx >> 1;
  563.             var ystep = fromY < toY?1:- 1;
  564.             var xstep = fromX < toX?1:- 1;
  565.             var state = 0; // In black pixels, looking for white, first or second time
  566.             for (var x = fromX, y = fromY; x != toX; x += xstep)
  567.             {
  568.                
  569.                 var realX = steep?y:x;
  570.                 var realY = steep?x:y;
  571.                 if (state == 1)
  572.                 {
  573.                     // In white pixels, looking for black
  574.                     if (this.image[realX + realY*qrcode.width])
  575.                     {
  576.                         state++;
  577.                     }
  578.                 }
  579.                 else
  580.                 {
  581.                     if (!this.image[realX + realY*qrcode.width])
  582.                     {
  583.                         state++;
  584.                     }
  585.                 }
  586.                
  587.                 if (state == 3)
  588.                 {
  589.                     // Found black, white, black, and stumbled back onto white; done
  590.                     var diffX = x - fromX;
  591.                     var diffY = y - fromY;
  592.                     return  Math.sqrt( (diffX * diffX + diffY * diffY));
  593.                 }
  594.                 error += dy;
  595.                 if (error > 0)
  596.                 {
  597.                     if (y == toY)
  598.                     {
  599.                         break;
  600.                     }
  601.                     y += ystep;
  602.                     error -= dx;
  603.                 }
  604.             }
  605.             var diffX2 = toX - fromX;
  606.             var diffY2 = toY - fromY;
  607.             return  Math.sqrt( (diffX2 * diffX2 + diffY2 * diffY2));
  608.         }
  609.  
  610.    
  611.     this.sizeOfBlackWhiteBlackRunBothWays=function( fromX,  fromY,  toX,  toY)
  612.         {
  613.            
  614.             var result = this.sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
  615.            
  616.             // Now count other way -- don't run off image though of course
  617.             var scale = 1.0;
  618.             var otherToX = fromX - (toX - fromX);
  619.             if (otherToX < 0)
  620.             {
  621.                 scale =  fromX /  (fromX - otherToX);
  622.                 otherToX = 0;
  623.             }
  624.             else if (otherToX >= qrcode.width)
  625.             {
  626.                 scale =  (qrcode.width - 1 - fromX) /  (otherToX - fromX);
  627.                 otherToX = qrcode.width - 1;
  628.             }
  629.             var otherToY = Math.floor (fromY - (toY - fromY) * scale);
  630.            
  631.             scale = 1.0;
  632.             if (otherToY < 0)
  633.             {
  634.                 scale =  fromY /  (fromY - otherToY);
  635.                 otherToY = 0;
  636.             }
  637.             else if (otherToY >= qrcode.height)
  638.             {
  639.                 scale =  (qrcode.height - 1 - fromY) /  (otherToY - fromY);
  640.                 otherToY = qrcode.height - 1;
  641.             }
  642.             otherToX = Math.floor (fromX + (otherToX - fromX) * scale);
  643.            
  644.             result += this.sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
  645.             return result - 1.0; // -1 because we counted the middle pixel twice
  646.         }
  647.        
  648.  
  649.    
  650.     this.calculateModuleSizeOneWay=function( pattern,  otherPattern)
  651.         {
  652.             var moduleSizeEst1 = this.sizeOfBlackWhiteBlackRunBothWays(Math.floor( pattern.X), Math.floor( pattern.Y), Math.floor( otherPattern.X), Math.floor(otherPattern.Y));
  653.             var moduleSizeEst2 = this.sizeOfBlackWhiteBlackRunBothWays(Math.floor(otherPattern.X), Math.floor(otherPattern.Y), Math.floor( pattern.X), Math.floor(pattern.Y));
  654.             if (isNaN(moduleSizeEst1))
  655.             {
  656.                 return moduleSizeEst2 / 7.0;
  657.             }
  658.             if (isNaN(moduleSizeEst2))
  659.             {
  660.                 return moduleSizeEst1 / 7.0;
  661.             }
  662.             // Average them, and divide by 7 since we've counted the width of 3 black modules,
  663.             // and 1 white and 1 black module on either side. Ergo, divide sum by 14.
  664.             return (moduleSizeEst1 + moduleSizeEst2) / 14.0;
  665.         }
  666.  
  667.    
  668.     this.calculateModuleSize=function( topLeft,  topRight,  bottomLeft)
  669.         {
  670.             // Take the average
  671.             return (this.calculateModuleSizeOneWay(topLeft, topRight) + this.calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0;
  672.         }
  673.  
  674.     this.distance=function( pattern1,  pattern2)
  675.     {
  676.         var xDiff = pattern1.X - pattern2.X;
  677.         var yDiff = pattern1.Y - pattern2.Y;
  678.         return  Math.sqrt( (xDiff * xDiff + yDiff * yDiff));
  679.     }
  680.     this.computeDimension=function( topLeft,  topRight,  bottomLeft,  moduleSize)
  681.         {
  682.            
  683.             var tltrCentersDimension = Math.round(this.distance(topLeft, topRight) / moduleSize);
  684.             var tlblCentersDimension = Math.round(this.distance(topLeft, bottomLeft) / moduleSize);
  685.             var dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
  686.             switch (dimension & 0x03)
  687.             {
  688.                
  689.                 // mod 4
  690.                 case 0:
  691.                     dimension++;
  692.                     break;
  693.                     // 1? do nothing
  694.                
  695.                 case 2:
  696.                     dimension--;
  697.                     break;
  698.                
  699.                 case 3:
  700.                     throw "Error";
  701.                 }
  702.             return dimension;
  703.         }
  704.  
  705.     this.findAlignmentInRegion=function( overallEstModuleSize,  estAlignmentX,  estAlignmentY,  allowanceFactor)
  706.         {
  707.             // Look for an alignment pattern (3 modules in size) around where it
  708.             // should be
  709.             var allowance = Math.floor (allowanceFactor * overallEstModuleSize);
  710.             var alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance);
  711.             var alignmentAreaRightX = Math.min(qrcode.width - 1, estAlignmentX + allowance);
  712.             if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3)
  713.             {
  714.                 throw "Error";
  715.             }
  716.            
  717.             var alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
  718.             var alignmentAreaBottomY = Math.min(qrcode.height - 1, estAlignmentY + allowance);
  719.            
  720.             var alignmentFinder = new AlignmentPatternFinder(this.image, alignmentAreaLeftX, alignmentAreaTopY, alignmentAreaRightX - alignmentAreaLeftX, alignmentAreaBottomY - alignmentAreaTopY, overallEstModuleSize, this.resultPointCallback);
  721.             return alignmentFinder.find();
  722.         }
  723.        
  724.     this.createTransform=function( topLeft,  topRight,  bottomLeft, alignmentPattern, dimension)
  725.         {
  726.             var dimMinusThree =  dimension - 3.5;
  727.             var bottomRightX;
  728.             var bottomRightY;
  729.             var sourceBottomRightX;
  730.             var sourceBottomRightY;
  731.             if (alignmentPattern != null)
  732.             {
  733.                 bottomRightX = alignmentPattern.X;
  734.                 bottomRightY = alignmentPattern.Y;
  735.                 sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0;
  736.             }
  737.             else
  738.             {
  739.                 // Don't have an alignment pattern, just make up the bottom-right point
  740.                 bottomRightX = (topRight.X - topLeft.X) + bottomLeft.X;
  741.                 bottomRightY = (topRight.Y - topLeft.Y) + bottomLeft.Y;
  742.                 sourceBottomRightX = sourceBottomRightY = dimMinusThree;
  743.             }
  744.            
  745.             var transform = PerspectiveTransform.quadrilateralToQuadrilateral(3.5, 3.5, dimMinusThree, 3.5, sourceBottomRightX, sourceBottomRightY, 3.5, dimMinusThree, topLeft.X, topLeft.Y, topRight.X, topRight.Y, bottomRightX, bottomRightY, bottomLeft.X, bottomLeft.Y);
  746.            
  747.             return transform;
  748.         }      
  749.    
  750.     this.sampleGrid=function( image,  transform,  dimension)
  751.         {
  752.            
  753.             var sampler = GridSampler;
  754.             return sampler.sampleGrid3(image, dimension, transform);
  755.         }
  756.    
  757.     this.processFinderPatternInfo = function( info)
  758.         {
  759.            
  760.             var topLeft = info.TopLeft;
  761.             var topRight = info.TopRight;
  762.             var bottomLeft = info.BottomLeft;
  763.            
  764.             var moduleSize = this.calculateModuleSize(topLeft, topRight, bottomLeft);
  765.             if (moduleSize < 1.0)
  766.             {
  767.                 throw "Error";
  768.             }
  769.             var dimension = this.computeDimension(topLeft, topRight, bottomLeft, moduleSize);
  770.             var provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
  771.             var modulesBetweenFPCenters = provisionalVersion.DimensionForVersion - 7;
  772.            
  773.             var alignmentPattern = null;
  774.             // Anything above version 1 has an alignment pattern
  775.             if (provisionalVersion.AlignmentPatternCenters.length > 0)
  776.             {
  777.                
  778.                 // Guess where a "bottom right" finder pattern would have been
  779.                 var bottomRightX = topRight.X - topLeft.X + bottomLeft.X;
  780.                 var bottomRightY = topRight.Y - topLeft.Y + bottomLeft.Y;
  781.                
  782.                 // Estimate that alignment pattern is closer by 3 modules
  783.                 // from "bottom right" to known top left location
  784.                 var correctionToTopLeft = 1.0 - 3.0 /  modulesBetweenFPCenters;
  785.                 var estAlignmentX = Math.floor (topLeft.X + correctionToTopLeft * (bottomRightX - topLeft.X));
  786.                 var estAlignmentY = Math.floor (topLeft.Y + correctionToTopLeft * (bottomRightY - topLeft.Y));
  787.                
  788.                 // Kind of arbitrary -- expand search radius before giving up
  789.                 for (var i = 4; i <= 16; i <<= 1)
  790.                 {
  791.                     //try
  792.                     //{
  793.                         alignmentPattern = this.findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY,  i);
  794.                         break;
  795.                     //}
  796.                     //catch (re)
  797.                     //{
  798.                         // try next round
  799.                     //}
  800.                 }
  801.                 // If we didn't find alignment pattern... well try anyway without it
  802.             }
  803.            
  804.             var transform = this.createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension);
  805.            
  806.             var bits = this.sampleGrid(this.image, transform, dimension);
  807.            
  808.             var points;
  809.             if (alignmentPattern == null)
  810.             {
  811.                 points = new Array(bottomLeft, topLeft, topRight);
  812.             }
  813.             else
  814.             {
  815.                 points = new Array(bottomLeft, topLeft, topRight, alignmentPattern);
  816.             }
  817.             return new DetectorResult(bits, points);
  818.         }
  819.        
  820.  
  821.    
  822.     this.detect=function()
  823.     {
  824.         var info =  new FinderPatternFinder().findFinderPattern(this.image);
  825.            
  826.         return this.processFinderPatternInfo(info);
  827.     }
  828. }
  829.  
  830. /*
  831.   Ported to JavaScript by Lazar Laszlo 2011
  832.  
  833.   lazarsoft@gmail.com, www.lazarsoft.info
  834.  
  835. */
  836.  
  837. /*
  838. *
  839. * Copyright 2007 ZXing authors
  840. *
  841. * Licensed under the Apache License, Version 2.0 (the "License");
  842. * you may not use this file except in compliance with the License.
  843. * You may obtain a copy of the License at
  844. *
  845. *      http://www.apache.org/licenses/LICENSE-2.0
  846. *
  847. * Unless required by applicable law or agreed to in writing, software
  848. * distributed under the License is distributed on an "AS IS" BASIS,
  849. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  850. * See the License for the specific language governing permissions and
  851. * limitations under the License.
  852. */
  853.  
  854.  
  855. var FORMAT_INFO_MASK_QR = 0x5412;
  856. var FORMAT_INFO_DECODE_LOOKUP = new Array(new Array(0x5412, 0x00), new Array(0x5125, 0x01), new Array(0x5E7C, 0x02), new Array(0x5B4B, 0x03), new Array(0x45F9, 0x04), new Array(0x40CE, 0x05), new Array(0x4F97, 0x06), new Array(0x4AA0, 0x07), new Array(0x77C4, 0x08), new Array(0x72F3, 0x09), new Array(0x7DAA, 0x0A), new Array(0x789D, 0x0B), new Array(0x662F, 0x0C), new Array(0x6318, 0x0D), new Array(0x6C41, 0x0E), new Array(0x6976, 0x0F), new Array(0x1689, 0x10), new Array(0x13BE, 0x11), new Array(0x1CE7, 0x12), new Array(0x19D0, 0x13), new Array(0x0762, 0x14), new Array(0x0255, 0x15), new Array(0x0D0C, 0x16), new Array(0x083B, 0x17), new Array(0x355F, 0x18), new Array(0x3068, 0x19), new Array(0x3F31, 0x1A), new Array(0x3A06, 0x1B), new Array(0x24B4, 0x1C), new Array(0x2183, 0x1D), new Array(0x2EDA, 0x1E), new Array(0x2BED, 0x1F));
  857. var BITS_SET_IN_HALF_BYTE = new Array(0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4);
  858.  
  859.  
  860. function FormatInformation(formatInfo)
  861. {
  862.     this.errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
  863.     this.dataMask =  (formatInfo & 0x07);
  864.  
  865.     this.__defineGetter__("ErrorCorrectionLevel", function()
  866.     {
  867.         return this.errorCorrectionLevel;
  868.     });
  869.     this.__defineGetter__("DataMask", function()
  870.     {
  871.         return this.dataMask;
  872.     });
  873.     this.GetHashCode=function()
  874.     {
  875.         return (this.errorCorrectionLevel.ordinal() << 3) |  this.dataMask;
  876.     }
  877.     this.Equals=function( o)
  878.     {
  879.         var other =  o;
  880.         return this.errorCorrectionLevel == other.errorCorrectionLevel && this.dataMask == other.dataMask;
  881.     }
  882. }
  883.  
  884. FormatInformation.numBitsDiffering=function( a,  b)
  885. {
  886.     a ^= b; // a now has a 1 bit exactly where its bit differs with b's
  887.     // Count bits set quickly with a series of lookups:
  888.     return BITS_SET_IN_HALF_BYTE[a & 0x0F] + BITS_SET_IN_HALF_BYTE[(URShift(a, 4) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 8) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 12) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 16) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 20) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 24) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 28) & 0x0F)];
  889. }
  890.  
  891. FormatInformation.decodeFormatInformation=function( maskedFormatInfo)
  892. {
  893.     var formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo);
  894.     if (formatInfo != null)
  895.     {
  896.         return formatInfo;
  897.     }
  898.     // Should return null, but, some QR codes apparently
  899.     // do not mask this info. Try again by actually masking the pattern
  900.     // first
  901.     return FormatInformation.doDecodeFormatInformation(maskedFormatInfo ^ FORMAT_INFO_MASK_QR);
  902. }
  903. FormatInformation.doDecodeFormatInformation=function( maskedFormatInfo)
  904. {
  905.     // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
  906.     var bestDifference = 0xffffffff;
  907.     var bestFormatInfo = 0;
  908.     for (var i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++)
  909.     {
  910.         var decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
  911.         var targetInfo = decodeInfo[0];
  912.         if (targetInfo == maskedFormatInfo)
  913.         {
  914.             // Found an exact match
  915.             return new FormatInformation(decodeInfo[1]);
  916.         }
  917.         var bitsDifference = this.numBitsDiffering(maskedFormatInfo, targetInfo);
  918.         if (bitsDifference < bestDifference)
  919.         {
  920.             bestFormatInfo = decodeInfo[1];
  921.             bestDifference = bitsDifference;
  922.         }
  923.     }
  924.     // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
  925.     // differing means we found a match
  926.     if (bestDifference <= 3)
  927.     {
  928.         return new FormatInformation(bestFormatInfo);
  929.     }
  930.     return null;
  931. }
  932.  
  933.        
  934.  
  935. /*
  936.   Ported to JavaScript by Lazar Laszlo 2011
  937.  
  938.   lazarsoft@gmail.com, www.lazarsoft.info
  939.  
  940. */
  941.  
  942. /*
  943. *
  944. * Copyright 2007 ZXing authors
  945. *
  946. * Licensed under the Apache License, Version 2.0 (the "License");
  947. * you may not use this file except in compliance with the License.
  948. * You may obtain a copy of the License at
  949. *
  950. *      http://www.apache.org/licenses/LICENSE-2.0
  951. *
  952. * Unless required by applicable law or agreed to in writing, software
  953. * distributed under the License is distributed on an "AS IS" BASIS,
  954. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  955. * See the License for the specific language governing permissions and
  956. * limitations under the License.
  957. */
  958.  
  959.  
  960. function ErrorCorrectionLevel(ordinal,  bits, name)
  961. {
  962.     this.ordinal_Renamed_Field = ordinal;
  963.     this.bits = bits;
  964.     this.name = name;
  965.     this.__defineGetter__("Bits", function()
  966.     {
  967.         return this.bits;
  968.     });
  969.     this.__defineGetter__("Name", function()
  970.     {
  971.         return this.name;
  972.     });
  973.     this.ordinal=function()
  974.     {
  975.         return this.ordinal_Renamed_Field;
  976.     }
  977. }
  978.  
  979. ErrorCorrectionLevel.forBits=function( bits)
  980. {
  981.     if (bits < 0 || bits >= FOR_BITS.length)
  982.     {
  983.         throw "ArgumentException";
  984.     }
  985.     return FOR_BITS[bits];
  986. }
  987.  
  988. var L = new ErrorCorrectionLevel(0, 0x01, "L");
  989. var M = new ErrorCorrectionLevel(1, 0x00, "M");
  990. var Q = new ErrorCorrectionLevel(2, 0x03, "Q");
  991. var H = new ErrorCorrectionLevel(3, 0x02, "H");
  992. var FOR_BITS = new Array( M, L, H, Q);
  993.  
  994. /*
  995.   Ported to JavaScript by Lazar Laszlo 2011
  996.  
  997.   lazarsoft@gmail.com, www.lazarsoft.info
  998.  
  999. */
  1000.  
  1001. /*
  1002. *
  1003. * Copyright 2007 ZXing authors
  1004. *
  1005. * Licensed under the Apache License, Version 2.0 (the "License");
  1006. * you may not use this file except in compliance with the License.
  1007. * You may obtain a copy of the License at
  1008. *
  1009. *      http://www.apache.org/licenses/LICENSE-2.0
  1010. *
  1011. * Unless required by applicable law or agreed to in writing, software
  1012. * distributed under the License is distributed on an "AS IS" BASIS,
  1013. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1014. * See the License for the specific language governing permissions and
  1015. * limitations under the License.
  1016. */
  1017.  
  1018.  
  1019. function BitMatrix( width,  height)
  1020. {
  1021.     if(!height)
  1022.         height=width;
  1023.     if (width < 1 || height < 1)
  1024.     {
  1025.         throw "Both dimensions must be greater than 0";
  1026.     }
  1027.     this.width = width;
  1028.     this.height = height;
  1029.     var rowSize = width >> 5;
  1030.     if ((width & 0x1f) != 0)
  1031.     {
  1032.         rowSize++;
  1033.     }
  1034.     this.rowSize = rowSize;
  1035.     this.bits = new Array(rowSize * height);
  1036.     for(var i=0;i<this.bits.length;i++)
  1037.         this.bits[i]=0;
  1038.    
  1039.     this.__defineGetter__("Width", function()
  1040.     {
  1041.         return this.width;
  1042.     });
  1043.     this.__defineGetter__("Height", function()
  1044.     {
  1045.         return this.height;
  1046.     });
  1047.     this.__defineGetter__("Dimension", function()
  1048.     {
  1049.         if (this.width != this.height)
  1050.         {
  1051.             throw "Can't call getDimension() on a non-square matrix";
  1052.         }
  1053.         return this.width;
  1054.     });
  1055.    
  1056.     this.get_Renamed=function( x,  y)
  1057.         {
  1058.             var offset = y * this.rowSize + (x >> 5);
  1059.             return ((URShift(this.bits[offset], (x & 0x1f))) & 1) != 0;
  1060.         }
  1061.     this.set_Renamed=function( x,  y)
  1062.         {
  1063.             var offset = y * this.rowSize + (x >> 5);
  1064.             this.bits[offset] |= 1 << (x & 0x1f);
  1065.         }
  1066.     this.flip=function( x,  y)
  1067.         {
  1068.             var offset = y * this.rowSize + (x >> 5);
  1069.             this.bits[offset] ^= 1 << (x & 0x1f);
  1070.         }
  1071.     this.clear=function()
  1072.         {
  1073.             var max = this.bits.length;
  1074.             for (var i = 0; i < max; i++)
  1075.             {
  1076.                 this.bits[i] = 0;
  1077.             }
  1078.         }
  1079.     this.setRegion=function( left,  top,  width,  height)
  1080.         {
  1081.             if (top < 0 || left < 0)
  1082.             {
  1083.                 throw "Left and top must be nonnegative";
  1084.             }
  1085.             if (height < 1 || width < 1)
  1086.             {
  1087.                 throw "Height and width must be at least 1";
  1088.             }
  1089.             var right = left + width;
  1090.             var bottom = top + height;
  1091.             if (bottom > this.height || right > this.width)
  1092.             {
  1093.                 throw "The region must fit inside the matrix";
  1094.             }
  1095.             for (var y = top; y < bottom; y++)
  1096.             {
  1097.                 var offset = y * this.rowSize;
  1098.                 for (var x = left; x < right; x++)
  1099.                 {
  1100.                     this.bits[offset + (x >> 5)] |= 1 << (x & 0x1f);
  1101.                 }
  1102.             }
  1103.         }
  1104. }
  1105.  
  1106.  
  1107. /*
  1108.   Ported to JavaScript by Lazar Laszlo 2011
  1109.  
  1110.   lazarsoft@gmail.com, www.lazarsoft.info
  1111.  
  1112. */
  1113.  
  1114. /*
  1115. *
  1116. * Copyright 2007 ZXing authors
  1117. *
  1118. * Licensed under the Apache License, Version 2.0 (the "License");
  1119. * you may not use this file except in compliance with the License.
  1120. * You may obtain a copy of the License at
  1121. *
  1122. *      http://www.apache.org/licenses/LICENSE-2.0
  1123. *
  1124. * Unless required by applicable law or agreed to in writing, software
  1125. * distributed under the License is distributed on an "AS IS" BASIS,
  1126. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1127. * See the License for the specific language governing permissions and
  1128. * limitations under the License.
  1129. */
  1130.  
  1131.  
  1132. function DataBlock(numDataCodewords,  codewords)
  1133. {
  1134.     this.numDataCodewords = numDataCodewords;
  1135.     this.codewords = codewords;
  1136.    
  1137.     this.__defineGetter__("NumDataCodewords", function()
  1138.     {
  1139.         return this.numDataCodewords;
  1140.     });
  1141.     this.__defineGetter__("Codewords", function()
  1142.     {
  1143.         return this.codewords;
  1144.     });
  1145. }  
  1146.    
  1147. DataBlock.getDataBlocks=function(rawCodewords,  version,  ecLevel)
  1148. {
  1149.    
  1150.     if (rawCodewords.length != version.TotalCodewords)
  1151.     {
  1152.         throw "ArgumentException";
  1153.     }
  1154.    
  1155.     // Figure out the number and size of data blocks used by this version and
  1156.     // error correction level
  1157.     var ecBlocks = version.getECBlocksForLevel(ecLevel);
  1158.    
  1159.     // First count the total number of data blocks
  1160.     var totalBlocks = 0;
  1161.     var ecBlockArray = ecBlocks.getECBlocks();
  1162.     for (var i = 0; i < ecBlockArray.length; i++)
  1163.     {
  1164.         totalBlocks += ecBlockArray[i].Count;
  1165.     }
  1166.    
  1167.     // Now establish DataBlocks of the appropriate size and number of data codewords
  1168.     var result = new Array(totalBlocks);
  1169.     var numResultBlocks = 0;
  1170.     for (var j = 0; j < ecBlockArray.length; j++)
  1171.     {
  1172.         var ecBlock = ecBlockArray[j];
  1173.         for (var i = 0; i < ecBlock.Count; i++)
  1174.         {
  1175.             var numDataCodewords = ecBlock.DataCodewords;
  1176.             var numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords;
  1177.             result[numResultBlocks++] = new DataBlock(numDataCodewords, new Array(numBlockCodewords));
  1178.         }
  1179.     }
  1180.    
  1181.     // All blocks have the same amount of data, except that the last n
  1182.     // (where n may be 0) have 1 more byte. Figure out where these start.
  1183.     var shorterBlocksTotalCodewords = result[0].codewords.length;
  1184.     var longerBlocksStartAt = result.length - 1;
  1185.     while (longerBlocksStartAt >= 0)
  1186.     {
  1187.         var numCodewords = result[longerBlocksStartAt].codewords.length;
  1188.         if (numCodewords == shorterBlocksTotalCodewords)
  1189.         {
  1190.             break;
  1191.         }
  1192.         longerBlocksStartAt--;
  1193.     }
  1194.     longerBlocksStartAt++;
  1195.    
  1196.     var shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock;
  1197.     // The last elements of result may be 1 element longer;
  1198.     // first fill out as many elements as all of them have
  1199.     var rawCodewordsOffset = 0;
  1200.     for (var i = 0; i < shorterBlocksNumDataCodewords; i++)
  1201.     {
  1202.         for (var j = 0; j < numResultBlocks; j++)
  1203.         {
  1204.             result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
  1205.         }
  1206.     }
  1207.     // Fill out the last data block in the longer ones
  1208.     for (var j = longerBlocksStartAt; j < numResultBlocks; j++)
  1209.     {
  1210.         result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
  1211.     }
  1212.     // Now add in error correction blocks
  1213.     var max = result[0].codewords.length;
  1214.     for (var i = shorterBlocksNumDataCodewords; i < max; i++)
  1215.     {
  1216.         for (var j = 0; j < numResultBlocks; j++)
  1217.         {
  1218.             var iOffset = j < longerBlocksStartAt?i:i + 1;
  1219.             result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
  1220.         }
  1221.     }
  1222.     return result;
  1223. }
  1224.  
  1225. /*
  1226.   Ported to JavaScript by Lazar Laszlo 2011
  1227.  
  1228.   lazarsoft@gmail.com, www.lazarsoft.info
  1229.  
  1230. */
  1231.  
  1232. /*
  1233. *
  1234. * Copyright 2007 ZXing authors
  1235. *
  1236. * Licensed under the Apache License, Version 2.0 (the "License");
  1237. * you may not use this file except in compliance with the License.
  1238. * You may obtain a copy of the License at
  1239. *
  1240. *      http://www.apache.org/licenses/LICENSE-2.0
  1241. *
  1242. * Unless required by applicable law or agreed to in writing, software
  1243. * distributed under the License is distributed on an "AS IS" BASIS,
  1244. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1245. * See the License for the specific language governing permissions and
  1246. * limitations under the License.
  1247. */
  1248.  
  1249.  
  1250. function BitMatrixParser(bitMatrix)
  1251. {
  1252.     var dimension = bitMatrix.Dimension;
  1253.     if (dimension < 21 || (dimension & 0x03) != 1)
  1254.     {
  1255.         throw "Error BitMatrixParser";
  1256.     }
  1257.     this.bitMatrix = bitMatrix;
  1258.     this.parsedVersion = null;
  1259.     this.parsedFormatInfo = null;
  1260.    
  1261.     this.copyBit=function( i,  j,  versionBits)
  1262.     {
  1263.         return this.bitMatrix.get_Renamed(i, j)?(versionBits << 1) | 0x1:versionBits << 1;
  1264.     }
  1265.    
  1266.     this.readFormatInformation=function()
  1267.     {
  1268.             if (this.parsedFormatInfo != null)
  1269.             {
  1270.                 return this.parsedFormatInfo;
  1271.             }
  1272.            
  1273.             // Read top-left format info bits
  1274.             var formatInfoBits = 0;
  1275.             for (var i = 0; i < 6; i++)
  1276.             {
  1277.                 formatInfoBits = this.copyBit(i, 8, formatInfoBits);
  1278.             }
  1279.             // .. and skip a bit in the timing pattern ...
  1280.             formatInfoBits = this.copyBit(7, 8, formatInfoBits);
  1281.             formatInfoBits = this.copyBit(8, 8, formatInfoBits);
  1282.             formatInfoBits = this.copyBit(8, 7, formatInfoBits);
  1283.             // .. and skip a bit in the timing pattern ...
  1284.             for (var j = 5; j >= 0; j--)
  1285.             {
  1286.                 formatInfoBits = this.copyBit(8, j, formatInfoBits);
  1287.             }
  1288.            
  1289.             this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);
  1290.             if (this.parsedFormatInfo != null)
  1291.             {
  1292.                 return this.parsedFormatInfo;
  1293.             }
  1294.            
  1295.             // Hmm, failed. Try the top-right/bottom-left pattern
  1296.             var dimension = this.bitMatrix.Dimension;
  1297.             formatInfoBits = 0;
  1298.             var iMin = dimension - 8;
  1299.             for (var i = dimension - 1; i >= iMin; i--)
  1300.             {
  1301.                 formatInfoBits = this.copyBit(i, 8, formatInfoBits);
  1302.             }
  1303.             for (var j = dimension - 7; j < dimension; j++)
  1304.             {
  1305.                 formatInfoBits = this.copyBit(8, j, formatInfoBits);
  1306.             }
  1307.            
  1308.             this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);
  1309.             if (this.parsedFormatInfo != null)
  1310.             {
  1311.                 return this.parsedFormatInfo;
  1312.             }
  1313.             throw "Error readFormatInformation";   
  1314.     }
  1315.     this.readVersion=function()
  1316.         {
  1317.            
  1318.             if (this.parsedVersion != null)
  1319.             {
  1320.                 return this.parsedVersion;
  1321.             }
  1322.            
  1323.             var dimension = this.bitMatrix.Dimension;
  1324.            
  1325.             var provisionalVersion = (dimension - 17) >> 2;
  1326.             if (provisionalVersion <= 6)
  1327.             {
  1328.                 return Version.getVersionForNumber(provisionalVersion);
  1329.             }
  1330.            
  1331.             // Read top-right version info: 3 wide by 6 tall
  1332.             var versionBits = 0;
  1333.             var ijMin = dimension - 11;
  1334.             for (var j = 5; j >= 0; j--)
  1335.             {
  1336.                 for (var i = dimension - 9; i >= ijMin; i--)
  1337.                 {
  1338.                     versionBits = this.copyBit(i, j, versionBits);
  1339.                 }
  1340.             }
  1341.            
  1342.             this.parsedVersion = Version.decodeVersionInformation(versionBits);
  1343.             if (this.parsedVersion != null && this.parsedVersion.DimensionForVersion == dimension)
  1344.             {
  1345.                 return this.parsedVersion;
  1346.             }
  1347.            
  1348.             // Hmm, failed. Try bottom left: 6 wide by 3 tall
  1349.             versionBits = 0;
  1350.             for (var i = 5; i >= 0; i--)
  1351.             {
  1352.                 for (var j = dimension - 9; j >= ijMin; j--)
  1353.                 {
  1354.                     versionBits = this.copyBit(i, j, versionBits);
  1355.                 }
  1356.             }
  1357.            
  1358.             this.parsedVersion = Version.decodeVersionInformation(versionBits);
  1359.             if (this.parsedVersion != null && this.parsedVersion.DimensionForVersion == dimension)
  1360.             {
  1361.                 return this.parsedVersion;
  1362.             }
  1363.             throw "Error readVersion";
  1364.         }
  1365.     this.readCodewords=function()
  1366.         {
  1367.            
  1368.             var formatInfo = this.readFormatInformation();
  1369.             var version = this.readVersion();
  1370.            
  1371.             // Get the data mask for the format used in this QR Code. This will exclude
  1372.             // some bits from reading as we wind through the bit matrix.
  1373.             var dataMask = DataMask.forReference( formatInfo.DataMask);
  1374.             var dimension = this.bitMatrix.Dimension;
  1375.             dataMask.unmaskBitMatrix(this.bitMatrix, dimension);
  1376.            
  1377.             var functionPattern = version.buildFunctionPattern();
  1378.            
  1379.             var readingUp = true;
  1380.             var result = new Array(version.TotalCodewords);
  1381.             var resultOffset = 0;
  1382.             var currentByte = 0;
  1383.             var bitsRead = 0;
  1384.             // Read columns in pairs, from right to left
  1385.             for (var j = dimension - 1; j > 0; j -= 2)
  1386.             {
  1387.                 if (j == 6)
  1388.                 {
  1389.                     // Skip whole column with vertical alignment pattern;
  1390.                     // saves time and makes the other code proceed more cleanly
  1391.                     j--;
  1392.                 }
  1393.                 // Read alternatingly from bottom to top then top to bottom
  1394.                 for (var count = 0; count < dimension; count++)
  1395.                 {
  1396.                     var i = readingUp?dimension - 1 - count:count;
  1397.                     for (var col = 0; col < 2; col++)
  1398.                     {
  1399.                         // Ignore bits covered by the function pattern
  1400.                         if (!functionPattern.get_Renamed(j - col, i))
  1401.                         {
  1402.                             // Read a bit
  1403.                             bitsRead++;
  1404.                             currentByte <<= 1;
  1405.                             if (this.bitMatrix.get_Renamed(j - col, i))
  1406.                             {
  1407.                                 currentByte |= 1;
  1408.                             }
  1409.                             // If we've made a whole byte, save it off
  1410.                             if (bitsRead == 8)
  1411.                             {
  1412.                                 result[resultOffset++] =  currentByte;
  1413.                                 bitsRead = 0;
  1414.                                 currentByte = 0;
  1415.                             }
  1416.                         }
  1417.                     }
  1418.                 }
  1419.                 readingUp ^= true; // readingUp = !readingUp; // switch directions
  1420.             }
  1421.             if (resultOffset != version.TotalCodewords)
  1422.             {
  1423.                 throw "Error readCodewords";
  1424.             }
  1425.             return result;
  1426.         }
  1427. }
  1428.  
  1429. /*
  1430.   Ported to JavaScript by Lazar Laszlo 2011
  1431.  
  1432.   lazarsoft@gmail.com, www.lazarsoft.info
  1433.  
  1434. */
  1435.  
  1436. /*
  1437. *
  1438. * Copyright 2007 ZXing authors
  1439. *
  1440. * Licensed under the Apache License, Version 2.0 (the "License");
  1441. * you may not use this file except in compliance with the License.
  1442. * You may obtain a copy of the License at
  1443. *
  1444. *      http://www.apache.org/licenses/LICENSE-2.0
  1445. *
  1446. * Unless required by applicable law or agreed to in writing, software
  1447. * distributed under the License is distributed on an "AS IS" BASIS,
  1448. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1449. * See the License for the specific language governing permissions and
  1450. * limitations under the License.
  1451. */
  1452.  
  1453.  
  1454. var DataMask = {};
  1455.  
  1456. DataMask.forReference = function(reference)
  1457. {
  1458.     if (reference < 0 || reference > 7)
  1459.     {
  1460.         throw "System.ArgumentException";
  1461.     }
  1462.     return DataMask.DATA_MASKS[reference];
  1463. }
  1464.  
  1465. function DataMask000()
  1466. {
  1467.     this.unmaskBitMatrix=function(bits,  dimension)
  1468.     {
  1469.         for (var i = 0; i < dimension; i++)
  1470.         {
  1471.             for (var j = 0; j < dimension; j++)
  1472.             {
  1473.                 if (this.isMasked(i, j))
  1474.                 {
  1475.                     bits.flip(j, i);
  1476.                 }
  1477.             }
  1478.         }
  1479.     }
  1480.     this.isMasked=function( i,  j)
  1481.     {
  1482.         return ((i + j) & 0x01) == 0;
  1483.     }
  1484. }
  1485.  
  1486. function DataMask001()
  1487. {
  1488.     this.unmaskBitMatrix=function(bits,  dimension)
  1489.     {
  1490.         for (var i = 0; i < dimension; i++)
  1491.         {
  1492.             for (var j = 0; j < dimension; j++)
  1493.             {
  1494.                 if (this.isMasked(i, j))
  1495.                 {
  1496.                     bits.flip(j, i);
  1497.                 }
  1498.             }
  1499.         }
  1500.     }
  1501.     this.isMasked=function( i,  j)
  1502.     {
  1503.         return (i & 0x01) == 0;
  1504.     }
  1505. }
  1506.  
  1507. function DataMask010()
  1508. {
  1509.     this.unmaskBitMatrix=function(bits,  dimension)
  1510.     {
  1511.         for (var i = 0; i < dimension; i++)
  1512.         {
  1513.             for (var j = 0; j < dimension; j++)
  1514.             {
  1515.                 if (this.isMasked(i, j))
  1516.                 {
  1517.                     bits.flip(j, i);
  1518.                 }
  1519.             }
  1520.         }
  1521.     }
  1522.     this.isMasked=function( i,  j)
  1523.     {
  1524.         return j % 3 == 0;
  1525.     }
  1526. }
  1527.  
  1528. function DataMask011()
  1529. {
  1530.     this.unmaskBitMatrix=function(bits,  dimension)
  1531.     {
  1532.         for (var i = 0; i < dimension; i++)
  1533.         {
  1534.             for (var j = 0; j < dimension; j++)
  1535.             {
  1536.                 if (this.isMasked(i, j))
  1537.                 {
  1538.                     bits.flip(j, i);
  1539.                 }
  1540.             }
  1541.         }
  1542.     }
  1543.     this.isMasked=function( i,  j)
  1544.     {
  1545.         return (i + j) % 3 == 0;
  1546.     }
  1547. }
  1548.  
  1549. function DataMask100()
  1550. {
  1551.     this.unmaskBitMatrix=function(bits,  dimension)
  1552.     {
  1553.         for (var i = 0; i < dimension; i++)
  1554.         {
  1555.             for (var j = 0; j < dimension; j++)
  1556.             {
  1557.                 if (this.isMasked(i, j))
  1558.                 {
  1559.                     bits.flip(j, i);
  1560.                 }
  1561.             }
  1562.         }
  1563.     }
  1564.     this.isMasked=function( i,  j)
  1565.     {
  1566.         return (((URShift(i, 1)) + (j / 3)) & 0x01) == 0;
  1567.     }
  1568. }
  1569.  
  1570. function DataMask101()
  1571. {
  1572.     this.unmaskBitMatrix=function(bits,  dimension)
  1573.     {
  1574.         for (var i = 0; i < dimension; i++)
  1575.         {
  1576.             for (var j = 0; j < dimension; j++)
  1577.             {
  1578.                 if (this.isMasked(i, j))
  1579.                 {
  1580.                     bits.flip(j, i);
  1581.                 }
  1582.             }
  1583.         }
  1584.     }
  1585.     this.isMasked=function( i,  j)
  1586.     {
  1587.         var temp = i * j;
  1588.         return (temp & 0x01) + (temp % 3) == 0;
  1589.     }
  1590. }
  1591.  
  1592. function DataMask110()
  1593. {
  1594.     this.unmaskBitMatrix=function(bits,  dimension)
  1595.     {
  1596.         for (var i = 0; i < dimension; i++)
  1597.         {
  1598.             for (var j = 0; j < dimension; j++)
  1599.             {
  1600.                 if (this.isMasked(i, j))
  1601.                 {
  1602.                     bits.flip(j, i);
  1603.                 }
  1604.             }
  1605.         }
  1606.     }
  1607.     this.isMasked=function( i,  j)
  1608.     {
  1609.         var temp = i * j;
  1610.         return (((temp & 0x01) + (temp % 3)) & 0x01) == 0;
  1611.     }
  1612. }
  1613. function DataMask111()
  1614. {
  1615.     this.unmaskBitMatrix=function(bits,  dimension)
  1616.     {
  1617.         for (var i = 0; i < dimension; i++)
  1618.         {
  1619.             for (var j = 0; j < dimension; j++)
  1620.             {
  1621.                 if (this.isMasked(i, j))
  1622.                 {
  1623.                     bits.flip(j, i);
  1624.                 }
  1625.             }
  1626.         }
  1627.     }
  1628.     this.isMasked=function( i,  j)
  1629.     {
  1630.         return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0;
  1631.     }
  1632. }
  1633.  
  1634. DataMask.DATA_MASKS = new Array(new DataMask000(), new DataMask001(), new DataMask010(), new DataMask011(), new DataMask100(), new DataMask101(), new DataMask110(), new DataMask111());
  1635.  
  1636.  
  1637. /*
  1638.   Ported to JavaScript by Lazar Laszlo 2011
  1639.  
  1640.   lazarsoft@gmail.com, www.lazarsoft.info
  1641.  
  1642. */
  1643.  
  1644. /*
  1645. *
  1646. * Copyright 2007 ZXing authors
  1647. *
  1648. * Licensed under the Apache License, Version 2.0 (the "License");
  1649. * you may not use this file except in compliance with the License.
  1650. * You may obtain a copy of the License at
  1651. *
  1652. *      http://www.apache.org/licenses/LICENSE-2.0
  1653. *
  1654. * Unless required by applicable law or agreed to in writing, software
  1655. * distributed under the License is distributed on an "AS IS" BASIS,
  1656. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1657. * See the License for the specific language governing permissions and
  1658. * limitations under the License.
  1659. */
  1660.  
  1661.  
  1662. function ReedSolomonDecoder(field)
  1663. {
  1664.     this.field = field;
  1665.     this.decode=function(received,  twoS)
  1666.     {
  1667.             var poly = new GF256Poly(this.field, received);
  1668.             var syndromeCoefficients = new Array(twoS);
  1669.             for(var i=0;i<syndromeCoefficients.length;i++)syndromeCoefficients[i]=0;
  1670.             var dataMatrix = false;//this.field.Equals(GF256.DATA_MATRIX_FIELD);
  1671.             var noError = true;
  1672.             for (var i = 0; i < twoS; i++)
  1673.             {
  1674.                 // Thanks to sanfordsquires for this fix:
  1675.                 var evalu = poly.evaluateAt(this.field.exp(dataMatrix?i + 1:i));
  1676.                 syndromeCoefficients[syndromeCoefficients.length - 1 - i] = evalu;
  1677.                 if (evalu != 0)
  1678.                 {
  1679.                     noError = false;
  1680.                 }
  1681.             }
  1682.             if (noError)
  1683.             {
  1684.                 return ;
  1685.             }
  1686.             var syndrome = new GF256Poly(this.field, syndromeCoefficients);
  1687.             var sigmaOmega = this.runEuclideanAlgorithm(this.field.buildMonomial(twoS, 1), syndrome, twoS);
  1688.             var sigma = sigmaOmega[0];
  1689.             var omega = sigmaOmega[1];
  1690.             var errorLocations = this.findErrorLocations(sigma);
  1691.             var errorMagnitudes = this.findErrorMagnitudes(omega, errorLocations, dataMatrix);
  1692.             for (var i = 0; i < errorLocations.length; i++)
  1693.             {
  1694.                 var position = received.length - 1 - this.field.log(errorLocations[i]);
  1695.                 if (position < 0)
  1696.                 {
  1697.                     throw "ReedSolomonException Bad error location";
  1698.                 }
  1699.                 received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]);
  1700.             }
  1701.     }
  1702.    
  1703.     this.runEuclideanAlgorithm=function( a,  b,  R)
  1704.         {
  1705.             // Assume a's degree is >= b's
  1706.             if (a.Degree < b.Degree)
  1707.             {
  1708.                 var temp = a;
  1709.                 a = b;
  1710.                 b = temp;
  1711.             }
  1712.            
  1713.             var rLast = a;
  1714.             var r = b;
  1715.             var sLast = this.field.One;
  1716.             var s = this.field.Zero;
  1717.             var tLast = this.field.Zero;
  1718.             var t = this.field.One;
  1719.            
  1720.             // Run Euclidean algorithm until r's degree is less than R/2
  1721.             while (r.Degree >= Math.floor(R / 2))
  1722.             {
  1723.                 var rLastLast = rLast;
  1724.                 var sLastLast = sLast;
  1725.                 var tLastLast = tLast;
  1726.                 rLast = r;
  1727.                 sLast = s;
  1728.                 tLast = t;
  1729.                
  1730.                 // Divide rLastLast by rLast, with quotient in q and remainder in r
  1731.                 if (rLast.Zero)
  1732.                 {
  1733.                     // Oops, Euclidean algorithm already terminated?
  1734.                     throw "r_{i-1} was zero";
  1735.                 }
  1736.                 r = rLastLast;
  1737.                 var q = this.field.Zero;
  1738.                 var denominatorLeadingTerm = rLast.getCoefficient(rLast.Degree);
  1739.                 var dltInverse = this.field.inverse(denominatorLeadingTerm);
  1740.                 while (r.Degree >= rLast.Degree && !r.Zero)
  1741.                 {
  1742.                     var degreeDiff = r.Degree - rLast.Degree;
  1743.                     var scale = this.field.multiply(r.getCoefficient(r.Degree), dltInverse);
  1744.                     q = q.addOrSubtract(this.field.buildMonomial(degreeDiff, scale));
  1745.                     r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale));
  1746.                     //r.EXE();
  1747.                 }
  1748.                
  1749.                 s = q.multiply1(sLast).addOrSubtract(sLastLast);
  1750.                 t = q.multiply1(tLast).addOrSubtract(tLastLast);
  1751.             }
  1752.            
  1753.             var sigmaTildeAtZero = t.getCoefficient(0);
  1754.             if (sigmaTildeAtZero == 0)
  1755.             {
  1756.                 throw "ReedSolomonException sigmaTilde(0) was zero";
  1757.             }
  1758.            
  1759.             var inverse = this.field.inverse(sigmaTildeAtZero);
  1760.             var sigma = t.multiply2(inverse);
  1761.             var omega = r.multiply2(inverse);
  1762.             return new Array(sigma, omega);
  1763.         }
  1764.     this.findErrorLocations=function( errorLocator)
  1765.         {
  1766.             // This is a direct application of Chien's search
  1767.             var numErrors = errorLocator.Degree;
  1768.             if (numErrors == 1)
  1769.             {
  1770.                 // shortcut
  1771.                 return new Array(errorLocator.getCoefficient(1));
  1772.             }
  1773.             var result = new Array(numErrors);
  1774.             var e = 0;
  1775.             for (var i = 1; i < 256 && e < numErrors; i++)
  1776.             {
  1777.                 if (errorLocator.evaluateAt(i) == 0)
  1778.                 {
  1779.                     result[e] = this.field.inverse(i);
  1780.                     e++;
  1781.                 }
  1782.             }
  1783.             if (e != numErrors)
  1784.             {
  1785.                 throw "Error locator degree does not match number of roots";
  1786.             }
  1787.             return result;
  1788.         }
  1789.     this.findErrorMagnitudes=function( errorEvaluator,  errorLocations,  dataMatrix)
  1790.         {
  1791.             // This is directly applying Forney's Formula
  1792.             var s = errorLocations.length;
  1793.             var result = new Array(s);
  1794.             for (var i = 0; i < s; i++)
  1795.             {
  1796.                 var xiInverse = this.field.inverse(errorLocations[i]);
  1797.                 var denominator = 1;
  1798.                 for (var j = 0; j < s; j++)
  1799.                 {
  1800.                     if (i != j)
  1801.                     {
  1802.                         denominator = this.field.multiply(denominator, GF256.addOrSubtract(1, this.field.multiply(errorLocations[j], xiInverse)));
  1803.                     }
  1804.                 }
  1805.                 result[i] = this.field.multiply(errorEvaluator.evaluateAt(xiInverse), this.field.inverse(denominator));
  1806.                 // Thanks to sanfordsquires for this fix:
  1807.                 if (dataMatrix)
  1808.                 {
  1809.                     result[i] = this.field.multiply(result[i], xiInverse);
  1810.                 }
  1811.             }
  1812.             return result;
  1813.         }
  1814. }
  1815.  
  1816. /*
  1817.   Ported to JavaScript by Lazar Laszlo 2011
  1818.  
  1819.   lazarsoft@gmail.com, www.lazarsoft.info
  1820.  
  1821. */
  1822.  
  1823. /*
  1824. *
  1825. * Copyright 2007 ZXing authors
  1826. *
  1827. * Licensed under the Apache License, Version 2.0 (the "License");
  1828. * you may not use this file except in compliance with the License.
  1829. * You may obtain a copy of the License at
  1830. *
  1831. *      http://www.apache.org/licenses/LICENSE-2.0
  1832. *
  1833. * Unless required by applicable law or agreed to in writing, software
  1834. * distributed under the License is distributed on an "AS IS" BASIS,
  1835. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1836. * See the License for the specific language governing permissions and
  1837. * limitations under the License.
  1838. */
  1839.  
  1840.  
  1841. function GF256Poly(field,  coefficients)
  1842. {
  1843.     if (coefficients == null || coefficients.length == 0)
  1844.     {
  1845.         throw "System.ArgumentException";
  1846.     }
  1847.     this.field = field;
  1848.     var coefficientsLength = coefficients.length;
  1849.     if (coefficientsLength > 1 && coefficients[0] == 0)
  1850.     {
  1851.         // Leading term must be non-zero for anything except the constant polynomial "0"
  1852.         var firstNonZero = 1;
  1853.         while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0)
  1854.         {
  1855.             firstNonZero++;
  1856.         }
  1857.         if (firstNonZero == coefficientsLength)
  1858.         {
  1859.             this.coefficients = field.Zero.coefficients;
  1860.         }
  1861.         else
  1862.         {
  1863.             this.coefficients = new Array(coefficientsLength - firstNonZero);
  1864.             for(var i=0;i<this.coefficients.length;i++)this.coefficients[i]=0;
  1865.             //Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length);
  1866.             for(var ci=0;ci<this.coefficients.length;ci++)this.coefficients[ci]=coefficients[firstNonZero+ci];
  1867.         }
  1868.     }
  1869.     else
  1870.     {
  1871.         this.coefficients = coefficients;
  1872.     }
  1873.    
  1874.     this.__defineGetter__("Zero", function()
  1875.     {
  1876.         return this.coefficients[0] == 0;
  1877.     });
  1878.     this.__defineGetter__("Degree", function()
  1879.     {
  1880.         return this.coefficients.length - 1;
  1881.     });
  1882.     this.__defineGetter__("Coefficients", function()
  1883.     {
  1884.         return this.coefficients;
  1885.     });
  1886.    
  1887.     this.getCoefficient=function( degree)
  1888.     {
  1889.         return this.coefficients[this.coefficients.length - 1 - degree];
  1890.     }
  1891.    
  1892.     this.evaluateAt=function( a)
  1893.     {
  1894.         if (a == 0)
  1895.         {
  1896.             // Just return the x^0 coefficient
  1897.             return this.getCoefficient(0);
  1898.         }
  1899.         var size = this.coefficients.length;
  1900.         if (a == 1)
  1901.         {
  1902.             // Just the sum of the coefficients
  1903.             var result = 0;
  1904.             for (var i = 0; i < size; i++)
  1905.             {
  1906.                 result = GF256.addOrSubtract(result, this.coefficients[i]);
  1907.             }
  1908.             return result;
  1909.         }
  1910.         var result2 = this.coefficients[0];
  1911.         for (var i = 1; i < size; i++)
  1912.         {
  1913.             result2 = GF256.addOrSubtract(this.field.multiply(a, result2), this.coefficients[i]);
  1914.         }
  1915.         return result2;
  1916.     }
  1917.    
  1918.     this.addOrSubtract=function( other)
  1919.         {
  1920.             if (this.field != other.field)
  1921.             {
  1922.                 throw "GF256Polys do not have same GF256 field";
  1923.             }
  1924.             if (this.Zero)
  1925.             {
  1926.                 return other;
  1927.             }
  1928.             if (other.Zero)
  1929.             {
  1930.                 return this;
  1931.             }
  1932.            
  1933.             var smallerCoefficients = this.coefficients;
  1934.             var largerCoefficients = other.coefficients;
  1935.             if (smallerCoefficients.length > largerCoefficients.length)
  1936.             {
  1937.                 var temp = smallerCoefficients;
  1938.                 smallerCoefficients = largerCoefficients;
  1939.                 largerCoefficients = temp;
  1940.             }
  1941.             var sumDiff = new Array(largerCoefficients.length);
  1942.             var lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  1943.             // Copy high-order terms only found in higher-degree polynomial's coefficients
  1944.             //Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  1945.             for(var ci=0;ci<lengthDiff;ci++)sumDiff[ci]=largerCoefficients[ci];
  1946.            
  1947.             for (var i = lengthDiff; i < largerCoefficients.length; i++)
  1948.             {
  1949.                 sumDiff[i] = GF256.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  1950.             }
  1951.            
  1952.             return new GF256Poly(field, sumDiff);
  1953.     }
  1954.     this.multiply1=function( other)
  1955.         {
  1956.             if (this.field!=other.field)
  1957.             {
  1958.                 throw "GF256Polys do not have same GF256 field";
  1959.             }
  1960.             if (this.Zero || other.Zero)
  1961.             {
  1962.                 return this.field.Zero;
  1963.             }
  1964.             var aCoefficients = this.coefficients;
  1965.             var aLength = aCoefficients.length;
  1966.             var bCoefficients = other.coefficients;
  1967.             var bLength = bCoefficients.length;
  1968.             var product = new Array(aLength + bLength - 1);
  1969.             for (var i = 0; i < aLength; i++)
  1970.             {
  1971.                 var aCoeff = aCoefficients[i];
  1972.                 for (var j = 0; j < bLength; j++)
  1973.                 {
  1974.                     product[i + j] = GF256.addOrSubtract(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
  1975.                 }
  1976.             }
  1977.             return new GF256Poly(this.field, product);
  1978.         }
  1979.     this.multiply2=function( scalar)
  1980.         {
  1981.             if (scalar == 0)
  1982.             {
  1983.                 return this.field.Zero;
  1984.             }
  1985.             if (scalar == 1)
  1986.             {
  1987.                 return this;
  1988.             }
  1989.             var size = this.coefficients.length;
  1990.             var product = new Array(size);
  1991.             for (var i = 0; i < size; i++)
  1992.             {
  1993.                 product[i] = this.field.multiply(this.coefficients[i], scalar);
  1994.             }
  1995.             return new GF256Poly(this.field, product);
  1996.         }
  1997.     this.multiplyByMonomial=function( degree,  coefficient)
  1998.         {
  1999.             if (degree < 0)
  2000.             {
  2001.                 throw "System.ArgumentException";
  2002.             }
  2003.             if (coefficient == 0)
  2004.             {
  2005.                 return this.field.Zero;
  2006.             }
  2007.             var size = this.coefficients.length;
  2008.             var product = new Array(size + degree);
  2009.             for(var i=0;i<product.length;i++)product[i]=0;
  2010.             for (var i = 0; i < size; i++)
  2011.             {
  2012.                 product[i] = this.field.multiply(this.coefficients[i], coefficient);
  2013.             }
  2014.             return new GF256Poly(this.field, product);
  2015.         }
  2016.     this.divide=function( other)
  2017.         {
  2018.             if (this.field!=other.field)
  2019.             {
  2020.                 throw "GF256Polys do not have same GF256 field";
  2021.             }
  2022.             if (other.Zero)
  2023.             {
  2024.                 throw "Divide by 0";
  2025.             }
  2026.            
  2027.             var quotient = this.field.Zero;
  2028.             var remainder = this;
  2029.            
  2030.             var denominatorLeadingTerm = other.getCoefficient(other.Degree);
  2031.             var inverseDenominatorLeadingTerm = this.field.inverse(denominatorLeadingTerm);
  2032.            
  2033.             while (remainder.Degree >= other.Degree && !remainder.Zero)
  2034.             {
  2035.                 var degreeDifference = remainder.Degree - other.Degree;
  2036.                 var scale = this.field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
  2037.                 var term = other.multiplyByMonomial(degreeDifference, scale);
  2038.                 var iterationQuotient = this.field.buildMonomial(degreeDifference, scale);
  2039.                 quotient = quotient.addOrSubtract(iterationQuotient);
  2040.                 remainder = remainder.addOrSubtract(term);
  2041.             }
  2042.            
  2043.             return new Array(quotient, remainder);
  2044.         }
  2045. }
  2046.  
  2047.  
  2048. /*
  2049.   Ported to JavaScript by Lazar Laszlo 2011
  2050.  
  2051.   lazarsoft@gmail.com, www.lazarsoft.info
  2052.  
  2053. */
  2054.  
  2055. /*
  2056. *
  2057. * Copyright 2007 ZXing authors
  2058. *
  2059. * Licensed under the Apache License, Version 2.0 (the "License");
  2060. * you may not use this file except in compliance with the License.
  2061. * You may obtain a copy of the License at
  2062. *
  2063. *      http://www.apache.org/licenses/LICENSE-2.0
  2064. *
  2065. * Unless required by applicable law or agreed to in writing, software
  2066. * distributed under the License is distributed on an "AS IS" BASIS,
  2067. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2068. * See the License for the specific language governing permissions and
  2069. * limitations under the License.
  2070. */
  2071.  
  2072.  
  2073. function GF256( primitive)
  2074. {
  2075.     this.expTable = new Array(256);
  2076.     this.logTable = new Array(256);
  2077.     var x = 1;
  2078.     for (var i = 0; i < 256; i++)
  2079.     {
  2080.         this.expTable[i] = x;
  2081.         x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
  2082.         if (x >= 0x100)
  2083.         {
  2084.             x ^= primitive;
  2085.         }
  2086.     }
  2087.     for (var i = 0; i < 255; i++)
  2088.     {
  2089.         this.logTable[this.expTable[i]] = i;
  2090.     }
  2091.     // logTable[0] == 0 but this should never be used
  2092.     var at0=new Array(1);at0[0]=0;
  2093.     this.zero = new GF256Poly(this, new Array(at0));
  2094.     var at1=new Array(1);at1[0]=1;
  2095.     this.one = new GF256Poly(this, new Array(at1));
  2096.    
  2097.     this.__defineGetter__("Zero", function()
  2098.     {
  2099.         return this.zero;
  2100.     });
  2101.     this.__defineGetter__("One", function()
  2102.     {
  2103.         return this.one;
  2104.     });
  2105.     this.buildMonomial=function( degree,  coefficient)
  2106.         {
  2107.             if (degree < 0)
  2108.             {
  2109.                 throw "System.ArgumentException";
  2110.             }
  2111.             if (coefficient == 0)
  2112.             {
  2113.                 return this.zero;
  2114.             }
  2115.             var coefficients = new Array(degree + 1);
  2116.             for(var i=0;i<coefficients.length;i++)coefficients[i]=0;
  2117.             coefficients[0] = coefficient;
  2118.             return new GF256Poly(this, coefficients);
  2119.         }
  2120.     this.exp=function( a)
  2121.         {
  2122.             return this.expTable[a];
  2123.         }
  2124.     this.log=function( a)
  2125.         {
  2126.             if (a == 0)
  2127.             {
  2128.                 throw "System.ArgumentException";
  2129.             }
  2130.             return this.logTable[a];
  2131.         }
  2132.     this.inverse=function( a)
  2133.         {
  2134.             if (a == 0)
  2135.             {
  2136.                 throw "System.ArithmeticException";
  2137.             }
  2138.             return this.expTable[255 - this.logTable[a]];
  2139.         }
  2140.     this.multiply=function( a,  b)
  2141.         {
  2142.             if (a == 0 || b == 0)
  2143.             {
  2144.                 return 0;
  2145.             }
  2146.             if (a == 1)
  2147.             {
  2148.                 return b;
  2149.             }
  2150.             if (b == 1)
  2151.             {
  2152.                 return a;
  2153.             }
  2154.             return this.expTable[(this.logTable[a] + this.logTable[b]) % 255];
  2155.         }      
  2156. }
  2157.  
  2158. GF256.QR_CODE_FIELD = new GF256(0x011D);
  2159. GF256.DATA_MATRIX_FIELD = new GF256(0x012D);
  2160.  
  2161. GF256.addOrSubtract=function( a,  b)
  2162. {
  2163.     return a ^ b;
  2164. }
  2165.  
  2166.  
  2167. /*
  2168.   Ported to JavaScript by Lazar Laszlo 2011
  2169.  
  2170.   lazarsoft@gmail.com, www.lazarsoft.info
  2171.  
  2172. */
  2173.  
  2174. /*
  2175. *
  2176. * Copyright 2007 ZXing authors
  2177. *
  2178. * Licensed under the Apache License, Version 2.0 (the "License");
  2179. * you may not use this file except in compliance with the License.
  2180. * You may obtain a copy of the License at
  2181. *
  2182. *      http://www.apache.org/licenses/LICENSE-2.0
  2183. *
  2184. * Unless required by applicable law or agreed to in writing, software
  2185. * distributed under the License is distributed on an "AS IS" BASIS,
  2186. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2187. * See the License for the specific language governing permissions and
  2188. * limitations under the License.
  2189. */
  2190.  
  2191.  
  2192. var Decoder={};
  2193. Decoder.rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
  2194.  
  2195. Decoder.correctErrors=function( codewordBytes,  numDataCodewords)
  2196. {
  2197.     var numCodewords = codewordBytes.length;
  2198.     // First read into an array of ints
  2199.     var codewordsInts = new Array(numCodewords);
  2200.     for (var i = 0; i < numCodewords; i++)
  2201.     {
  2202.         codewordsInts[i] = codewordBytes[i] & 0xFF;
  2203.     }
  2204.     var numECCodewords = codewordBytes.length - numDataCodewords;
  2205.     try
  2206.     {
  2207.         Decoder.rsDecoder.decode(codewordsInts, numECCodewords);
  2208.         //var corrector = new ReedSolomon(codewordsInts, numECCodewords);
  2209.         //corrector.correct();
  2210.     }
  2211.     catch ( rse)
  2212.     {
  2213.         throw rse;
  2214.     }
  2215.     // Copy back into array of bytes -- only need to worry about the bytes that were data
  2216.     // We don't care about errors in the error-correction codewords
  2217.     for (var i = 0; i < numDataCodewords; i++)
  2218.     {
  2219.         codewordBytes[i] =  codewordsInts[i];
  2220.     }
  2221. }
  2222.  
  2223. Decoder.decode=function(bits)
  2224. {
  2225.     var parser = new BitMatrixParser(bits);
  2226.     var version = parser.readVersion();
  2227.     var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel;
  2228.    
  2229.     // Read codewords
  2230.     var codewords = parser.readCodewords();
  2231.  
  2232.     // Separate into data blocks
  2233.     var dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);
  2234.    
  2235.     // Count total number of data bytes
  2236.     var totalBytes = 0;
  2237.     for (var i = 0; i < dataBlocks.length; i++)
  2238.     {
  2239.         totalBytes += dataBlocks[i].NumDataCodewords;
  2240.     }
  2241.     var resultBytes = new Array(totalBytes);
  2242.     var resultOffset = 0;
  2243.    
  2244.     // Error-correct and copy data blocks together into a stream of bytes
  2245.     for (var j = 0; j < dataBlocks.length; j++)
  2246.     {
  2247.         var dataBlock = dataBlocks[j];
  2248.         var codewordBytes = dataBlock.Codewords;
  2249.         var numDataCodewords = dataBlock.NumDataCodewords;
  2250.         Decoder.correctErrors(codewordBytes, numDataCodewords);
  2251.         for (var i = 0; i < numDataCodewords; i++)
  2252.         {
  2253.             resultBytes[resultOffset++] = codewordBytes[i];
  2254.         }
  2255.     }
  2256.    
  2257.     // Decode the contents of that stream of bytes
  2258.     var reader = new QRCodeDataBlockReader(resultBytes, version.VersionNumber, ecLevel.Bits);
  2259.     return reader;
  2260.     //return DecodedBitStreamParser.decode(resultBytes, version, ecLevel);
  2261. }
  2262.  
  2263.  
  2264. /*
  2265.    Copyright 2011 Lazar Laszlo (lazarsoft@gmail.com, www.lazarsoft.info)
  2266.    
  2267.    Licensed under the Apache License, Version 2.0 (the "License");
  2268.    you may not use this file except in compliance with the License.
  2269.    You may obtain a copy of the License at
  2270.  
  2271.        http://www.apache.org/licenses/LICENSE-2.0
  2272.  
  2273.    Unless required by applicable law or agreed to in writing, software
  2274.    distributed under the License is distributed on an "AS IS" BASIS,
  2275.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2276.    See the License for the specific language governing permissions and
  2277.    limitations under the License.
  2278. */
  2279.  
  2280.  
  2281. var qrcode = {};
  2282. qrcode.imagedata = null;
  2283. qrcode.width = 0;
  2284. qrcode.height = 0;
  2285. qrcode.qrCodeSymbol = null;
  2286. qrcode.debug = false;
  2287. qrcode.maxImgSize = 1024*1024;
  2288.  
  2289. qrcode.sizeOfDataLengthInfo =  [  [ 10, 9, 8, 8 ],  [ 12, 11, 16, 10 ],  [ 14, 13, 16, 12 ] ];
  2290.  
  2291. qrcode.callback = null;
  2292.  
  2293. qrcode.vidSuccess = function (stream)
  2294. {
  2295.     qrcode.localstream = stream;
  2296.     if(qrcode.webkit)
  2297.         qrcode.video.src = window.webkitURL.createObjectURL(stream);
  2298.     else
  2299.     if(qrcode.moz)
  2300.     {
  2301.         qrcode.video.mozSrcObject = stream;
  2302.         qrcode.video.play();
  2303.     }
  2304.     else
  2305.         qrcode.video.src = stream;
  2306.    
  2307.     qrcode.gUM=true;
  2308.    
  2309.     qrcode.canvas_qr2 = document.createElement('canvas');
  2310.     qrcode.canvas_qr2.id = "qr-canvas";
  2311.     qrcode.qrcontext2 = qrcode.canvas_qr2.getContext('2d');
  2312.     qrcode.canvas_qr2.width = qrcode.video.videoWidth;
  2313.     qrcode.canvas_qr2.height = qrcode.video.videoHeight;
  2314.     setTimeout(qrcode.captureToCanvas, 500);
  2315. }
  2316.        
  2317. qrcode.vidError = function(error)
  2318. {
  2319.     qrcode.gUM=false;
  2320.     return;
  2321. }
  2322.  
  2323. qrcode.captureToCanvas = function()
  2324. {
  2325.     if(qrcode.gUM)
  2326.     {
  2327.         try{
  2328.             if(qrcode.video.videoWidth == 0)
  2329.             {
  2330.                 setTimeout(qrcode.captureToCanvas, 500);
  2331.                 return;
  2332.             }
  2333.             else
  2334.             {
  2335.                 qrcode.canvas_qr2.width = qrcode.video.videoWidth;
  2336.                 qrcode.canvas_qr2.height = qrcode.video.videoHeight;
  2337.             }
  2338.             qrcode.qrcontext2.drawImage(qrcode.video,0,0);
  2339.             try{
  2340.                 qrcode.decode();
  2341.             }
  2342.             catch(e){      
  2343.                 console.log(e);
  2344.                 setTimeout(qrcode.captureToCanvas, 500);
  2345.             };
  2346.         }
  2347.         catch(e){      
  2348.                 console.log(e);
  2349.                 setTimeout(qrcode.captureToCanvas, 500);
  2350.         };
  2351.     }
  2352. }
  2353.  
  2354. qrcode.setWebcam = function(videoId)
  2355. {
  2356.     var n=navigator;
  2357.     qrcode.video=document.getElementById(videoId);
  2358.  
  2359.     var options = true;
  2360.     if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices)
  2361.     {
  2362.         try{
  2363.             navigator.mediaDevices.enumerateDevices()
  2364.             .then(function(devices) {
  2365.               devices.forEach(function(device) {
  2366.                 console.log("deb1");
  2367.                 if (device.kind === 'videoinput') {
  2368.                   if(device.label.toLowerCase().search("back") >-1)
  2369.                     options=[{'sourceId': device.deviceId}] ;
  2370.                 }
  2371.                 console.log(device.kind + ": " + device.label +
  2372.                             " id = " + device.deviceId);
  2373.               });
  2374.             })
  2375.            
  2376.         }
  2377.         catch(e)
  2378.         {
  2379.             console.log(e);
  2380.         }
  2381.     }
  2382.     else{
  2383.         console.log("no navigator.mediaDevices.enumerateDevices" );
  2384.     }
  2385.    
  2386.     if(n.getUserMedia)
  2387.         n.getUserMedia({video: options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  2388.     else
  2389.     if(n.webkitGetUserMedia)
  2390.     {
  2391.         qrcode.webkit=true;
  2392.         n.webkitGetUserMedia({video:options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  2393.     }
  2394.     else
  2395.     if(n.mozGetUserMedia)
  2396.     {
  2397.         qrcode.moz=true;
  2398.         n.mozGetUserMedia({video: options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  2399.     }
  2400. }
  2401.  
  2402. qrcode.decode = function(src){
  2403.    
  2404.     if(arguments.length==0)
  2405.     {
  2406.         if(qrcode.canvas_qr2)
  2407.         {
  2408.             var canvas_qr = qrcode.canvas_qr2;
  2409.             var context = qrcode.qrcontext2;
  2410.         }  
  2411.         else
  2412.         {
  2413.             var canvas_qr = document.getElementById("qr-canvas");
  2414.             var context = canvas_qr.getContext('2d');
  2415.         }
  2416.         qrcode.width = canvas_qr.width;
  2417.         qrcode.height = canvas_qr.height;
  2418.         qrcode.imagedata = context.getImageData(0, 0, qrcode.width, qrcode.height);
  2419.         qrcode.result = qrcode.process(context);
  2420.         if(qrcode.callback!=null)
  2421.             qrcode.callback(qrcode.result);
  2422.         return qrcode.result;
  2423.     }
  2424.     else
  2425.     {
  2426.         var image = new Image();
  2427.         image.crossOrigin = "Anonymous";
  2428.         image.onload=function(){
  2429.             //var canvas_qr = document.getElementById("qr-canvas");
  2430.             var canvas_out = document.getElementById("out-canvas");
  2431.             if(canvas_out!=null)
  2432.             {
  2433.                 var outctx = canvas_out.getContext('2d');
  2434.                 outctx.clearRect(0, 0, 320, 240);
  2435.                 outctx.drawImage(image, 0, 0, 320, 240);
  2436.             }
  2437.  
  2438.             var canvas_qr = document.createElement('canvas');
  2439.             var context = canvas_qr.getContext('2d');
  2440.             var nheight = image.height;
  2441.             var nwidth = image.width;
  2442.             if(image.width*image.height>qrcode.maxImgSize)
  2443.             {
  2444.                 var ir = image.width / image.height;
  2445.                 nheight = Math.sqrt(qrcode.maxImgSize/ir);
  2446.                 nwidth=ir*nheight;
  2447.             }
  2448.  
  2449.             canvas_qr.width = nwidth;
  2450.             canvas_qr.height = nheight;
  2451.            
  2452.             context.drawImage(image, 0, 0, canvas_qr.width, canvas_qr.height );
  2453.             qrcode.width = canvas_qr.width;
  2454.             qrcode.height = canvas_qr.height;
  2455.             try{
  2456.                 qrcode.imagedata = context.getImageData(0, 0, canvas_qr.width, canvas_qr.height);
  2457.             }catch(e){
  2458.                 qrcode.result = "Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!";
  2459.                 if(qrcode.callback!=null)
  2460.                     qrcode.callback(qrcode.result);
  2461.                 return;
  2462.             }
  2463.            
  2464.             try
  2465.             {
  2466.                 qrcode.result = qrcode.process(context);
  2467.             }
  2468.             catch(e)
  2469.             {
  2470.                 console.log(e);
  2471.                 qrcode.result = "error decoding QR Code";
  2472.             }
  2473.             if(qrcode.callback!=null)
  2474.                 qrcode.callback(qrcode.result);
  2475.         }
  2476.         image.onerror = function ()
  2477.         {
  2478.             if(qrcode.callback!=null)
  2479.                 qrcode.callback("Failed to load the image");
  2480.         }
  2481.         image.src = src;
  2482.     }
  2483. }
  2484.  
  2485. qrcode.isUrl = function(s)
  2486. {
  2487.     var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  2488.     return regexp.test(s);
  2489. }
  2490.  
  2491. qrcode.decode_url = function (s)
  2492. {
  2493.   var escaped = "";
  2494.   try{
  2495.     escaped = escape( s );
  2496.   }
  2497.   catch(e)
  2498.   {
  2499.     console.log(e);
  2500.     escaped = s;
  2501.   }
  2502.   var ret = "";
  2503.   try{
  2504.     ret = decodeURIComponent( escaped );
  2505.   }
  2506.   catch(e)
  2507.   {
  2508.     console.log(e);
  2509.     ret = escaped;
  2510.   }
  2511.   return ret;
  2512. }
  2513.  
  2514. qrcode.decode_utf8 = function ( s )
  2515. {
  2516.     if(qrcode.isUrl(s))
  2517.         return qrcode.decode_url(s);
  2518.     else
  2519.         return s;
  2520. }
  2521.  
  2522. qrcode.process = function(ctx){
  2523.    
  2524.     var start = new Date().getTime();
  2525.  
  2526.     var image = qrcode.grayScaleToBitmap(qrcode.grayscale());
  2527.     //var image = qrcode.binarize(128);
  2528.    
  2529.     if(qrcode.debug)
  2530.     {
  2531.         for (var y = 0; y < qrcode.height; y++)
  2532.         {
  2533.             for (var x = 0; x < qrcode.width; x++)
  2534.             {
  2535.                 var point = (x * 4) + (y * qrcode.width * 4);
  2536.                 qrcode.imagedata.data[point] = image[x+y*qrcode.width]?0:0;
  2537.                 qrcode.imagedata.data[point+1] = image[x+y*qrcode.width]?0:0;
  2538.                 qrcode.imagedata.data[point+2] = image[x+y*qrcode.width]?255:0;
  2539.             }
  2540.         }
  2541.         ctx.putImageData(qrcode.imagedata, 0, 0);
  2542.     }
  2543.    
  2544.     //var finderPatternInfo = new FinderPatternFinder().findFinderPattern(image);
  2545.    
  2546.     var detector = new Detector(image);
  2547.  
  2548.     var qRCodeMatrix = detector.detect();
  2549.    
  2550.     if(qrcode.debug)
  2551.     {
  2552.         for (var y = 0; y < qRCodeMatrix.bits.Height; y++)
  2553.         {
  2554.             for (var x = 0; x < qRCodeMatrix.bits.Width; x++)
  2555.             {
  2556.                 var point = (x * 4*2) + (y*2 * qrcode.width * 4);
  2557.                 qrcode.imagedata.data[point] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  2558.                 qrcode.imagedata.data[point+1] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  2559.                 qrcode.imagedata.data[point+2] = qRCodeMatrix.bits.get_Renamed(x,y)?255:0;
  2560.             }
  2561.         }
  2562.         ctx.putImageData(qrcode.imagedata, 0, 0);
  2563.     }
  2564.    
  2565.    
  2566.     var reader = Decoder.decode(qRCodeMatrix.bits);
  2567.     var data = reader.DataByte;
  2568.     var str="";
  2569.     for(var i=0;i<data.length;i++)
  2570.     {
  2571.         for(var j=0;j<data[i].length;j++)
  2572.             str+=String.fromCharCode(data[i][j]);
  2573.     }
  2574.    
  2575.     var end = new Date().getTime();
  2576.     var time = end - start;
  2577.     console.log(time);
  2578.    
  2579.     return qrcode.decode_utf8(str);
  2580.     //alert("Time:" + time + " Code: "+str);
  2581. }
  2582.  
  2583. qrcode.getPixel = function(x,y){
  2584.     if (qrcode.width < x) {
  2585.         throw "point error";
  2586.     }
  2587.     if (qrcode.height < y) {
  2588.         throw "point error";
  2589.     }
  2590.     var point = (x * 4) + (y * qrcode.width * 4);
  2591.     var p = (qrcode.imagedata.data[point]*33 + qrcode.imagedata.data[point + 1]*34 + qrcode.imagedata.data[point + 2]*33)/100;
  2592.     return p;
  2593. }
  2594.  
  2595. qrcode.binarize = function(th){
  2596.     var ret = new Array(qrcode.width*qrcode.height);
  2597.     for (var y = 0; y < qrcode.height; y++)
  2598.     {
  2599.         for (var x = 0; x < qrcode.width; x++)
  2600.         {
  2601.             var gray = qrcode.getPixel(x, y);
  2602.            
  2603.             ret[x+y*qrcode.width] = gray<=th?true:false;
  2604.         }
  2605.     }
  2606.     return ret;
  2607. }
  2608.  
  2609. qrcode.getMiddleBrightnessPerArea=function(image)
  2610. {
  2611.     var numSqrtArea = 4;
  2612.     //obtain middle brightness((min + max) / 2) per area
  2613.     var areaWidth = Math.floor(qrcode.width / numSqrtArea);
  2614.     var areaHeight = Math.floor(qrcode.height / numSqrtArea);
  2615.     var minmax = new Array(numSqrtArea);
  2616.     for (var i = 0; i < numSqrtArea; i++)
  2617.     {
  2618.         minmax[i] = new Array(numSqrtArea);
  2619.         for (var i2 = 0; i2 < numSqrtArea; i2++)
  2620.         {
  2621.             minmax[i][i2] = new Array(0,0);
  2622.         }
  2623.     }
  2624.     for (var ay = 0; ay < numSqrtArea; ay++)
  2625.     {
  2626.         for (var ax = 0; ax < numSqrtArea; ax++)
  2627.         {
  2628.             minmax[ax][ay][0] = 0xFF;
  2629.             for (var dy = 0; dy < areaHeight; dy++)
  2630.             {
  2631.                 for (var dx = 0; dx < areaWidth; dx++)
  2632.                 {
  2633.                     var target = image[areaWidth * ax + dx+(areaHeight * ay + dy)*qrcode.width];
  2634.                     if (target < minmax[ax][ay][0])
  2635.                         minmax[ax][ay][0] = target;
  2636.                     if (target > minmax[ax][ay][1])
  2637.                         minmax[ax][ay][1] = target;
  2638.                 }
  2639.             }
  2640.             //minmax[ax][ay][0] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
  2641.         }
  2642.     }
  2643.     var middle = new Array(numSqrtArea);
  2644.     for (var i3 = 0; i3 < numSqrtArea; i3++)
  2645.     {
  2646.         middle[i3] = new Array(numSqrtArea);
  2647.     }
  2648.     for (var ay = 0; ay < numSqrtArea; ay++)
  2649.     {
  2650.         for (var ax = 0; ax < numSqrtArea; ax++)
  2651.         {
  2652.             middle[ax][ay] = Math.floor((minmax[ax][ay][0] + minmax[ax][ay][1]) / 2);
  2653.             //Console.out.print(middle[ax][ay] + ",");
  2654.         }
  2655.         //Console.out.println("");
  2656.     }
  2657.     //Console.out.println("");
  2658.    
  2659.     return middle;
  2660. }
  2661.  
  2662. qrcode.grayScaleToBitmap=function(grayScale)
  2663. {
  2664.     var middle = qrcode.getMiddleBrightnessPerArea(grayScale);
  2665.     var sqrtNumArea = middle.length;
  2666.     var areaWidth = Math.floor(qrcode.width / sqrtNumArea);
  2667.     var areaHeight = Math.floor(qrcode.height / sqrtNumArea);
  2668.  
  2669.     var buff = new ArrayBuffer(qrcode.width*qrcode.height);
  2670.     var bitmap = new Uint8Array(buff);
  2671.  
  2672.     //var bitmap = new Array(qrcode.height*qrcode.width);
  2673.    
  2674.     for (var ay = 0; ay < sqrtNumArea; ay++)
  2675.     {
  2676.         for (var ax = 0; ax < sqrtNumArea; ax++)
  2677.         {
  2678.             for (var dy = 0; dy < areaHeight; dy++)
  2679.             {
  2680.                 for (var dx = 0; dx < areaWidth; dx++)
  2681.                 {
  2682.                     bitmap[areaWidth * ax + dx+ (areaHeight * ay + dy)*qrcode.width] = (grayScale[areaWidth * ax + dx+ (areaHeight * ay + dy)*qrcode.width] < middle[ax][ay])?true:false;
  2683.                 }
  2684.             }
  2685.         }
  2686.     }
  2687.     return bitmap;
  2688. }
  2689.  
  2690. qrcode.grayscale = function()
  2691. {
  2692.     var buff = new ArrayBuffer(qrcode.width*qrcode.height);
  2693.     var ret = new Uint8Array(buff);
  2694.     //var ret = new Array(qrcode.width*qrcode.height);
  2695.    
  2696.     for (var y = 0; y < qrcode.height; y++)
  2697.     {
  2698.         for (var x = 0; x < qrcode.width; x++)
  2699.         {
  2700.             var gray = qrcode.getPixel(x, y);
  2701.            
  2702.             ret[x+y*qrcode.width] = gray;
  2703.         }
  2704.     }
  2705.     return ret;
  2706. }
  2707.  
  2708.  
  2709.  
  2710.  
  2711. function URShift( number,  bits)
  2712. {
  2713.     if (number >= 0)
  2714.         return number >> bits;
  2715.     else
  2716.         return (number >> bits) + (2 << ~bits);
  2717. }
  2718.  
  2719.  
  2720.  
  2721. /*
  2722.   Ported to JavaScript by Lazar Laszlo 2011
  2723.  
  2724.   lazarsoft@gmail.com, www.lazarsoft.info
  2725.  
  2726. */
  2727.  
  2728. /*
  2729. *
  2730. * Copyright 2007 ZXing authors
  2731. *
  2732. * Licensed under the Apache License, Version 2.0 (the "License");
  2733. * you may not use this file except in compliance with the License.
  2734. * You may obtain a copy of the License at
  2735. *
  2736. *      http://www.apache.org/licenses/LICENSE-2.0
  2737. *
  2738. * Unless required by applicable law or agreed to in writing, software
  2739. * distributed under the License is distributed on an "AS IS" BASIS,
  2740. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2741. * See the License for the specific language governing permissions and
  2742. * limitations under the License.
  2743. */
  2744.  
  2745.  
  2746. var MIN_SKIP = 3;
  2747. var MAX_MODULES = 57;
  2748. var INTEGER_MATH_SHIFT = 8;
  2749. var CENTER_QUORUM = 2;
  2750.  
  2751. qrcode.orderBestPatterns=function(patterns)
  2752.         {
  2753.            
  2754.             function distance( pattern1,  pattern2)
  2755.             {
  2756.                 var xDiff = pattern1.X - pattern2.X;
  2757.                 var yDiff = pattern1.Y - pattern2.Y;
  2758.                 return  Math.sqrt( (xDiff * xDiff + yDiff * yDiff));
  2759.             }
  2760.            
  2761.             /// <summary> Returns the z component of the cross product between vectors BC and BA.</summary>
  2762.             function crossProductZ( pointA,  pointB,  pointC)
  2763.             {
  2764.                 var bX = pointB.x;
  2765.                 var bY = pointB.y;
  2766.                 return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));
  2767.             }
  2768.  
  2769.            
  2770.             // Find distances between pattern centers
  2771.             var zeroOneDistance = distance(patterns[0], patterns[1]);
  2772.             var oneTwoDistance = distance(patterns[1], patterns[2]);
  2773.             var zeroTwoDistance = distance(patterns[0], patterns[2]);
  2774.            
  2775.             var pointA, pointB, pointC;
  2776.             // Assume one closest to other two is B; A and C will just be guesses at first
  2777.             if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance)
  2778.             {
  2779.                 pointB = patterns[0];
  2780.                 pointA = patterns[1];
  2781.                 pointC = patterns[2];
  2782.             }
  2783.             else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance)
  2784.             {
  2785.                 pointB = patterns[1];
  2786.                 pointA = patterns[0];
  2787.                 pointC = patterns[2];
  2788.             }
  2789.             else
  2790.             {
  2791.                 pointB = patterns[2];
  2792.                 pointA = patterns[0];
  2793.                 pointC = patterns[1];
  2794.             }
  2795.            
  2796.             // Use cross product to figure out whether A and C are correct or flipped.
  2797.             // This asks whether BC x BA has a positive z component, which is the arrangement
  2798.             // we want for A, B, C. If it's negative, then we've got it flipped around and
  2799.             // should swap A and C.
  2800.             if (crossProductZ(pointA, pointB, pointC) < 0.0)
  2801.             {
  2802.                 var temp = pointA;
  2803.                 pointA = pointC;
  2804.                 pointC = temp;
  2805.             }
  2806.            
  2807.             patterns[0] = pointA;
  2808.             patterns[1] = pointB;
  2809.             patterns[2] = pointC;
  2810.         }
  2811.  
  2812.  
  2813. function FinderPattern(posX, posY,  estimatedModuleSize)
  2814. {
  2815.     this.x=posX;
  2816.     this.y=posY;
  2817.     this.count = 1;
  2818.     this.estimatedModuleSize = estimatedModuleSize;
  2819.    
  2820.     this.__defineGetter__("EstimatedModuleSize", function()
  2821.     {
  2822.         return this.estimatedModuleSize;
  2823.     });
  2824.     this.__defineGetter__("Count", function()
  2825.     {
  2826.         return this.count;
  2827.     });
  2828.     this.__defineGetter__("X", function()
  2829.     {
  2830.         return this.x;
  2831.     });
  2832.     this.__defineGetter__("Y", function()
  2833.     {
  2834.         return this.y;
  2835.     });
  2836.     this.incrementCount = function()
  2837.     {
  2838.         this.count++;
  2839.     }
  2840.     this.aboutEquals=function( moduleSize,  i,  j)
  2841.         {
  2842.             if (Math.abs(i - this.y) <= moduleSize && Math.abs(j - this.x) <= moduleSize)
  2843.             {
  2844.                 var moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize);
  2845.                 return moduleSizeDiff <= 1.0 || moduleSizeDiff / this.estimatedModuleSize <= 1.0;
  2846.             }
  2847.             return false;
  2848.         }
  2849.    
  2850. }
  2851.  
  2852. function FinderPatternInfo(patternCenters)
  2853. {
  2854.     this.bottomLeft = patternCenters[0];
  2855.     this.topLeft = patternCenters[1];
  2856.     this.topRight = patternCenters[2];
  2857.     this.__defineGetter__("BottomLeft", function()
  2858.     {
  2859.         return this.bottomLeft;
  2860.     });
  2861.     this.__defineGetter__("TopLeft", function()
  2862.     {
  2863.         return this.topLeft;
  2864.     });
  2865.     this.__defineGetter__("TopRight", function()
  2866.     {
  2867.         return this.topRight;
  2868.     });
  2869. }
  2870.  
  2871. function FinderPatternFinder()
  2872. {
  2873.     this.image=null;
  2874.     this.possibleCenters = [];
  2875.     this.hasSkipped = false;
  2876.     this.crossCheckStateCount = new Array(0,0,0,0,0);
  2877.     this.resultPointCallback = null;
  2878.    
  2879.     this.__defineGetter__("CrossCheckStateCount", function()
  2880.     {
  2881.         this.crossCheckStateCount[0] = 0;
  2882.         this.crossCheckStateCount[1] = 0;
  2883.         this.crossCheckStateCount[2] = 0;
  2884.         this.crossCheckStateCount[3] = 0;
  2885.         this.crossCheckStateCount[4] = 0;
  2886.         return this.crossCheckStateCount;
  2887.     });
  2888.    
  2889.     this.foundPatternCross=function( stateCount)
  2890.         {
  2891.             var totalModuleSize = 0;
  2892.             for (var i = 0; i < 5; i++)
  2893.             {
  2894.                 var count = stateCount[i];
  2895.                 if (count == 0)
  2896.                 {
  2897.                     return false;
  2898.                 }
  2899.                 totalModuleSize += count;
  2900.             }
  2901.             if (totalModuleSize < 7)
  2902.             {
  2903.                 return false;
  2904.             }
  2905.             var moduleSize = Math.floor((totalModuleSize << INTEGER_MATH_SHIFT) / 7);
  2906.             var maxVariance = Math.floor(moduleSize / 2);
  2907.             // Allow less than 50% variance from 1-1-3-1-1 proportions
  2908.             return Math.abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) < maxVariance && Math.abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) < maxVariance && Math.abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) < 3 * maxVariance && Math.abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) < maxVariance && Math.abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance;
  2909.         }
  2910.     this.centerFromEnd=function( stateCount,  end)
  2911.         {
  2912.             return  (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0;
  2913.         }
  2914.     this.crossCheckVertical=function( startI,  centerJ,  maxCount,  originalStateCountTotal)
  2915.         {
  2916.             var image = this.image;
  2917.            
  2918.             var maxI = qrcode.height;
  2919.             var stateCount = this.CrossCheckStateCount;
  2920.            
  2921.             // Start counting up from center
  2922.             var i = startI;
  2923.             while (i >= 0 && image[centerJ + i*qrcode.width])
  2924.             {
  2925.                 stateCount[2]++;
  2926.                 i--;
  2927.             }
  2928.             if (i < 0)
  2929.             {
  2930.                 return NaN;
  2931.             }
  2932.             while (i >= 0 && !image[centerJ +i*qrcode.width] && stateCount[1] <= maxCount)
  2933.             {
  2934.                 stateCount[1]++;
  2935.                 i--;
  2936.             }
  2937.             // If already too many modules in this state or ran off the edge:
  2938.             if (i < 0 || stateCount[1] > maxCount)
  2939.             {
  2940.                 return NaN;
  2941.             }
  2942.             while (i >= 0 && image[centerJ + i*qrcode.width] && stateCount[0] <= maxCount)
  2943.             {
  2944.                 stateCount[0]++;
  2945.                 i--;
  2946.             }
  2947.             if (stateCount[0] > maxCount)
  2948.             {
  2949.                 return NaN;
  2950.             }
  2951.            
  2952.             // Now also count down from center
  2953.             i = startI + 1;
  2954.             while (i < maxI && image[centerJ +i*qrcode.width])
  2955.             {
  2956.                 stateCount[2]++;
  2957.                 i++;
  2958.             }
  2959.             if (i == maxI)
  2960.             {
  2961.                 return NaN;
  2962.             }
  2963.             while (i < maxI && !image[centerJ + i*qrcode.width] && stateCount[3] < maxCount)
  2964.             {
  2965.                 stateCount[3]++;
  2966.                 i++;
  2967.             }
  2968.             if (i == maxI || stateCount[3] >= maxCount)
  2969.             {
  2970.                 return NaN;
  2971.             }
  2972.             while (i < maxI && image[centerJ + i*qrcode.width] && stateCount[4] < maxCount)
  2973.             {
  2974.                 stateCount[4]++;
  2975.                 i++;
  2976.             }
  2977.             if (stateCount[4] >= maxCount)
  2978.             {
  2979.                 return NaN;
  2980.             }
  2981.            
  2982.             // If we found a finder-pattern-like section, but its size is more than 40% different than
  2983.             // the original, assume it's a false positive
  2984.             var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
  2985.             if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal)
  2986.             {
  2987.                 return NaN;
  2988.             }
  2989.            
  2990.             return this.foundPatternCross(stateCount)?this.centerFromEnd(stateCount, i):NaN;
  2991.         }
  2992.     this.crossCheckHorizontal=function( startJ,  centerI,  maxCount, originalStateCountTotal)
  2993.         {
  2994.             var image = this.image;
  2995.            
  2996.             var maxJ = qrcode.width;
  2997.             var stateCount = this.CrossCheckStateCount;
  2998.            
  2999.             var j = startJ;
  3000.             while (j >= 0 && image[j+ centerI*qrcode.width])
  3001.             {
  3002.                 stateCount[2]++;
  3003.                 j--;
  3004.             }
  3005.             if (j < 0)
  3006.             {
  3007.                 return NaN;
  3008.             }
  3009.             while (j >= 0 && !image[j+ centerI*qrcode.width] && stateCount[1] <= maxCount)
  3010.             {
  3011.                 stateCount[1]++;
  3012.                 j--;
  3013.             }
  3014.             if (j < 0 || stateCount[1] > maxCount)
  3015.             {
  3016.                 return NaN;
  3017.             }
  3018.             while (j >= 0 && image[j+ centerI*qrcode.width] && stateCount[0] <= maxCount)
  3019.             {
  3020.                 stateCount[0]++;
  3021.                 j--;
  3022.             }
  3023.             if (stateCount[0] > maxCount)
  3024.             {
  3025.                 return NaN;
  3026.             }
  3027.            
  3028.             j = startJ + 1;
  3029.             while (j < maxJ && image[j+ centerI*qrcode.width])
  3030.             {
  3031.                 stateCount[2]++;
  3032.                 j++;
  3033.             }
  3034.             if (j == maxJ)
  3035.             {
  3036.                 return NaN;
  3037.             }
  3038.             while (j < maxJ && !image[j+ centerI*qrcode.width] && stateCount[3] < maxCount)
  3039.             {
  3040.                 stateCount[3]++;
  3041.                 j++;
  3042.             }
  3043.             if (j == maxJ || stateCount[3] >= maxCount)
  3044.             {
  3045.                 return NaN;
  3046.             }
  3047.             while (j < maxJ && image[j+ centerI*qrcode.width] && stateCount[4] < maxCount)
  3048.             {
  3049.                 stateCount[4]++;
  3050.                 j++;
  3051.             }
  3052.             if (stateCount[4] >= maxCount)
  3053.             {
  3054.                 return NaN;
  3055.             }
  3056.            
  3057.             // If we found a finder-pattern-like section, but its size is significantly different than
  3058.             // the original, assume it's a false positive
  3059.             var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
  3060.             if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal)
  3061.             {
  3062.                 return NaN;
  3063.             }
  3064.            
  3065.             return this.foundPatternCross(stateCount)?this.centerFromEnd(stateCount, j):NaN;
  3066.         }
  3067.     this.handlePossibleCenter=function( stateCount,  i,  j)
  3068.         {
  3069.             var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
  3070.             var centerJ = this.centerFromEnd(stateCount, j); //float
  3071.             var centerI = this.crossCheckVertical(i, Math.floor( centerJ), stateCount[2], stateCountTotal); //float
  3072.             if (!isNaN(centerI))
  3073.             {
  3074.                 // Re-cross check
  3075.                 centerJ = this.crossCheckHorizontal(Math.floor( centerJ), Math.floor( centerI), stateCount[2], stateCountTotal);
  3076.                 if (!isNaN(centerJ))
  3077.                 {
  3078.                     var estimatedModuleSize =   stateCountTotal / 7.0;
  3079.                     var found = false;
  3080.                     var max = this.possibleCenters.length;
  3081.                     for (var index = 0; index < max; index++)
  3082.                     {
  3083.                         var center = this.possibleCenters[index];
  3084.                         // Look for about the same center and module size:
  3085.                         if (center.aboutEquals(estimatedModuleSize, centerI, centerJ))
  3086.                         {
  3087.                             center.incrementCount();
  3088.                             found = true;
  3089.                             break;
  3090.                         }
  3091.                     }
  3092.                     if (!found)
  3093.                     {
  3094.                         var point = new FinderPattern(centerJ, centerI, estimatedModuleSize);
  3095.                         this.possibleCenters.push(point);
  3096.                         if (this.resultPointCallback != null)
  3097.                         {
  3098.                             this.resultPointCallback.foundPossibleResultPoint(point);
  3099.                         }
  3100.                     }
  3101.                     return true;
  3102.                 }
  3103.             }
  3104.             return false;
  3105.         }
  3106.        
  3107.     this.selectBestPatterns=function()
  3108.         {
  3109.            
  3110.             var startSize = this.possibleCenters.length;
  3111.             if (startSize < 3)
  3112.             {
  3113.                 // Couldn't find enough finder patterns
  3114.                 throw "Couldn't find enough finder patterns (found " + startSize + ")"
  3115.             }
  3116.            
  3117.             // Filter outlier possibilities whose module size is too different
  3118.             if (startSize > 3)
  3119.             {
  3120.                 // But we can only afford to do so if we have at least 4 possibilities to choose from
  3121.                 var totalModuleSize = 0.0;
  3122.                 var square = 0.0;
  3123.                 for (var i = 0; i < startSize; i++)
  3124.                 {
  3125.                     //totalModuleSize +=  this.possibleCenters[i].EstimatedModuleSize;
  3126.                     var centerValue=this.possibleCenters[i].EstimatedModuleSize;
  3127.                     totalModuleSize += centerValue;
  3128.                     square += (centerValue * centerValue);
  3129.                 }
  3130.                 var average = totalModuleSize /  startSize;
  3131.                 this.possibleCenters.sort(function(center1,center2) {
  3132.                       var dA=Math.abs(center2.EstimatedModuleSize - average);
  3133.                       var dB=Math.abs(center1.EstimatedModuleSize - average);
  3134.                       if (dA < dB) {
  3135.                           return (-1);
  3136.                       } else if (dA == dB) {
  3137.                           return 0;
  3138.                       } else {
  3139.                           return 1;
  3140.                       }
  3141.                     });
  3142.  
  3143.                 var stdDev = Math.sqrt(square / startSize - average * average);
  3144.                 var limit = Math.max(0.2 * average, stdDev);
  3145.                 //for (var i = 0; i < this.possibleCenters.length && this.possibleCenters.length > 3; i++)
  3146.                 for (var i = this.possibleCenters.length - 1; i >= 0 ; i--)
  3147.                 {
  3148.                     var pattern =  this.possibleCenters[i];
  3149.                     //if (Math.abs(pattern.EstimatedModuleSize - average) > 0.2 * average)
  3150.                     if (Math.abs(pattern.EstimatedModuleSize - average) > limit)
  3151.                     {
  3152.                         //this.possibleCenters.remove(i);
  3153.                         this.possibleCenters.splice(i,1);
  3154.                         //i--;
  3155.                     }
  3156.                 }
  3157.             }
  3158.            
  3159.             if (this.possibleCenters.length > 3)
  3160.             {
  3161.                 // Throw away all but those first size candidate points we found.
  3162.                 this.possibleCenters.sort(function(a, b){
  3163.                     if (a.count > b.count){return -1;}
  3164.                     if (a.count < b.count){return 1;}
  3165.                     return 0;
  3166.                 });
  3167.             }
  3168.            
  3169.             return new Array( this.possibleCenters[0],  this.possibleCenters[1],  this.possibleCenters[2]);
  3170.         }
  3171.        
  3172.     this.findRowSkip=function()
  3173.         {
  3174.             var max = this.possibleCenters.length;
  3175.             if (max <= 1)
  3176.             {
  3177.                 return 0;
  3178.             }
  3179.             var firstConfirmedCenter = null;
  3180.             for (var i = 0; i < max; i++)
  3181.             {
  3182.                 var center =  this.possibleCenters[i];
  3183.                 if (center.Count >= CENTER_QUORUM)
  3184.                 {
  3185.                     if (firstConfirmedCenter == null)
  3186.                     {
  3187.                         firstConfirmedCenter = center;
  3188.                     }
  3189.                     else
  3190.                     {
  3191.                         // We have two confirmed centers
  3192.                         // How far down can we skip before resuming looking for the next
  3193.                         // pattern? In the worst case, only the difference between the
  3194.                         // difference in the x / y coordinates of the two centers.
  3195.                         // This is the case where you find top left last.
  3196.                         this.hasSkipped = true;
  3197.                         return Math.floor ((Math.abs(firstConfirmedCenter.X - center.X) - Math.abs(firstConfirmedCenter.Y - center.Y)) / 2);
  3198.                     }
  3199.                 }
  3200.             }
  3201.             return 0;
  3202.         }
  3203.    
  3204.     this.haveMultiplyConfirmedCenters=function()
  3205.         {
  3206.             var confirmedCount = 0;
  3207.             var totalModuleSize = 0.0;
  3208.             var max = this.possibleCenters.length;
  3209.             for (var i = 0; i < max; i++)
  3210.             {
  3211.                 var pattern =  this.possibleCenters[i];
  3212.                 if (pattern.Count >= CENTER_QUORUM)
  3213.                 {
  3214.                     confirmedCount++;
  3215.                     totalModuleSize += pattern.EstimatedModuleSize;
  3216.                 }
  3217.             }
  3218.             if (confirmedCount < 3)
  3219.             {
  3220.                 return false;
  3221.             }
  3222.             // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"
  3223.             // and that we need to keep looking. We detect this by asking if the estimated module sizes
  3224.             // vary too much. We arbitrarily say that when the total deviation from average exceeds
  3225.             // 5% of the total module size estimates, it's too much.
  3226.             var average = totalModuleSize / max;
  3227.             var totalDeviation = 0.0;
  3228.             for (var i = 0; i < max; i++)
  3229.             {
  3230.                 pattern = this.possibleCenters[i];
  3231.                 totalDeviation += Math.abs(pattern.EstimatedModuleSize - average);
  3232.             }
  3233.             return totalDeviation <= 0.05 * totalModuleSize;
  3234.         }
  3235.        
  3236.     this.findFinderPattern = function(image){
  3237.         var tryHarder = false;
  3238.         this.image=image;
  3239.         var maxI = qrcode.height;
  3240.         var maxJ = qrcode.width;
  3241.         var iSkip = Math.floor((3 * maxI) / (4 * MAX_MODULES));
  3242.         if (iSkip < MIN_SKIP || tryHarder)
  3243.         {
  3244.                 iSkip = MIN_SKIP;
  3245.         }
  3246.        
  3247.         var done = false;
  3248.         var stateCount = new Array(5);
  3249.         for (var i = iSkip - 1; i < maxI && !done; i += iSkip)
  3250.         {
  3251.             // Get a row of black/white values
  3252.             stateCount[0] = 0;
  3253.             stateCount[1] = 0;
  3254.             stateCount[2] = 0;
  3255.             stateCount[3] = 0;
  3256.             stateCount[4] = 0;
  3257.             var currentState = 0;
  3258.             for (var j = 0; j < maxJ; j++)
  3259.             {
  3260.                 if (image[j+i*qrcode.width] )
  3261.                 {
  3262.                     // Black pixel
  3263.                     if ((currentState & 1) == 1)
  3264.                     {
  3265.                         // Counting white pixels
  3266.                         currentState++;
  3267.                     }
  3268.                     stateCount[currentState]++;
  3269.                 }
  3270.                 else
  3271.                 {
  3272.                     // White pixel
  3273.                     if ((currentState & 1) == 0)
  3274.                     {
  3275.                         // Counting black pixels
  3276.                         if (currentState == 4)
  3277.                         {
  3278.                             // A winner?
  3279.                             if (this.foundPatternCross(stateCount))
  3280.                             {
  3281.                                 // Yes
  3282.                                 var confirmed = this.handlePossibleCenter(stateCount, i, j);
  3283.                                 if (confirmed)
  3284.                                 {
  3285.                                     // Start examining every other line. Checking each line turned out to be too
  3286.                                     // expensive and didn't improve performance.
  3287.                                     iSkip = 2;
  3288.                                     if (this.hasSkipped)
  3289.                                     {
  3290.                                         done = this.haveMultiplyConfirmedCenters();
  3291.                                     }
  3292.                                     else
  3293.                                     {
  3294.                                         var rowSkip = this.findRowSkip();
  3295.                                         if (rowSkip > stateCount[2])
  3296.                                         {
  3297.                                             // Skip rows between row of lower confirmed center
  3298.                                             // and top of presumed third confirmed center
  3299.                                             // but back up a bit to get a full chance of detecting
  3300.                                             // it, entire width of center of finder pattern
  3301.                                            
  3302.                                             // Skip by rowSkip, but back off by stateCount[2] (size of last center
  3303.                                             // of pattern we saw) to be conservative, and also back off by iSkip which
  3304.                                             // is about to be re-added
  3305.                                             i += rowSkip - stateCount[2] - iSkip;
  3306.                                             j = maxJ - 1;
  3307.                                         }
  3308.                                     }
  3309.                                 }
  3310.                                 else
  3311.                                 {
  3312.                                     // Advance to next black pixel
  3313.                                     do
  3314.                                     {
  3315.                                         j++;
  3316.                                     }
  3317.                                     while (j < maxJ && !image[j + i*qrcode.width]);
  3318.                                     j--; // back up to that last white pixel
  3319.                                 }
  3320.                                 // Clear state to start looking again
  3321.                                 currentState = 0;
  3322.                                 stateCount[0] = 0;
  3323.                                 stateCount[1] = 0;
  3324.                                 stateCount[2] = 0;
  3325.                                 stateCount[3] = 0;
  3326.                                 stateCount[4] = 0;
  3327.                             }
  3328.                             else
  3329.                             {
  3330.                                 // No, shift counts back by two
  3331.                                 stateCount[0] = stateCount[2];
  3332.                                 stateCount[1] = stateCount[3];
  3333.                                 stateCount[2] = stateCount[4];
  3334.                                 stateCount[3] = 1;
  3335.                                 stateCount[4] = 0;
  3336.                                 currentState = 3;
  3337.                             }
  3338.                         }
  3339.                         else
  3340.                         {
  3341.                             stateCount[++currentState]++;
  3342.                         }
  3343.                     }
  3344.                     else
  3345.                     {
  3346.                         // Counting white pixels
  3347.                         stateCount[currentState]++;
  3348.                     }
  3349.                 }
  3350.             }
  3351.             if (this.foundPatternCross(stateCount))
  3352.             {
  3353.                 var confirmed = this.handlePossibleCenter(stateCount, i, maxJ);
  3354.                 if (confirmed)
  3355.                 {
  3356.                     iSkip = stateCount[0];
  3357.                     if (this.hasSkipped)
  3358.                     {
  3359.                         // Found a third one
  3360.                         done = this.haveMultiplyConfirmedCenters();
  3361.                     }
  3362.                 }
  3363.             }
  3364.         }
  3365.        
  3366.         var patternInfo = this.selectBestPatterns();
  3367.         qrcode.orderBestPatterns(patternInfo);
  3368.        
  3369.         return new FinderPatternInfo(patternInfo);
  3370.     };
  3371. }
  3372.  
  3373.  
  3374. /*
  3375.   Ported to JavaScript by Lazar Laszlo 2011
  3376.  
  3377.   lazarsoft@gmail.com, www.lazarsoft.info
  3378.  
  3379. */
  3380.  
  3381. /*
  3382. *
  3383. * Copyright 2007 ZXing authors
  3384. *
  3385. * Licensed under the Apache License, Version 2.0 (the "License");
  3386. * you may not use this file except in compliance with the License.
  3387. * You may obtain a copy of the License at
  3388. *
  3389. *      http://www.apache.org/licenses/LICENSE-2.0
  3390. *
  3391. * Unless required by applicable law or agreed to in writing, software
  3392. * distributed under the License is distributed on an "AS IS" BASIS,
  3393. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3394. * See the License for the specific language governing permissions and
  3395. * limitations under the License.
  3396. */
  3397.  
  3398.  
  3399. function AlignmentPattern(posX, posY,  estimatedModuleSize)
  3400. {
  3401.     this.x=posX;
  3402.     this.y=posY;
  3403.     this.count = 1;
  3404.     this.estimatedModuleSize = estimatedModuleSize;
  3405.    
  3406.     this.__defineGetter__("EstimatedModuleSize", function()
  3407.     {
  3408.         return this.estimatedModuleSize;
  3409.     });
  3410.     this.__defineGetter__("Count", function()
  3411.     {
  3412.         return this.count;
  3413.     });
  3414.     this.__defineGetter__("X", function()
  3415.     {
  3416.         return Math.floor(this.x);
  3417.     });
  3418.     this.__defineGetter__("Y", function()
  3419.     {
  3420.         return Math.floor(this.y);
  3421.     });
  3422.     this.incrementCount = function()
  3423.     {
  3424.         this.count++;
  3425.     }
  3426.     this.aboutEquals=function( moduleSize,  i,  j)
  3427.         {
  3428.             if (Math.abs(i - this.y) <= moduleSize && Math.abs(j - this.x) <= moduleSize)
  3429.             {
  3430.                 var moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize);
  3431.                 return moduleSizeDiff <= 1.0 || moduleSizeDiff / this.estimatedModuleSize <= 1.0;
  3432.             }
  3433.             return false;
  3434.         }
  3435.    
  3436. }
  3437.  
  3438. function AlignmentPatternFinder( image,  startX,  startY,  width,  height,  moduleSize,  resultPointCallback)
  3439. {
  3440.     this.image = image;
  3441.     this.possibleCenters = new Array();
  3442.     this.startX = startX;
  3443.     this.startY = startY;
  3444.     this.width = width;
  3445.     this.height = height;
  3446.     this.moduleSize = moduleSize;
  3447.     this.crossCheckStateCount = new Array(0,0,0);
  3448.     this.resultPointCallback = resultPointCallback;
  3449.    
  3450.     this.centerFromEnd=function(stateCount,  end)
  3451.         {
  3452.             return  (end - stateCount[2]) - stateCount[1] / 2.0;
  3453.         }
  3454.     this.foundPatternCross = function(stateCount)
  3455.         {
  3456.             var moduleSize = this.moduleSize;
  3457.             var maxVariance = moduleSize / 2.0;
  3458.             for (var i = 0; i < 3; i++)
  3459.             {
  3460.                 if (Math.abs(moduleSize - stateCount[i]) >= maxVariance)
  3461.                 {
  3462.                     return false;
  3463.                 }
  3464.             }
  3465.             return true;
  3466.         }
  3467.  
  3468.     this.crossCheckVertical=function( startI,  centerJ,  maxCount,  originalStateCountTotal)
  3469.         {
  3470.             var image = this.image;
  3471.            
  3472.             var maxI = qrcode.height;
  3473.             var stateCount = this.crossCheckStateCount;
  3474.             stateCount[0] = 0;
  3475.             stateCount[1] = 0;
  3476.             stateCount[2] = 0;
  3477.            
  3478.             // Start counting up from center
  3479.             var i = startI;
  3480.             while (i >= 0 && image[centerJ + i*qrcode.width] && stateCount[1] <= maxCount)
  3481.             {
  3482.                 stateCount[1]++;
  3483.                 i--;
  3484.             }
  3485.             // If already too many modules in this state or ran off the edge:
  3486.             if (i < 0 || stateCount[1] > maxCount)
  3487.             {
  3488.                 return NaN;
  3489.             }
  3490.             while (i >= 0 && !image[centerJ + i*qrcode.width] && stateCount[0] <= maxCount)
  3491.             {
  3492.                 stateCount[0]++;
  3493.                 i--;
  3494.             }
  3495.             if (stateCount[0] > maxCount)
  3496.             {
  3497.                 return NaN;
  3498.             }
  3499.            
  3500.             // Now also count down from center
  3501.             i = startI + 1;
  3502.             while (i < maxI && image[centerJ + i*qrcode.width] && stateCount[1] <= maxCount)
  3503.             {
  3504.                 stateCount[1]++;
  3505.                 i++;
  3506.             }
  3507.             if (i == maxI || stateCount[1] > maxCount)
  3508.             {
  3509.                 return NaN;
  3510.             }
  3511.             while (i < maxI && !image[centerJ + i*qrcode.width] && stateCount[2] <= maxCount)
  3512.             {
  3513.                 stateCount[2]++;
  3514.                 i++;
  3515.             }
  3516.             if (stateCount[2] > maxCount)
  3517.             {
  3518.                 return NaN;
  3519.             }
  3520.            
  3521.             var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  3522.             if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal)
  3523.             {
  3524.                 return NaN;
  3525.             }
  3526.            
  3527.             return this.foundPatternCross(stateCount)?this.centerFromEnd(stateCount, i):NaN;
  3528.         }
  3529.        
  3530.     this.handlePossibleCenter=function( stateCount,  i,  j)
  3531.         {
  3532.             var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  3533.             var centerJ = this.centerFromEnd(stateCount, j);
  3534.             var centerI = this.crossCheckVertical(i, Math.floor (centerJ), 2 * stateCount[1], stateCountTotal);
  3535.             if (!isNaN(centerI))
  3536.             {
  3537.                 var estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0;
  3538.                 var max = this.possibleCenters.length;
  3539.                 for (var index = 0; index < max; index++)
  3540.                 {
  3541.                     var center =  this.possibleCenters[index];
  3542.                     // Look for about the same center and module size:
  3543.                     if (center.aboutEquals(estimatedModuleSize, centerI, centerJ))
  3544.                     {
  3545.                         return new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
  3546.                     }
  3547.                 }
  3548.                 // Hadn't found this before; save it
  3549.                 var point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
  3550.                 this.possibleCenters.push(point);
  3551.                 if (this.resultPointCallback != null)
  3552.                 {
  3553.                     this.resultPointCallback.foundPossibleResultPoint(point);
  3554.                 }
  3555.             }
  3556.             return null;
  3557.         }
  3558.        
  3559.     this.find = function()
  3560.     {
  3561.             var startX = this.startX;
  3562.             var height = this.height;
  3563.             var maxJ = startX + width;
  3564.             var middleI = startY + (height >> 1);
  3565.             // We are looking for black/white/black modules in 1:1:1 ratio;
  3566.             // this tracks the number of black/white/black modules seen so far
  3567.             var stateCount = new Array(0,0,0);
  3568.             for (var iGen = 0; iGen < height; iGen++)
  3569.             {
  3570.                 // Search from middle outwards
  3571.                 var i = middleI + ((iGen & 0x01) == 0?((iGen + 1) >> 1):- ((iGen + 1) >> 1));
  3572.                 stateCount[0] = 0;
  3573.                 stateCount[1] = 0;
  3574.                 stateCount[2] = 0;
  3575.                 var j = startX;
  3576.                 // Burn off leading white pixels before anything else; if we start in the middle of
  3577.                 // a white run, it doesn't make sense to count its length, since we don't know if the
  3578.                 // white run continued to the left of the start point
  3579.                 while (j < maxJ && !image[j + qrcode.width* i])
  3580.                 {
  3581.                     j++;
  3582.                 }
  3583.                 var currentState = 0;
  3584.                 while (j < maxJ)
  3585.                 {
  3586.                     if (image[j + i*qrcode.width])
  3587.                     {
  3588.                         // Black pixel
  3589.                         if (currentState == 1)
  3590.                         {
  3591.                             // Counting black pixels
  3592.                             stateCount[currentState]++;
  3593.                         }
  3594.                         else
  3595.                         {
  3596.                             // Counting white pixels
  3597.                             if (currentState == 2)
  3598.                             {
  3599.                                 // A winner?
  3600.                                 if (this.foundPatternCross(stateCount))
  3601.                                 {
  3602.                                     // Yes
  3603.                                     var confirmed = this.handlePossibleCenter(stateCount, i, j);
  3604.                                     if (confirmed != null)
  3605.                                     {
  3606.                                         return confirmed;
  3607.                                     }
  3608.                                 }
  3609.                                 stateCount[0] = stateCount[2];
  3610.                                 stateCount[1] = 1;
  3611.                                 stateCount[2] = 0;
  3612.                                 currentState = 1;
  3613.                             }
  3614.                             else
  3615.                             {
  3616.                                 stateCount[++currentState]++;
  3617.                             }
  3618.                         }
  3619.                     }
  3620.                     else
  3621.                     {
  3622.                         // White pixel
  3623.                         if (currentState == 1)
  3624.                         {
  3625.                             // Counting black pixels
  3626.                             currentState++;
  3627.                         }
  3628.                         stateCount[currentState]++;
  3629.                     }
  3630.                     j++;
  3631.                 }
  3632.                 if (this.foundPatternCross(stateCount))
  3633.                 {
  3634.                     var confirmed = this.handlePossibleCenter(stateCount, i, maxJ);
  3635.                     if (confirmed != null)
  3636.                     {
  3637.                         return confirmed;
  3638.                     }
  3639.                 }
  3640.             }
  3641.            
  3642.             // Hmm, nothing we saw was observed and confirmed twice. If we had
  3643.             // any guess at all, return it.
  3644.             if (!(this.possibleCenters.length == 0))
  3645.             {
  3646.                 return  this.possibleCenters[0];
  3647.             }
  3648.            
  3649.             throw "Couldn't find enough alignment patterns";
  3650.         }
  3651.    
  3652. }
  3653.  
  3654. /*
  3655.   Ported to JavaScript by Lazar Laszlo 2011
  3656.  
  3657.   lazarsoft@gmail.com, www.lazarsoft.info
  3658.  
  3659. */
  3660.  
  3661. /*
  3662. *
  3663. * Copyright 2007 ZXing authors
  3664. *
  3665. * Licensed under the Apache License, Version 2.0 (the "License");
  3666. * you may not use this file except in compliance with the License.
  3667. * You may obtain a copy of the License at
  3668. *
  3669. *      http://www.apache.org/licenses/LICENSE-2.0
  3670. *
  3671. * Unless required by applicable law or agreed to in writing, software
  3672. * distributed under the License is distributed on an "AS IS" BASIS,
  3673. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3674. * See the License for the specific language governing permissions and
  3675. * limitations under the License.
  3676. */
  3677.  
  3678.  
  3679. function QRCodeDataBlockReader(blocks,  version,  numErrorCorrectionCode)
  3680. {
  3681.     this.blockPointer = 0;
  3682.     this.bitPointer = 7;
  3683.     this.dataLength = 0;
  3684.     this.blocks = blocks;
  3685.     this.numErrorCorrectionCode = numErrorCorrectionCode;
  3686.     if (version <= 9)
  3687.         this.dataLengthMode = 0;
  3688.     else if (version >= 10 && version <= 26)
  3689.         this.dataLengthMode = 1;
  3690.     else if (version >= 27 && version <= 40)
  3691.         this.dataLengthMode = 2;
  3692.        
  3693.     this.getNextBits = function( numBits)
  3694.         {          
  3695.             var bits = 0;
  3696.             if (numBits < this.bitPointer + 1)
  3697.             {
  3698.                 // next word fits into current data block
  3699.                 var mask = 0;
  3700.                 for (var i = 0; i < numBits; i++)
  3701.                 {
  3702.                     mask += (1 << i);
  3703.                 }
  3704.                 mask <<= (this.bitPointer - numBits + 1);
  3705.                
  3706.                 bits = (this.blocks[this.blockPointer] & mask) >> (this.bitPointer - numBits + 1);
  3707.                 this.bitPointer -= numBits;
  3708.                 return bits;
  3709.             }
  3710.             else if (numBits < this.bitPointer + 1 + 8)
  3711.             {
  3712.                 // next word crosses 2 data blocks
  3713.                 var mask1 = 0;
  3714.                 for (var i = 0; i < this.bitPointer + 1; i++)
  3715.                 {
  3716.                     mask1 += (1 << i);
  3717.                 }
  3718.                 bits = (this.blocks[this.blockPointer] & mask1) << (numBits - (this.bitPointer + 1));
  3719.                 this.blockPointer++;
  3720.                 bits += ((this.blocks[this.blockPointer]) >> (8 - (numBits - (this.bitPointer + 1))));
  3721.                
  3722.                 this.bitPointer = this.bitPointer - numBits % 8;
  3723.                 if (this.bitPointer < 0)
  3724.                 {
  3725.                     this.bitPointer = 8 + this.bitPointer;
  3726.                 }
  3727.                 return bits;
  3728.             }
  3729.             else if (numBits < this.bitPointer + 1 + 16)
  3730.             {
  3731.                 // next word crosses 3 data blocks
  3732.                 var mask1 = 0; // mask of first block
  3733.                 var mask3 = 0; // mask of 3rd block
  3734.                 //bitPointer + 1 : number of bits of the 1st block
  3735.                 //8 : number of the 2nd block (note that use already 8bits because next word uses 3 data blocks)
  3736.                 //numBits - (bitPointer + 1 + 8) : number of bits of the 3rd block
  3737.                 for (var i = 0; i < this.bitPointer + 1; i++)
  3738.                 {
  3739.                     mask1 += (1 << i);
  3740.                 }
  3741.                 var bitsFirstBlock = (this.blocks[this.blockPointer] & mask1) << (numBits - (this.bitPointer + 1));
  3742.                 this.blockPointer++;
  3743.                
  3744.                 var bitsSecondBlock = this.blocks[this.blockPointer] << (numBits - (this.bitPointer + 1 + 8));
  3745.                 this.blockPointer++;
  3746.                
  3747.                 for (var i = 0; i < numBits - (this.bitPointer + 1 + 8); i++)
  3748.                 {
  3749.                     mask3 += (1 << i);
  3750.                 }
  3751.                 mask3 <<= 8 - (numBits - (this.bitPointer + 1 + 8));
  3752.                 var bitsThirdBlock = (this.blocks[this.blockPointer] & mask3) >> (8 - (numBits - (this.bitPointer + 1 + 8)));
  3753.                
  3754.                 bits = bitsFirstBlock + bitsSecondBlock + bitsThirdBlock;
  3755.                 this.bitPointer = this.bitPointer - (numBits - 8) % 8;
  3756.                 if (this.bitPointer < 0)
  3757.                 {
  3758.                     this.bitPointer = 8 + this.bitPointer;
  3759.                 }
  3760.                 return bits;
  3761.             }
  3762.             else
  3763.             {
  3764.                 return 0;
  3765.             }
  3766.         }
  3767.     this.NextMode=function()
  3768.     {
  3769.         if ((this.blockPointer > this.blocks.length - this.numErrorCorrectionCode - 2))
  3770.             return 0;
  3771.         else
  3772.             return this.getNextBits(4);
  3773.     }
  3774.     this.getDataLength=function( modeIndicator)
  3775.         {
  3776.             var index = 0;
  3777.             while (true)
  3778.             {
  3779.                 if ((modeIndicator >> index) == 1)
  3780.                     break;
  3781.                 index++;
  3782.             }
  3783.            
  3784.             return this.getNextBits(qrcode.sizeOfDataLengthInfo[this.dataLengthMode][index]);
  3785.         }
  3786.     this.getRomanAndFigureString=function( dataLength)
  3787.         {
  3788.             var length = dataLength;
  3789.             var intData = 0;
  3790.             var strData = "";
  3791.             var tableRomanAndFigure = 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', ' ', '$', '%', '*', '+', '-', '.', '/', ':');
  3792.             do
  3793.             {
  3794.                 if (length > 1)
  3795.                 {
  3796.                     intData = this.getNextBits(11);
  3797.                     var firstLetter = Math.floor(intData / 45);
  3798.                     var secondLetter = intData % 45;
  3799.                     strData += tableRomanAndFigure[firstLetter];
  3800.                     strData += tableRomanAndFigure[secondLetter];
  3801.                     length -= 2;
  3802.                 }
  3803.                 else if (length == 1)
  3804.                 {
  3805.                     intData = this.getNextBits(6);
  3806.                     strData += tableRomanAndFigure[intData];
  3807.                     length -= 1;
  3808.                 }
  3809.             }
  3810.             while (length > 0);
  3811.            
  3812.             return strData;
  3813.         }
  3814.     this.getFigureString=function( dataLength)
  3815.         {
  3816.             var length = dataLength;
  3817.             var intData = 0;
  3818.             var strData = "";
  3819.             do
  3820.             {
  3821.                 if (length >= 3)
  3822.                 {
  3823.                     intData = this.getNextBits(10);
  3824.                     if (intData < 100)
  3825.                         strData += "0";
  3826.                     if (intData < 10)
  3827.                         strData += "0";
  3828.                     length -= 3;
  3829.                 }
  3830.                 else if (length == 2)
  3831.                 {
  3832.                     intData = this.getNextBits(7);
  3833.                     if (intData < 10)
  3834.                         strData += "0";
  3835.                     length -= 2;
  3836.                 }
  3837.                 else if (length == 1)
  3838.                 {
  3839.                     intData = this.getNextBits(4);
  3840.                     length -= 1;
  3841.                 }
  3842.                 strData += intData;
  3843.             }
  3844.             while (length > 0);
  3845.            
  3846.             return strData;
  3847.         }
  3848.     this.get8bitByteArray=function( dataLength)
  3849.         {
  3850.             var length = dataLength;
  3851.             var intData = 0;
  3852.             var output = new Array();
  3853.            
  3854.             do
  3855.             {
  3856.                 intData = this.getNextBits(8);
  3857.                 output.push( intData);
  3858.                 length--;
  3859.             }
  3860.             while (length > 0);
  3861.             return output;
  3862.         }
  3863.     this.getKanjiString=function( dataLength)
  3864.         {
  3865.             var length = dataLength;
  3866.             var intData = 0;
  3867.             var unicodeString = "";
  3868.             do
  3869.             {
  3870.                 intData = this.getNextBits(13);
  3871.                 var lowerByte = intData % 0xC0;
  3872.                 var higherByte = intData / 0xC0;
  3873.                
  3874.                 var tempWord = (higherByte << 8) + lowerByte;
  3875.                 var shiftjisWord = 0;
  3876.                 if (tempWord + 0x8140 <= 0x9FFC)
  3877.                 {
  3878.                     // between 8140 - 9FFC on Shift_JIS character set
  3879.                     shiftjisWord = tempWord + 0x8140;
  3880.                 }
  3881.                 else
  3882.                 {
  3883.                     // between E040 - EBBF on Shift_JIS character set
  3884.                     shiftjisWord = tempWord + 0xC140;
  3885.                 }
  3886.                
  3887.                 //var tempByte = new Array(0,0);
  3888.                 //tempByte[0] = (sbyte) (shiftjisWord >> 8);
  3889.                 //tempByte[1] = (sbyte) (shiftjisWord & 0xFF);
  3890.                 //unicodeString += new String(SystemUtils.ToCharArray(SystemUtils.ToByteArray(tempByte)));
  3891.                 unicodeString += String.fromCharCode(shiftjisWord);
  3892.                 length--;
  3893.             }
  3894.             while (length > 0);
  3895.            
  3896.            
  3897.             return unicodeString;
  3898.         }
  3899.  
  3900.     this.parseECIValue = function ()
  3901.     {
  3902.         var intData = 0;
  3903.         var firstByte = this.getNextBits(8);
  3904.         if ((firstByte & 0x80) == 0) {
  3905.             intData = firstByte & 0x7F;
  3906.         }
  3907.         if ((firstByte & 0xC0) == 0x80) {
  3908.             // two bytes
  3909.             var secondByte = this.getNextBits(8);
  3910.             intData = ((firstByte & 0x3F) << 8) | secondByte;
  3911.         }
  3912.         if ((firstByte & 0xE0) == 0xC0) {
  3913.             // three bytes
  3914.             var secondThirdBytes = this.getNextBits(8);;
  3915.             intData = ((firstByte & 0x1F) << 16) | secondThirdBytes;
  3916.         }
  3917.         return intData;
  3918.     }
  3919.  
  3920.     this.__defineGetter__("DataByte", function()
  3921.     {
  3922.         var output = new Array();
  3923.         var MODE_NUMBER = 1;
  3924.         var MODE_ROMAN_AND_NUMBER = 2;
  3925.         var MODE_8BIT_BYTE = 4;
  3926.         var MODE_ECI = 7;
  3927.         var MODE_KANJI = 8;
  3928.         do
  3929.                     {
  3930.                         var mode = this.NextMode();
  3931.                         //canvas.println("mode: " + mode);
  3932.                         if (mode == 0)
  3933.                         {
  3934.                             if (output.length > 0)
  3935.                                 break;
  3936.                             else
  3937.                                 throw "Empty data block";
  3938.                         }
  3939.                         if (mode != MODE_NUMBER && mode != MODE_ROMAN_AND_NUMBER && mode != MODE_8BIT_BYTE && mode != MODE_KANJI && mode != MODE_ECI)
  3940.                         {
  3941.                             throw "Invalid mode: " + mode + " in (block:" + this.blockPointer + " bit:" + this.bitPointer + ")";
  3942.                         }
  3943.  
  3944.                         if(mode == MODE_ECI)
  3945.                         {
  3946.                             var temp_sbyteArray3 = this.parseECIValue();
  3947.                             //output.push(temp_sbyteArray3);
  3948.                         }
  3949.                         else
  3950.                         {
  3951.  
  3952.                             var dataLength = this.getDataLength(mode);
  3953.                             if (dataLength < 1)
  3954.                                 throw "Invalid data length: " + dataLength;
  3955.                             switch (mode)
  3956.                             {
  3957.                                
  3958.                                 case MODE_NUMBER:
  3959.                                     var temp_str = this.getFigureString(dataLength);
  3960.                                     var ta = new Array(temp_str.length);
  3961.                                     for(var j=0;j<temp_str.length;j++)
  3962.                                         ta[j]=temp_str.charCodeAt(j);
  3963.                                     output.push(ta);
  3964.                                     break;
  3965.                                
  3966.                                 case MODE_ROMAN_AND_NUMBER:
  3967.                                     var temp_str = this.getRomanAndFigureString(dataLength);
  3968.                                     var ta = new Array(temp_str.length);
  3969.                                     for(var j=0;j<temp_str.length;j++)
  3970.                                         ta[j]=temp_str.charCodeAt(j);
  3971.                                     output.push(ta );
  3972.                                     break;
  3973.                                
  3974.                                 case MODE_8BIT_BYTE:
  3975.                                     var temp_sbyteArray3 = this.get8bitByteArray(dataLength);
  3976.                                     output.push(temp_sbyteArray3);
  3977.                                     break;
  3978.                                
  3979.                                 case MODE_KANJI:
  3980.                                     var temp_str = this.getKanjiString(dataLength);
  3981.                                     output.push(temp_str);
  3982.                                     break;
  3983.                             }
  3984.                         }
  3985.                     }
  3986.                     while (true);
  3987.         return output;
  3988.     });
  3989. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement