git-svn-id: http://zxing.googlecode.com/svn/trunk@2 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / src / com / google / zxing / qrcode / detector / GridSampler.java
1 /*
2  * Copyright 2007 Google Inc.
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.qrcode.detector;
18
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.common.BitMatrix;
22
23 /**
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
29  * versa.
30  *
31  * The implementation used can be controlled by calling {@link #setGridSamplerClassName(String)}
32  * with the name of a class which implements this interface.
33  *
34  * @author srowen@google.com (Sean Owen)
35  */
36 public abstract class GridSampler {
37
38   private static final String DEFAULT_IMPL_CLASS =
39       "com.google.zxing.qrcode.detector.DefaultGridSampler";
40
41   private static String gridSamplerClassName = DEFAULT_IMPL_CLASS;
42   private static GridSampler gridSampler;
43
44   public static void setGridSamplerClassName(String className) {
45     if (className == null) {
46       throw new IllegalArgumentException();
47     }
48     gridSamplerClassName = className;
49   }
50
51   public static GridSampler getInstance() {
52     if (gridSampler == null) {
53       try {
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());
62       }
63     }
64     return gridSampler;
65   }
66
67   protected abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
68                                           FinderPattern topLeft,
69                                           FinderPattern topRight,
70                                           FinderPattern bottomLeft,
71                                           AlignmentPattern alignmentPattern,
72                                           int dimension) throws ReaderException;
73
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);
80     }
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);
85     }
86   }
87
88 }