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.

412 lines
13 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. function PerspectiveTransform( a11, a21, a31, a12, a22, a32, a13, a23, a33)
  22. {
  23. this.a11 = a11;
  24. this.a12 = a12;
  25. this.a13 = a13;
  26. this.a21 = a21;
  27. this.a22 = a22;
  28. this.a23 = a23;
  29. this.a31 = a31;
  30. this.a32 = a32;
  31. this.a33 = a33;
  32. this.transformPoints1=function( points)
  33. {
  34. var max = points.length;
  35. var a11 = this.a11;
  36. var a12 = this.a12;
  37. var a13 = this.a13;
  38. var a21 = this.a21;
  39. var a22 = this.a22;
  40. var a23 = this.a23;
  41. var a31 = this.a31;
  42. var a32 = this.a32;
  43. var a33 = this.a33;
  44. for (var i = 0; i < max; i += 2)
  45. {
  46. var x = points[i];
  47. var y = points[i + 1];
  48. var denominator = a13 * x + a23 * y + a33;
  49. points[i] = (a11 * x + a21 * y + a31) / denominator;
  50. points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
  51. }
  52. }
  53. this. transformPoints2=function(xValues, yValues)
  54. {
  55. var n = xValues.length;
  56. for (var i = 0; i < n; i++)
  57. {
  58. var x = xValues[i];
  59. var y = yValues[i];
  60. var denominator = this.a13 * x + this.a23 * y + this.a33;
  61. xValues[i] = (this.a11 * x + this.a21 * y + this.a31) / denominator;
  62. yValues[i] = (this.a12 * x + this.a22 * y + this.a32) / denominator;
  63. }
  64. }
  65. this.buildAdjoint=function()
  66. {
  67. // Adjoint is the transpose of the cofactor matrix:
  68. 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);
  69. }
  70. this.times=function( other)
  71. {
  72. 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);
  73. }
  74. }
  75. PerspectiveTransform.quadrilateralToQuadrilateral=function( x0, y0, x1, y1, x2, y2, x3, y3, x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p)
  76. {
  77. var qToS = this.quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
  78. var sToQ = this.squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);
  79. return sToQ.times(qToS);
  80. }
  81. PerspectiveTransform.squareToQuadrilateral=function( x0, y0, x1, y1, x2, y2, x3, y3)
  82. {
  83. var dy2 = y3 - y2;
  84. var dy3 = y0 - y1 + y2 - y3;
  85. if (dy2 == 0.0 && dy3 == 0.0)
  86. {
  87. return new PerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, y2 - y1, y0, 0.0, 0.0, 1.0);
  88. }
  89. else
  90. {
  91. var dx1 = x1 - x2;
  92. var dx2 = x3 - x2;
  93. var dx3 = x0 - x1 + x2 - x3;
  94. var dy1 = y1 - y2;
  95. var denominator = dx1 * dy2 - dx2 * dy1;
  96. var a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
  97. var a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
  98. 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);
  99. }
  100. }
  101. PerspectiveTransform.quadrilateralToSquare=function( x0, y0, x1, y1, x2, y2, x3, y3)
  102. {
  103. // Here, the adjoint serves as the inverse:
  104. return this.squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint();
  105. }
  106. function DetectorResult(bits, points)
  107. {
  108. this.bits = bits;
  109. this.points = points;
  110. }
  111. function Detector(image)
  112. {
  113. this.image=image;
  114. this.resultPointCallback = null;
  115. this.sizeOfBlackWhiteBlackRun=function( fromX, fromY, toX, toY)
  116. {
  117. // Mild variant of Bresenham's algorithm;
  118. // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  119. var steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  120. if (steep)
  121. {
  122. var temp = fromX;
  123. fromX = fromY;
  124. fromY = temp;
  125. temp = toX;
  126. toX = toY;
  127. toY = temp;
  128. }
  129. var dx = Math.abs(toX - fromX);
  130. var dy = Math.abs(toY - fromY);
  131. var error = - dx >> 1;
  132. var ystep = fromY < toY?1:- 1;
  133. var xstep = fromX < toX?1:- 1;
  134. var state = 0; // In black pixels, looking for white, first or second time
  135. for (var x = fromX, y = fromY; x != toX; x += xstep)
  136. {
  137. var realX = steep?y:x;
  138. var realY = steep?x:y;
  139. if (state == 1)
  140. {
  141. // In white pixels, looking for black
  142. if (this.image[realX + realY*qrcode.width])
  143. {
  144. state++;
  145. }
  146. }
  147. else
  148. {
  149. if (!this.image[realX + realY*qrcode.width])
  150. {
  151. state++;
  152. }
  153. }
  154. if (state == 3)
  155. {
  156. // Found black, white, black, and stumbled back onto white; done
  157. var diffX = x - fromX;
  158. var diffY = y - fromY;
  159. return Math.sqrt( (diffX * diffX + diffY * diffY));
  160. }
  161. error += dy;
  162. if (error > 0)
  163. {
  164. if (y == toY)
  165. {
  166. break;
  167. }
  168. y += ystep;
  169. error -= dx;
  170. }
  171. }
  172. var diffX2 = toX - fromX;
  173. var diffY2 = toY - fromY;
  174. return Math.sqrt( (diffX2 * diffX2 + diffY2 * diffY2));
  175. }
  176. this.sizeOfBlackWhiteBlackRunBothWays=function( fromX, fromY, toX, toY)
  177. {
  178. var result = this.sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
  179. // Now count other way -- don't run off image though of course
  180. var scale = 1.0;
  181. var otherToX = fromX - (toX - fromX);
  182. if (otherToX < 0)
  183. {
  184. scale = fromX / (fromX - otherToX);
  185. otherToX = 0;
  186. }
  187. else if (otherToX >= qrcode.width)
  188. {
  189. scale = (qrcode.width - 1 - fromX) / (otherToX - fromX);
  190. otherToX = qrcode.width - 1;
  191. }
  192. var otherToY = Math.floor (fromY - (toY - fromY) * scale);
  193. scale = 1.0;
  194. if (otherToY < 0)
  195. {
  196. scale = fromY / (fromY - otherToY);
  197. otherToY = 0;
  198. }
  199. else if (otherToY >= qrcode.height)
  200. {
  201. scale = (qrcode.height - 1 - fromY) / (otherToY - fromY);
  202. otherToY = qrcode.height - 1;
  203. }
  204. otherToX = Math.floor (fromX + (otherToX - fromX) * scale);
  205. result += this.sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
  206. return result - 1.0; // -1 because we counted the middle pixel twice
  207. }
  208. this.calculateModuleSizeOneWay=function( pattern, otherPattern)
  209. {
  210. var moduleSizeEst1 = this.sizeOfBlackWhiteBlackRunBothWays(Math.floor( pattern.X), Math.floor( pattern.Y), Math.floor( otherPattern.X), Math.floor(otherPattern.Y));
  211. var moduleSizeEst2 = this.sizeOfBlackWhiteBlackRunBothWays(Math.floor(otherPattern.X), Math.floor(otherPattern.Y), Math.floor( pattern.X), Math.floor(pattern.Y));
  212. if (isNaN(moduleSizeEst1))
  213. {
  214. return moduleSizeEst2 / 7.0;
  215. }
  216. if (isNaN(moduleSizeEst2))
  217. {
  218. return moduleSizeEst1 / 7.0;
  219. }
  220. // Average them, and divide by 7 since we've counted the width of 3 black modules,
  221. // and 1 white and 1 black module on either side. Ergo, divide sum by 14.
  222. return (moduleSizeEst1 + moduleSizeEst2) / 14.0;
  223. }
  224. this.calculateModuleSize=function( topLeft, topRight, bottomLeft)
  225. {
  226. // Take the average
  227. return (this.calculateModuleSizeOneWay(topLeft, topRight) + this.calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0;
  228. }
  229. this.distance=function( pattern1, pattern2)
  230. {
  231. var xDiff = pattern1.X - pattern2.X;
  232. var yDiff = pattern1.Y - pattern2.Y;
  233. return Math.sqrt( (xDiff * xDiff + yDiff * yDiff));
  234. }
  235. this.computeDimension=function( topLeft, topRight, bottomLeft, moduleSize)
  236. {
  237. var tltrCentersDimension = Math.round(this.distance(topLeft, topRight) / moduleSize);
  238. var tlblCentersDimension = Math.round(this.distance(topLeft, bottomLeft) / moduleSize);
  239. var dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
  240. switch (dimension & 0x03)
  241. {
  242. // mod 4
  243. case 0:
  244. dimension++;
  245. break;
  246. // 1? do nothing
  247. case 2:
  248. dimension--;
  249. break;
  250. case 3:
  251. throw "Error";
  252. }
  253. return dimension;
  254. }
  255. this.findAlignmentInRegion=function( overallEstModuleSize, estAlignmentX, estAlignmentY, allowanceFactor)
  256. {
  257. // Look for an alignment pattern (3 modules in size) around where it
  258. // should be
  259. var allowance = Math.floor (allowanceFactor * overallEstModuleSize);
  260. var alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance);
  261. var alignmentAreaRightX = Math.min(qrcode.width - 1, estAlignmentX + allowance);
  262. if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3)
  263. {
  264. throw "Error";
  265. }
  266. var alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
  267. var alignmentAreaBottomY = Math.min(qrcode.height - 1, estAlignmentY + allowance);
  268. var alignmentFinder = new AlignmentPatternFinder(this.image, alignmentAreaLeftX, alignmentAreaTopY, alignmentAreaRightX - alignmentAreaLeftX, alignmentAreaBottomY - alignmentAreaTopY, overallEstModuleSize, this.resultPointCallback);
  269. return alignmentFinder.find();
  270. }
  271. this.createTransform=function( topLeft, topRight, bottomLeft, alignmentPattern, dimension)
  272. {
  273. var dimMinusThree = dimension - 3.5;
  274. var bottomRightX;
  275. var bottomRightY;
  276. var sourceBottomRightX;
  277. var sourceBottomRightY;
  278. if (alignmentPattern != null)
  279. {
  280. bottomRightX = alignmentPattern.X;
  281. bottomRightY = alignmentPattern.Y;
  282. sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0;
  283. }
  284. else
  285. {
  286. // Don't have an alignment pattern, just make up the bottom-right point
  287. bottomRightX = (topRight.X - topLeft.X) + bottomLeft.X;
  288. bottomRightY = (topRight.Y - topLeft.Y) + bottomLeft.Y;
  289. sourceBottomRightX = sourceBottomRightY = dimMinusThree;
  290. }
  291. 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);
  292. return transform;
  293. }
  294. this.sampleGrid=function( image, transform, dimension)
  295. {
  296. var sampler = GridSampler;
  297. return sampler.sampleGrid3(image, dimension, transform);
  298. }
  299. this.processFinderPatternInfo = function( info)
  300. {
  301. var topLeft = info.TopLeft;
  302. var topRight = info.TopRight;
  303. var bottomLeft = info.BottomLeft;
  304. var moduleSize = this.calculateModuleSize(topLeft, topRight, bottomLeft);
  305. if (moduleSize < 1.0)
  306. {
  307. throw "Error";
  308. }
  309. var dimension = this.computeDimension(topLeft, topRight, bottomLeft, moduleSize);
  310. var provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
  311. var modulesBetweenFPCenters = provisionalVersion.DimensionForVersion - 7;
  312. var alignmentPattern = null;
  313. // Anything above version 1 has an alignment pattern
  314. if (provisionalVersion.AlignmentPatternCenters.length > 0)
  315. {
  316. // Guess where a "bottom right" finder pattern would have been
  317. var bottomRightX = topRight.X - topLeft.X + bottomLeft.X;
  318. var bottomRightY = topRight.Y - topLeft.Y + bottomLeft.Y;
  319. // Estimate that alignment pattern is closer by 3 modules
  320. // from "bottom right" to known top left location
  321. var correctionToTopLeft = 1.0 - 3.0 / modulesBetweenFPCenters;
  322. var estAlignmentX = Math.floor (topLeft.X + correctionToTopLeft * (bottomRightX - topLeft.X));
  323. var estAlignmentY = Math.floor (topLeft.Y + correctionToTopLeft * (bottomRightY - topLeft.Y));
  324. // Kind of arbitrary -- expand search radius before giving up
  325. for (var i = 4; i <= 16; i <<= 1)
  326. {
  327. //try
  328. //{
  329. alignmentPattern = this.findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, i);
  330. break;
  331. //}
  332. //catch (re)
  333. //{
  334. // try next round
  335. //}
  336. }
  337. // If we didn't find alignment pattern... well try anyway without it
  338. }
  339. var transform = this.createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension);
  340. var bits = this.sampleGrid(this.image, transform, dimension);
  341. var points;
  342. if (alignmentPattern == null)
  343. {
  344. points = new Array(bottomLeft, topLeft, topRight);
  345. }
  346. else
  347. {
  348. points = new Array(bottomLeft, topLeft, topRight, alignmentPattern);
  349. }
  350. return new DetectorResult(bits, points);
  351. }
  352. this.detect=function()
  353. {
  354. var info = new FinderPatternFinder().findFinderPattern(this.image);
  355. return this.processFinderPatternInfo(info);
  356. }
  357. }