- /**\r
- * <p>Having found three "best" finder patterns we need to decide which is the top-left, top-right,\r
- * bottom-left. We assume that the one closest to the other two is the top-left one; this is not\r
- * strictly true (imagine extreme perspective distortion) but for the moment is a serviceable assumption.\r
- * Lastly we sort top-right from bottom-left by figuring out orientation from vector cross products.</p>\r
- *\r
- * @param patterns three best {@link FinderPattern}s\r
- * @return same {@link FinderPattern}s ordered bottom-left, top-left, top-right\r
- */\r
- private static FinderPattern[] orderBestPatterns(FinderPattern[] patterns) {\r
-\r
- // Find distances between pattern centers\r
- float abDistance = distance(patterns[0], patterns[1]);\r
- float bcDistance = distance(patterns[1], patterns[2]);\r
- float acDistance = distance(patterns[0], patterns[2]);\r
-\r
- FinderPattern topLeft;\r
- FinderPattern topRight;\r
- FinderPattern bottomLeft;\r
- // Assume one closest to other two is top left;\r
- // topRight and bottomLeft will just be guesses below at first\r
- if (bcDistance >= abDistance && bcDistance >= acDistance) {\r
- topLeft = patterns[0];\r
- topRight = patterns[1];\r
- bottomLeft = patterns[2];\r
- } else if (acDistance >= bcDistance && acDistance >= abDistance) {\r
- topLeft = patterns[1];\r
- topRight = patterns[0];\r
- bottomLeft = patterns[2];\r
- } else {\r
- topLeft = patterns[2];\r
- topRight = patterns[0];\r
- bottomLeft = patterns[1];\r
- }\r
-\r
- // Use cross product to figure out which of other1/2 is the bottom left\r
- // pattern. The vector "top-left -> bottom-left" x "top-left -> top-right"\r
- // should yield a vector with positive z component\r
- if ((bottomLeft.getY() - topLeft.getY()) * (topRight.getX() - topLeft.getX()) <\r
- (bottomLeft.getX() - topLeft.getX()) * (topRight.getY() - topLeft.getY())) {\r
- FinderPattern temp = topRight;\r
- topRight = bottomLeft;\r
- bottomLeft = temp;\r
- }\r
-\r
- return new FinderPattern[]{bottomLeft, topLeft, topRight};\r
- }\r
-\r
- /**\r
- * @return distance between two points\r
- */\r
- static float distance(ResultPoint pattern1, ResultPoint pattern2) {\r
- float xDiff = pattern1.getX() - pattern2.getX();\r
- float yDiff = pattern1.getY() - pattern2.getY();\r
- return (float) Math.sqrt((double) (xDiff * xDiff + yDiff * yDiff));\r
- }\r
-\r