]> Pileus Git - ~andy/freeotp/blob - src/com/google/zxing/qrcode/detector/FinderPattern.java
Add native camera support
[~andy/freeotp] / src / com / google / zxing / qrcode / detector / FinderPattern.java
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
17 package com.google.zxing.qrcode.detector;
18
19 import com.google.zxing.ResultPoint;
20
21 /**
22  * <p>Encapsulates a finder pattern, which are the three square patterns found in
23  * the corners of QR Codes. It also encapsulates a count of similar finder patterns,
24  * as a convenience to the finder's bookkeeping.</p>
25  *
26  * @author Sean Owen
27  */
28 public final class FinderPattern extends ResultPoint {
29
30   private final float estimatedModuleSize;
31   private final int count;
32
33   FinderPattern(float posX, float posY, float estimatedModuleSize) {
34     this(posX, posY, estimatedModuleSize, 1);
35   }
36
37   private FinderPattern(float posX, float posY, float estimatedModuleSize, int count) {
38     super(posX, posY);
39     this.estimatedModuleSize = estimatedModuleSize;
40     this.count = count;
41   }
42
43   public float getEstimatedModuleSize() {
44     return estimatedModuleSize;
45   }
46
47   int getCount() {
48     return count;
49   }
50
51   /*
52   void incrementCount() {
53     this.count++;
54   }
55    */
56
57   /**
58    * <p>Determines if this finder pattern "about equals" a finder pattern at the stated
59    * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
60    */
61   boolean aboutEquals(float moduleSize, float i, float j) {
62     if (Math.abs(i - getY()) <= moduleSize && Math.abs(j - getX()) <= moduleSize) {
63       float moduleSizeDiff = Math.abs(moduleSize - estimatedModuleSize);
64       return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize;
65     }
66     return false;
67   }
68
69   /**
70    * Combines this object's current estimate of a finder pattern position and module size
71    * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
72    * based on count.
73    */
74   FinderPattern combineEstimate(float i, float j, float newModuleSize) {
75     int combinedCount = count + 1;
76     float combinedX = (count * getX() + j) / combinedCount;
77     float combinedY = (count * getY() + i) / combinedCount;
78     float combinedModuleSize = (count * estimatedModuleSize + newModuleSize) / combinedCount;
79     return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount);
80   }
81
82 }