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.qrcode.detector;
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.common.BitMatrix;
24 * Implementations of this class can, given locations of finder patterns for a QR code in an
25 * image, sample the right points in the image to reconstruct the QR code, accounting for
26 * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
27 * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
28 * Imaging library, but which may not be available in other environments such as J2ME, and vice
31 * The implementation used can be controlled by calling {@link #setGridSamplerClassName(String)}
32 * with the name of a class which implements this interface.
34 * @author srowen@google.com (Sean Owen)
36 public abstract class GridSampler {
38 private static final String DEFAULT_IMPL_CLASS =
39 "com.google.zxing.qrcode.detector.DefaultGridSampler";
41 private static String gridSamplerClassName = DEFAULT_IMPL_CLASS;
42 private static GridSampler gridSampler;
44 public static void setGridSamplerClassName(String className) {
45 if (className == null) {
46 throw new IllegalArgumentException();
48 gridSamplerClassName = className;
51 public static GridSampler getInstance() {
52 if (gridSampler == null) {
54 Class gridSamplerClass = Class.forName(gridSamplerClassName);
55 gridSampler = (GridSampler) gridSamplerClass.newInstance();
56 } catch (ClassNotFoundException cnfe) {
57 throw new RuntimeException(cnfe.toString());
58 } catch (IllegalAccessException iae) {
59 throw new RuntimeException(iae.toString());
60 } catch (InstantiationException ie) {
61 throw new RuntimeException(ie.toString());
67 protected abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
68 FinderPattern topLeft,
69 FinderPattern topRight,
70 FinderPattern bottomLeft,
71 AlignmentPattern alignmentPattern,
72 int dimension) throws ReaderException;
74 protected static void checkEndpoint(MonochromeBitmapSource image, float[] points)
75 throws ReaderException {
76 int x = (int) points[0];
77 int y = (int) points[1];
78 if (x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) {
79 throw new ReaderException("Transformed point out of bounds at " + x + ',' + y);
81 x = (int) points[points.length - 2];
82 y = (int) points[points.length - 1];
83 if (x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) {
84 throw new ReaderException("Transformed point out of bounds at " + x + ',' + y);