3df26cf104a11d297c8fc90a864506d3ba21e78a
[zxing.git] / core / 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 implements ResultPoint {
29
30   private final float posX;
31   private final float posY;
32   private final float estimatedModuleSize;
33   private int count;
34
35   FinderPattern(float posX, float posY, float estimatedModuleSize) {
36     this.posX = posX;
37     this.posY = posY;
38     this.estimatedModuleSize = estimatedModuleSize;
39     this.count = 1;
40   }
41
42   public float getX() {
43     return posX;
44   }
45
46   public float getY() {
47     return posY;
48   }
49
50   float getEstimatedModuleSize() {
51     return estimatedModuleSize;
52   }
53
54   int getCount() {
55     return count;
56   }
57
58   void incrementCount() {
59     this.count++;
60   }
61
62   /**
63    * <p>Determines if this finder pattern "about equals" a finder pattern at the stated
64    * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
65    */
66   boolean aboutEquals(float moduleSize, float i, float j) {
67     return Math.abs(i - posY) <= moduleSize &&
68         Math.abs(j - posX) <= moduleSize &&
69         (Math.abs(moduleSize - estimatedModuleSize) <= 1.0f ||
70             Math.abs(moduleSize - estimatedModuleSize) / estimatedModuleSize <= 0.1f);
71   }
72
73 }