| 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; |
| 18 | |
| 19 | import com.google.zxing.common.detector.MathUtils; |
| 20 | |
| 21 | /** |
| 22 | * <p>Encapsulates a point of interest in an image containing a barcode. Typically, this |
| 23 | * would be the location of a finder pattern or the corner of the barcode, for example.</p> |
| 24 | * |
| 25 | * @author Sean Owen |
| 26 | */ |
| 27 | public class ResultPoint { |
| 28 | |
| 29 | private final float x; |
| 30 | private final float y; |
| 31 | |
| 32 | public ResultPoint(float x, float y) { |
| 33 | this.x = x; |
| 34 | this.y = y; |
| 35 | } |
| 36 | |
| 37 | public final float getX() { |
| 38 | return x; |
| 39 | } |
| 40 | |
| 41 | public final float getY() { |
| 42 | return y; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public final boolean equals(Object other) { |
| 47 | if (other instanceof ResultPoint) { |
| 48 | ResultPoint otherPoint = (ResultPoint) other; |
| 49 | return x == otherPoint.x && y == otherPoint.y; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public final int hashCode() { |
| 56 | return 31 * Float.floatToIntBits(x) + Float.floatToIntBits(y); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public final String toString() { |
| 61 | return "(" + x + ',' + y + ')'; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC |
| 66 | * and BC is less than AC, and the angle between BC and BA is less than 180 degrees. |
| 67 | * |
| 68 | * @param patterns array of three {@code ResultPoint} to order |
| 69 | */ |
| 70 | public static void orderBestPatterns(ResultPoint[] patterns) { |
| 71 | |
| 72 | // Find distances between pattern centers |
| 73 | float zeroOneDistance = distance(patterns[0], patterns[1]); |
| 74 | float oneTwoDistance = distance(patterns[1], patterns[2]); |
| 75 | float zeroTwoDistance = distance(patterns[0], patterns[2]); |
| 76 | |
| 77 | ResultPoint pointA; |
| 78 | ResultPoint pointB; |
| 79 | ResultPoint pointC; |
| 80 | // Assume one closest to other two is B; A and C will just be guesses at first |
| 81 | if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) { |
| 82 | pointB = patterns[0]; |
| 83 | pointA = patterns[1]; |
| 84 | pointC = patterns[2]; |
| 85 | } else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) { |
| 86 | pointB = patterns[1]; |
| 87 | pointA = patterns[0]; |
| 88 | pointC = patterns[2]; |
| 89 | } else { |
| 90 | pointB = patterns[2]; |
| 91 | pointA = patterns[0]; |
| 92 | pointC = patterns[1]; |
| 93 | } |
| 94 | |
| 95 | // Use cross product to figure out whether A and C are correct or flipped. |
| 96 | // This asks whether BC x BA has a positive z component, which is the arrangement |
| 97 | // we want for A, B, C. If it's negative, then we've got it flipped around and |
| 98 | // should swap A and C. |
| 99 | if (crossProductZ(pointA, pointB, pointC) < 0.0f) { |
| 100 | ResultPoint temp = pointA; |
| 101 | pointA = pointC; |
| 102 | pointC = temp; |
| 103 | } |
| 104 | |
| 105 | patterns[0] = pointA; |
| 106 | patterns[1] = pointB; |
| 107 | patterns[2] = pointC; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param pattern1 first pattern |
| 112 | * @param pattern2 second pattern |
| 113 | * @return distance between two points |
| 114 | */ |
| 115 | public static float distance(ResultPoint pattern1, ResultPoint pattern2) { |
| 116 | return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the z component of the cross product between vectors BC and BA. |
| 121 | */ |
| 122 | private static float crossProductZ(ResultPoint pointA, |
| 123 | ResultPoint pointB, |
| 124 | ResultPoint pointC) { |
| 125 | float bX = pointB.x; |
| 126 | float bY = pointB.y; |
| 127 | return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX)); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |