]> Pileus Git - ~andy/freeotp/blob - src/com/google/zxing/common/DefaultGridSampler.java
Add native camera support
[~andy/freeotp] / src / com / google / zxing / common / DefaultGridSampler.java
1 /*
2  * Copyright 2007 ZXing authors
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.common;
18
19 import com.google.zxing.NotFoundException;
20
21 /**
22  * @author Sean Owen
23  */
24 public final class DefaultGridSampler extends GridSampler {
25
26   @Override
27   public BitMatrix sampleGrid(BitMatrix image,
28                               int dimensionX,
29                               int dimensionY,
30                               float p1ToX, float p1ToY,
31                               float p2ToX, float p2ToY,
32                               float p3ToX, float p3ToY,
33                               float p4ToX, float p4ToY,
34                               float p1FromX, float p1FromY,
35                               float p2FromX, float p2FromY,
36                               float p3FromX, float p3FromY,
37                               float p4FromX, float p4FromY) throws NotFoundException {
38
39     PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
40         p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
41         p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
42
43     return sampleGrid(image, dimensionX, dimensionY, transform);
44   }
45
46   @Override
47   public BitMatrix sampleGrid(BitMatrix image,
48                               int dimensionX,
49                               int dimensionY,
50                               PerspectiveTransform transform) throws NotFoundException {
51     if (dimensionX <= 0 || dimensionY <= 0) {
52       throw NotFoundException.getNotFoundInstance();      
53     }
54     BitMatrix bits = new BitMatrix(dimensionX, dimensionY);
55     float[] points = new float[dimensionX << 1];
56     for (int y = 0; y < dimensionY; y++) {
57       int max = points.length;
58       float iValue = (float) y + 0.5f;
59       for (int x = 0; x < max; x += 2) {
60         points[x] = (float) (x >> 1) + 0.5f;
61         points[x + 1] = iValue;
62       }
63       transform.transformPoints(points);
64       // Quick check to see if points transformed to something inside the image;
65       // sufficient to check the endpoints
66       checkAndNudgePoints(image, points);
67       try {
68         for (int x = 0; x < max; x += 2) {
69           if (image.get((int) points[x], (int) points[x + 1])) {
70             // Black(-ish) pixel
71             bits.set(x >> 1, y);
72           }
73         }
74       } catch (ArrayIndexOutOfBoundsException aioobe) {
75         // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
76         // transform gets "twisted" such that it maps a straight line of points to a set of points
77         // whose endpoints are in bounds, but others are not. There is probably some mathematical
78         // way to detect this about the transformation that I don't know yet.
79         // This results in an ugly runtime exception despite our clever checks above -- can't have
80         // that. We could check each point's coordinates but that feels duplicative. We settle for
81         // catching and wrapping ArrayIndexOutOfBoundsException.
82         throw NotFoundException.getNotFoundInstance();
83       }
84     }
85     return bits;
86   }
87
88 }