Move GridSampler into common package and refactor to ready it for use with Data Matrix
[zxing.git] / android / src / com / google / zxing / client / android / AndroidGraphicsGridSampler.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.client.android;
18
19 import android.graphics.Matrix;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitMatrix;
23 import com.google.zxing.common.GridSampler;
24
25 /**
26  * Implementation based on Android's
27  * {@link Matrix#setPolyToPoly(float[], int, float[], int, int)}
28  * class, which should offer faster performance for these matrix
29  * operations.
30  * 
31  * @author srowen@google.com (Sean Owen)
32  */
33 public final class AndroidGraphicsGridSampler extends GridSampler {
34
35   @Override
36   public BitMatrix sampleGrid(MonochromeBitmapSource image,
37                               int dimension,
38                               float p1ToX, float p1ToY,
39                               float p2ToX, float p2ToY,
40                               float p3ToX, float p3ToY,
41                               float p4ToX, float p4ToY,
42                               float p1FromX, float p1FromY,
43                               float p2FromX, float p2FromY,
44                               float p3FromX, float p3FromY,
45                               float p4FromX, float p4FromY) throws ReaderException {
46
47     Matrix transformMatrix = new Matrix();
48     boolean succeeded = transformMatrix.setPolyToPoly(
49       new float[] { p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY },
50       0,
51       new float[] { p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY },
52       0,
53       4
54     );
55     if (!succeeded) {
56       throw new ReaderException("Could not establish transformation matrix");
57     }
58
59     BitMatrix bits = new BitMatrix(dimension);
60     float[] points = new float[dimension << 1];
61     for (int i = 0; i < dimension; i++) {
62       int max = points.length;
63       float iValue = (float) i + 0.5f;
64       for (int j = 0; j < max; j += 2) {
65         points[j] = (float) (j >> 1) + 0.5f;
66         points[j + 1] = iValue;
67       }
68       transformMatrix.mapPoints(points);
69       // Quick check to see if points transformed to something inside the image;
70       // sufficent to check the endpoints
71       checkEndpoint(image, points);
72       for (int j = 0; j < max; j += 2) {
73         if (image.isBlack((int) points[j], (int) points[j + 1])) {
74           // Black(-ish) pixel
75           bits.set(i, j >> 1);
76         }
77       }
78     }
79     return bits;
80   }
81
82 }