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

FinderPattern.cs 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace ZXing.QrCode.Internal
  18. {
  19. /// <summary>
  20. /// <p>Encapsulates a finder pattern, which are the three square patterns found in
  21. /// the corners of QR Codes. It also encapsulates a count of similar finder patterns,
  22. /// as a convenience to the finder's bookkeeping.</p>
  23. /// </summary>
  24. /// <author>Sean Owen</author>
  25. public sealed class FinderPattern : ResultPoint
  26. {
  27. private readonly float estimatedModuleSize;
  28. private int count;
  29. internal FinderPattern(float posX, float posY, float estimatedModuleSize)
  30. : this(posX, posY, estimatedModuleSize, 1)
  31. {
  32. this.estimatedModuleSize = estimatedModuleSize;
  33. this.count = 1;
  34. }
  35. internal FinderPattern(float posX, float posY, float estimatedModuleSize, int count)
  36. : base(posX, posY)
  37. {
  38. this.estimatedModuleSize = estimatedModuleSize;
  39. this.count = count;
  40. }
  41. /// <summary>
  42. /// Gets the size of the estimated module.
  43. /// </summary>
  44. /// <value>
  45. /// The size of the estimated module.
  46. /// </value>
  47. public float EstimatedModuleSize
  48. {
  49. get
  50. {
  51. return estimatedModuleSize;
  52. }
  53. }
  54. internal int Count
  55. {
  56. get
  57. {
  58. return count;
  59. }
  60. }
  61. /*
  62. internal void incrementCount()
  63. {
  64. this.count++;
  65. }
  66. */
  67. /// <summary> <p>Determines if this finder pattern "about equals" a finder pattern at the stated
  68. /// position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
  69. /// </summary>
  70. internal bool aboutEquals(float moduleSize, float i, float j)
  71. {
  72. if (Math.Abs(i - Y) <= moduleSize && Math.Abs(j - X) <= moduleSize)
  73. {
  74. float moduleSizeDiff = Math.Abs(moduleSize - estimatedModuleSize);
  75. return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize;
  76. }
  77. return false;
  78. }
  79. /// <summary>
  80. /// Combines this object's current estimate of a finder pattern position and module size
  81. /// with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
  82. /// based on count.
  83. /// </summary>
  84. /// <param name="i">The i.</param>
  85. /// <param name="j">The j.</param>
  86. /// <param name="newModuleSize">New size of the module.</param>
  87. /// <returns></returns>
  88. internal FinderPattern combineEstimate(float i, float j, float newModuleSize)
  89. {
  90. int combinedCount = count + 1;
  91. float combinedX = (count * X + j) / combinedCount;
  92. float combinedY = (count * Y + i) / combinedCount;
  93. float combinedModuleSize = (count * estimatedModuleSize + newModuleSize) / combinedCount;
  94. return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount);
  95. }
  96. }
  97. }