2 * Licensed under the Apache License, Version 2.0 (the "License");
\r
3 * you may not use this file except in compliance with the License.
\r
4 * You may obtain a copy of the License at
\r
6 * http://www.apache.org/licenses/LICENSE-2.0
\r
8 * Unless required by applicable law or agreed to in writing, software
\r
9 * distributed under the License is distributed on an "AS IS" BASIS,
\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
11 * See the License for the specific language governing permissions and
\r
12 * limitations under the License.
\r
14 namespace com.google.zxing.oned
\r
17 * <p>Implements decoding of the EAN-13 format.</p>
\r
19 * @author dswitkin@google.com (Daniel Switkin)
\r
21 * @author alasdair@google.com (Alasdair Mackintosh)
\r
25 using com.google.zxing.common;
\r
27 public sealed class ITFReader : AbstractOneDReader
\r
29 private static int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
\r
30 private static int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f);
\r
32 private static int W = 3; // Pixel width of a wide line
\r
33 private static int N = 1; // Pixed width of a narrow line
\r
35 // Stores the actual narrow line width of the image being decoded.
\r
36 private int narrowLineWidth = -1;
\r
39 * Start/end guard pattern.
\r
41 * Note: The end pattern is reversed because the row is reversed before
\r
42 * searching for the END_PATTERN
\r
44 private static int[] START_PATTERN = {N, N, N, N};
\r
45 private static int[] END_PATTERN_REVERSED = {N, N, W};
\r
48 * Patterns of Wide / Narrow lines to indicate each digit
\r
50 private static int[][] PATTERNS = new int[][]{
\r
51 new int[]{N, N, W, W, N}, // 0
\r
52 new int[]{W, N, N, N, W}, // 1
\r
53 new int[]{N, W, N, N, W}, // 2
\r
54 new int[]{W, W, N, N, N}, // 3
\r
55 new int[]{N, N, W, N, W}, // 4
\r
56 new int[]{W, N, W, N, N}, // 5
\r
57 new int[]{N, W, W, N, N}, // 6
\r
58 new int[]{N, N, N, W, W}, // 7
\r
59 new int[]{W, N, N, W, N}, // 8
\r
60 new int[]{N, W, N, W, N} // 9
\r
63 public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints) {
\r
65 StringBuilder result = new StringBuilder(20);
\r
67 // Find out where the Middle section (payload) starts & ends
\r
68 int[] startRange = decodeStart(row);
\r
69 int[] endRange = decodeEnd(row);
\r
71 decodeMiddle(row, startRange[1], endRange[0], result);
\r
73 string resultString = result.ToString();
\r
75 // To avoid false positives with 2D barcodes (and other patterns), make
\r
76 // an assumption that the decoded string must be 6, 10 or 14 digits.
\r
77 int length = resultString.Length;
\r
78 if (length != 6 && length != 10 && length != 14) {
\r
79 throw new ReaderException();
\r
84 null, // no natural byte representation for these barcodes
\r
85 new ResultPoint[] { new GenericResultPoint(startRange[1], (float) rowNumber),
\r
86 new GenericResultPoint(startRange[0], (float) rowNumber)},
\r
91 * @param row row of black/white values to search
\r
92 * @param payloadStart offset of start pattern
\r
93 * @param resultString {@link StringBuilder} to Append decoded chars to
\r
94 * @throws ReaderException if decoding could not complete successfully
\r
96 static void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuilder resultString) {
\r
98 // Digits are interleaved in pairs - 5 black lines for one digit, and the
\r
100 // interleaved white lines for the second digit.
\r
101 // Therefore, need to scan 10 lines and then
\r
102 // split these into two arrays
\r
103 int[] counterDigitPair = new int[10];
\r
104 int[] counterBlack = new int[5];
\r
105 int[] counterWhite = new int[5];
\r
107 while (payloadStart < payloadEnd) {
\r
109 // Get 10 runs of black/white.
\r
110 recordPattern(row, payloadStart, counterDigitPair);
\r
111 // Split them into each array
\r
112 for (int k = 0; k < 5; k++) {
\r
114 counterBlack[k] = counterDigitPair[twoK];
\r
115 counterWhite[k] = counterDigitPair[twoK + 1];
\r
118 int bestMatch = decodeDigit(counterBlack);
\r
119 resultString.Append((char) ('0' + bestMatch));
\r
120 bestMatch = decodeDigit(counterWhite);
\r
121 resultString.Append((char) ('0' + bestMatch));
\r
123 for (int i = 0; i < counterDigitPair.Length; i++) {
\r
124 payloadStart += counterDigitPair[i];
\r
130 * Identify where the start of the middle / payload section starts.
\r
132 * @param row row of black/white values to search
\r
133 * @return Array, containing index of start of 'start block' and end of
\r
135 * @throws ReaderException
\r
137 int[] decodeStart(BitArray row) {
\r
138 int endStart = skipWhiteSpace(row);
\r
139 int[] startPattern = findGuardPattern(row, endStart, START_PATTERN);
\r
141 // Determine the width of a narrow line in pixels. We can do this by
\r
142 // getting the width of the start pattern and dividing by 4 because its
\r
143 // made up of 4 narrow lines.
\r
144 this.narrowLineWidth = (startPattern[1] - startPattern[0]) >> 2;
\r
146 validateQuietZone(row, startPattern[0]);
\r
148 return startPattern;
\r
152 * The start & end patterns must be pre/post fixed by a quiet zone. This
\r
153 * zone must be at least 10 times the width of a narrow line. Scan back until
\r
154 * we either get to the start of the barcode or match the necessary number of
\r
155 * quiet zone pixels.
\r
157 * Note: Its assumed the row is reversed when using this method to find
\r
158 * quiet zone after the end pattern.
\r
160 * ref: http://www.barcode-1.net/i25code.html
\r
162 * @param row bit array representing the scanned barcode.
\r
163 * @param startPattern index into row of the start or end pattern.
\r
164 * @throws ReaderException if the quiet zone cannot be found, a ReaderException is thrown.
\r
166 private void validateQuietZone(BitArray row, int startPattern) {
\r
168 int quietCount = this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone
\r
170 for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) {
\r
176 if (quietCount != 0) {
\r
177 // Unable to find the necessary number of quiet zone pixels.
\r
178 throw new ReaderException();
\r
183 * Skip all whitespace until we get to the first black line.
\r
185 * @param row row of black/white values to search
\r
186 * @return index of the first black line.
\r
187 * @throws ReaderException Throws exception if no black lines are found in the row
\r
189 private int skipWhiteSpace(BitArray row) {
\r
190 int width = row.getSize();
\r
192 while (endStart < width) {
\r
193 if (row.get(endStart)) {
\r
198 if (endStart == width) {
\r
199 throw new ReaderException();
\r
206 * Identify where the end of the middle / payload section ends.
\r
208 * @param row row of black/white values to search
\r
209 * @return Array, containing index of start of 'end block' and end of 'end
\r
211 * @throws ReaderException
\r
214 int[] decodeEnd(BitArray row) {
\r
216 // For convenience, reverse the row and then
\r
217 // search from 'the start' for the end block
\r
220 int endStart = skipWhiteSpace(row);
\r
223 endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED);
\r
224 } catch (ReaderException e) {
\r
225 // Put our row of data back the right way before throwing
\r
230 // The start & end patterns must be pre/post fixed by a quiet zone. This
\r
231 // zone must be at least 10 times the width of a narrow line.
\r
232 // ref: http://www.barcode-1.net/i25code.html
\r
233 validateQuietZone(row, endPattern[0]);
\r
235 // Now recalc the indicies of where the 'endblock' starts & stops to
\r
237 // the reversed nature of the search
\r
238 int temp = endPattern[0];
\r
239 endPattern[0] = row.getSize() - endPattern[1];
\r
240 endPattern[1] = row.getSize() - temp;
\r
242 // Put the row back the righ way.
\r
248 * @param row row of black/white values to search
\r
249 * @param rowOffset position to start search
\r
250 * @param pattern pattern of counts of number of black and white pixels that are
\r
251 * being searched for as a pattern
\r
252 * @return start/end horizontal offset of guard pattern, as an array of two
\r
254 * @throws ReaderException if pattern is not found
\r
256 int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) {
\r
258 // TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be merged to
\r
259 // a single method.
\r
261 int patternLength = pattern.Length;
\r
262 int[] counters = new int[patternLength];
\r
263 int width = row.getSize();
\r
264 bool isWhite = false;
\r
266 int counterPosition = 0;
\r
267 int patternStart = rowOffset;
\r
268 for (int x = rowOffset; x < width; x++) {
\r
269 bool pixel = row.get(x);
\r
270 if ((!pixel && isWhite) || (pixel && !isWhite)) {
\r
271 counters[counterPosition]++;
\r
273 if (counterPosition == patternLength - 1) {
\r
274 if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
\r
275 return new int[]{patternStart, x};
\r
277 patternStart += counters[0] + counters[1];
\r
278 for (int y = 2; y < patternLength; y++) {
\r
279 counters[y - 2] = counters[y];
\r
281 counters[patternLength - 2] = 0;
\r
282 counters[patternLength - 1] = 0;
\r
287 counters[counterPosition] = 1;
\r
288 isWhite = !isWhite;
\r
291 throw new ReaderException();
\r
295 * Attempts to decode a sequence of ITF black/white lines into single
\r
298 * @param counters the counts of runs of observed black/white/black/... values
\r
299 * @return The decoded digit
\r
300 * @throws ReaderException if digit cannot be decoded
\r
302 private static int decodeDigit(int[] counters) {
\r
304 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
\r
305 int bestMatch = -1;
\r
306 int max = PATTERNS.Length;
\r
307 for (int i = 0; i < max; i++) {
\r
308 int[] pattern = PATTERNS[i];
\r
309 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
\r
310 if (variance < bestVariance) {
\r
311 bestVariance = variance;
\r
315 if (bestMatch >= 0) {
\r
318 throw new ReaderException();
\r