2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.common;
19 import com.google.zxing.NotFoundException;
22 * Implementations of this class can, given locations of finder patterns for a QR code in an
23 * image, sample the right points in the image to reconstruct the QR code, accounting for
24 * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
25 * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
26 * Imaging library, but which may not be available in other environments such as J2ME, and vice
29 * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
30 * with an instance of a class which implements this interface.
34 public abstract class GridSampler {
36 private static GridSampler gridSampler = new DefaultGridSampler();
39 * Sets the implementation of {@link GridSampler} used by the library. One global
40 * instance is stored, which may sound problematic. But, the implementation provided
41 * ought to be appropriate for the entire platform, and all uses of this library
42 * in the whole lifetime of the JVM. For instance, an Android activity can swap in
43 * an implementation that takes advantage of native platform libraries.
45 * @param newGridSampler The platform-specific object to install.
47 public static void setGridSampler(GridSampler newGridSampler) {
48 if (newGridSampler == null) {
49 throw new IllegalArgumentException();
51 gridSampler = newGridSampler;
55 * @return the current implementation of {@link GridSampler}
57 public static GridSampler getInstance() {
62 * Samples an image for a rectangular matrix of bits of the given dimension.
63 * @param image image to sample
64 * @param dimensionX width of {@link BitMatrix} to sample from image
65 * @param dimensionY height of {@link BitMatrix} to sample from image
66 * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
67 * defined by the "from" parameters
68 * @throws NotFoundException if image can't be sampled, for example, if the transformation defined
69 * by the given points is invalid or results in sampling outside the image boundaries
71 public abstract BitMatrix sampleGrid(BitMatrix image,
74 float p1ToX, float p1ToY,
75 float p2ToX, float p2ToY,
76 float p3ToX, float p3ToY,
77 float p4ToX, float p4ToY,
78 float p1FromX, float p1FromY,
79 float p2FromX, float p2FromY,
80 float p3FromX, float p3FromY,
81 float p4FromX, float p4FromY) throws NotFoundException;
83 public abstract BitMatrix sampleGrid(BitMatrix image,
86 PerspectiveTransform transform) throws NotFoundException;
89 * <p>Checks a set of points that have been transformed to sample points on an image against
90 * the image's dimensions to see if the point are even within the image.</p>
92 * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
93 * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
94 * patterns in an image where the QR Code runs all the way to the image border.</p>
96 * <p>For efficiency, the method will check points from either end of the line until one is found
97 * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
99 * @param image image into which the points should map
100 * @param points actual points in x1,y1,...,xn,yn form
101 * @throws NotFoundException if an endpoint is lies outside the image boundaries
103 protected static void checkAndNudgePoints(BitMatrix image, float[] points) throws NotFoundException {
104 int width = image.getWidth();
105 int height = image.getHeight();
106 // Check and nudge points from start until we see some that are OK:
107 boolean nudged = true;
108 for (int offset = 0; offset < points.length && nudged; offset += 2) {
109 int x = (int) points[offset];
110 int y = (int) points[offset + 1];
111 if (x < -1 || x > width || y < -1 || y > height) {
112 throw NotFoundException.getNotFoundInstance();
116 points[offset] = 0.0f;
118 } else if (x == width) {
119 points[offset] = width - 1;
123 points[offset + 1] = 0.0f;
125 } else if (y == height) {
126 points[offset + 1] = height - 1;
130 // Check and nudge points from end:
132 for (int offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
133 int x = (int) points[offset];
134 int y = (int) points[offset + 1];
135 if (x < -1 || x > width || y < -1 || y > height) {
136 throw NotFoundException.getNotFoundInstance();
140 points[offset] = 0.0f;
142 } else if (x == width) {
143 points[offset] = width - 1;
147 points[offset + 1] = 0.0f;
149 } else if (y == height) {
150 points[offset + 1] = height - 1;