not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.5 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. var GridSampler = {};
  22. GridSampler.checkAndNudgePoints=function( image, points)
  23. {
  24. var width = qrcode.width;
  25. var height = qrcode.height;
  26. // Check and nudge points from start until we see some that are OK:
  27. var nudged = true;
  28. for (var offset = 0; offset < points.length && nudged; offset += 2)
  29. {
  30. var x = Math.floor (points[offset]);
  31. var y = Math.floor( points[offset + 1]);
  32. if (x < - 1 || x > width || y < - 1 || y > height)
  33. {
  34. throw "Error.checkAndNudgePoints ";
  35. }
  36. nudged = false;
  37. if (x == - 1)
  38. {
  39. points[offset] = 0.0;
  40. nudged = true;
  41. }
  42. else if (x == width)
  43. {
  44. points[offset] = width - 1;
  45. nudged = true;
  46. }
  47. if (y == - 1)
  48. {
  49. points[offset + 1] = 0.0;
  50. nudged = true;
  51. }
  52. else if (y == height)
  53. {
  54. points[offset + 1] = height - 1;
  55. nudged = true;
  56. }
  57. }
  58. // Check and nudge points from end:
  59. nudged = true;
  60. for (var offset = points.length - 2; offset >= 0 && nudged; offset -= 2)
  61. {
  62. var x = Math.floor( points[offset]);
  63. var y = Math.floor( points[offset + 1]);
  64. if (x < - 1 || x > width || y < - 1 || y > height)
  65. {
  66. throw "Error.checkAndNudgePoints ";
  67. }
  68. nudged = false;
  69. if (x == - 1)
  70. {
  71. points[offset] = 0.0;
  72. nudged = true;
  73. }
  74. else if (x == width)
  75. {
  76. points[offset] = width - 1;
  77. nudged = true;
  78. }
  79. if (y == - 1)
  80. {
  81. points[offset + 1] = 0.0;
  82. nudged = true;
  83. }
  84. else if (y == height)
  85. {
  86. points[offset + 1] = height - 1;
  87. nudged = true;
  88. }
  89. }
  90. }
  91. GridSampler.sampleGrid3=function( image, dimension, transform)
  92. {
  93. var bits = new BitMatrix(dimension);
  94. var points = new Array(dimension << 1);
  95. for (var y = 0; y < dimension; y++)
  96. {
  97. var max = points.length;
  98. var iValue = y + 0.5;
  99. for (var x = 0; x < max; x += 2)
  100. {
  101. points[x] = (x >> 1) + 0.5;
  102. points[x + 1] = iValue;
  103. }
  104. transform.transformPoints1(points);
  105. // Quick check to see if points transformed to something inside the image;
  106. // sufficient to check the endpoints
  107. GridSampler.checkAndNudgePoints(image, points);
  108. try
  109. {
  110. for (var x = 0; x < max; x += 2)
  111. {
  112. //var xpoint = (Math.floor( points[x]) * 4) + (Math.floor( points[x + 1]) * qrcode.width * 4);
  113. var bit = image[Math.floor( points[x])+ qrcode.width* Math.floor( points[x + 1])];
  114. //qrcode.imagedata.data[xpoint] = bit?255:0;
  115. //qrcode.imagedata.data[xpoint+1] = bit?255:0;
  116. //qrcode.imagedata.data[xpoint+2] = 0;
  117. //qrcode.imagedata.data[xpoint+3] = 255;
  118. //bits[x >> 1][ y]=bit;
  119. if(bit)
  120. bits.set_Renamed(x >> 1, y);
  121. }
  122. }
  123. catch ( aioobe)
  124. {
  125. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  126. // transform gets "twisted" such that it maps a straight line of points to a set of points
  127. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  128. // way to detect this about the transformation that I don't know yet.
  129. // This results in an ugly runtime exception despite our clever checks above -- can't have
  130. // that. We could check each point's coordinates but that feels duplicative. We settle for
  131. // catching and wrapping ArrayIndexOutOfBoundsException.
  132. throw "Error.checkAndNudgePoints";
  133. }
  134. }
  135. return bits;
  136. }
  137. GridSampler.sampleGridx=function( image, dimension, p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY)
  138. {
  139. var transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
  140. return GridSampler.sampleGrid3(image, dimension, transform);
  141. }