2 * Copyright 2007 Google Inc.
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.MonochromeBitmapSource;
20 import com.google.zxing.ReaderException;
23 * Implementations of this class can, given locations of finder patterns for a QR code in an
24 * image, sample the right points in the image to reconstruct the QR code, accounting for
25 * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
26 * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
27 * Imaging library, but which may not be available in other environments such as J2ME, and vice
30 * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
31 * with an instance of a class which implements this interface.
33 * @author srowen@google.com (Sean Owen)
35 public abstract class GridSampler {
37 private static GridSampler gridSampler;
40 * Sets the implementation of {@link GridSampler} used by the library. One global
41 * instance is stored, which may sound problematic. But, the implementation provided
42 * ought to be appropriate for the entire platform, and all uses of this library
43 * in the whole lifetime of the JVM. For instance, an Android activity can swap in
44 * an implementation that takes advantage of native platform libraries.
46 * @param newGridSampler
48 public static void setGridSampler(GridSampler newGridSampler) {
49 if (newGridSampler == null) {
50 throw new IllegalArgumentException();
52 gridSampler = newGridSampler;
56 * @return the current implementation of {@link GridSampler}
58 public static GridSampler getInstance() {
59 if (gridSampler == null) {
60 gridSampler = new DefaultGridSampler();
66 * <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract the
67 * black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode may be
68 * rotated or perspective-distorted, the caller supplies four points in the source image that define
69 * known points in the barcode, so that the image may be sampled appropriately.</p>
71 * <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in
72 * the image that define some significant points in the image to be sample. For example,
73 * these may be the location of finder pattern in a QR Code.</p>
75 * <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination
76 * {@link BitMatrix}, from the top left, where the known points in the image given by the "from" parameters
79 * <p>These 16 parameters define the transformation needed to sample the image.</p>
81 * @param image image to sample
82 * @param dimension width/height of {@link BitMatrix} to sample from iamge
83 * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
84 * defined by the "from" parameters
85 * @throws ReaderException if image can't be sampled, for example, if the transformation defined by
86 * the given points is invalid or results in sampling outside the image boundaries
88 public abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
90 float p1ToX, float p1ToY,
91 float p2ToX, float p2ToY,
92 float p3ToX, float p3ToY,
93 float p4ToX, float p4ToY,
94 float p1FromX, float p1FromY,
95 float p2FromX, float p2FromY,
96 float p3FromX, float p3FromY,
97 float p4FromX, float p4FromY) throws ReaderException;
100 * <p>Checks a set of points that have been transformed to sample points on an image against
101 * the image's dimensions to see if the point are even within the image.</p>
103 * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be barely
104 * (less than 1 pixel) off the image. This accounts for imperfect detection of finder patterns in an image
105 * where the QR Code runs all the way to the image border.</p>
107 * <p>For efficiency, the method will check points from either end of the line until one is found
108 * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
110 * @param image image into which the points should map
111 * @param points actual points in x1,y1,...,xn,yn form
112 * @throws ReaderException if an endpoint is lies outside the image boundaries
114 protected static void checkAndNudgePoints(MonochromeBitmapSource image, float[] points) throws ReaderException {
115 int width = image.getWidth();
116 int height = image.getHeight();
117 // Check and nudge points from start until we see some that are OK:
118 boolean nudged = true;
119 for (int offset = 0; offset < points.length && nudged; offset += 2) {
120 int x = (int) points[offset];
121 int y = (int) points[offset + 1];
122 if (x < -1 || x > width || y < -1 || y > height) {
123 throw new ReaderException("Transformed point out of bounds at " + x + ',' + y);
127 points[offset] = 0.0f;
129 } else if (x == width) {
130 points[offset] = width - 1;
134 points[offset + 1] = 0.0f;
136 } else if (y == height) {
137 points[offset + 1] = height - 1;
141 // Check and nudge points from end:
143 for (int offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
144 int x = (int) points[offset];
145 int y = (int) points[offset + 1];
146 if (x < -1 || x > width || y < -1 || y > height) {
147 throw new ReaderException("Transformed point out of bounds at " + x + ',' + y);
151 points[offset] = 0.0f;
153 } else if (x == width) {
154 points[offset] = width - 1;
158 points[offset + 1] = 0.0f;
160 } else if (y == height) {
161 points[offset + 1] = height - 1;