2 * Copyright 2008 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 using BarcodeFormat = com.google.zxing.BarcodeFormat;
\r
18 using ReaderException = com.google.zxing.ReaderException;
\r
19 using Result = com.google.zxing.Result;
\r
20 using ResultPoint = com.google.zxing.ResultPoint;
\r
21 using BitArray = com.google.zxing.common.BitArray;
\r
22 namespace com.google.zxing.oned
\r
25 /// <summary> <p>Decodes Code 39 barcodes. This does not support "Full ASCII Code 39" yet.</p>
\r
28 /// <author> Sean Owen
\r
30 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
32 public sealed class Code39Reader:OneDReader
\r
35 private const System.String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
\r
36 //UPGRADE_NOTE: Final was removed from the declaration of 'ALPHABET '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
37 private static readonly char[] ALPHABET = ALPHABET_STRING.ToCharArray();
\r
39 /// <summary> These represent the encodings of characters, as patterns of wide and narrow bars.
\r
40 /// The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
\r
41 /// with 1s representing "wide" and 0s representing narrow.
\r
43 //UPGRADE_NOTE: Final was removed from the declaration of 'CHARACTER_ENCODINGS'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
44 private static readonly int[] CHARACTER_ENCODINGS = new int[]{0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A};
\r
46 //UPGRADE_NOTE: Final was removed from the declaration of 'ASTERISK_ENCODING '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
47 private static readonly int ASTERISK_ENCODING = CHARACTER_ENCODINGS[39];
\r
49 //UPGRADE_NOTE: Final was removed from the declaration of 'usingCheckDigit '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
50 private bool usingCheckDigit;
\r
51 //UPGRADE_NOTE: Final was removed from the declaration of 'extendedMode '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
52 private bool extendedMode;
\r
54 /// <summary> Creates a reader that assumes all encoded data is data, and does not treat the final
\r
55 /// character as a check digit. It will not decoded "extended Code 39" sequences.
\r
57 public Code39Reader()
\r
59 usingCheckDigit = false;
\r
60 extendedMode = false;
\r
63 /// <summary> Creates a reader that can be configured to check the last character as a check digit.
\r
64 /// It will not decoded "extended Code 39" sequences.
\r
67 /// <param name="usingCheckDigit">if true, treat the last data character as a check digit, not
\r
68 /// data, and verify that the checksum passes.
\r
70 public Code39Reader(bool usingCheckDigit)
\r
72 this.usingCheckDigit = usingCheckDigit;
\r
73 this.extendedMode = false;
\r
76 /// <summary> Creates a reader that can be configured to check the last character as a check digit,
\r
77 /// or optionally attempt to decode "extended Code 39" sequences that are used to encode
\r
78 /// the full ASCII character set.
\r
81 /// <param name="usingCheckDigit">if true, treat the last data character as a check digit, not
\r
82 /// data, and verify that the checksum passes.
\r
84 /// <param name="extendedMode">if true, will attempt to decode extended Code 39 sequences in the
\r
87 public Code39Reader(bool usingCheckDigit, bool extendedMode)
\r
89 this.usingCheckDigit = usingCheckDigit;
\r
90 this.extendedMode = extendedMode;
\r
93 public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints)
\r
96 int[] start = findAsteriskPattern(row);
\r
97 int nextStart = start[1];
\r
100 // Read off white space
\r
101 while (nextStart < end && !row.get_Renamed(nextStart))
\r
106 System.Text.StringBuilder result = new System.Text.StringBuilder(20);
\r
107 int[] counters = new int[9];
\r
112 recordPattern(row, nextStart, counters);
\r
113 int pattern = toNarrowWidePattern(counters);
\r
116 throw ReaderException.Instance;
\r
118 decodedChar = patternToChar(pattern);
\r
119 result.Append(decodedChar);
\r
120 lastStart = nextStart;
\r
121 for (int i = 0; i < counters.Length; i++)
\r
123 nextStart += counters[i];
\r
125 // Read off white space
\r
126 while (nextStart < end && !row.get_Renamed(nextStart))
\r
131 while (decodedChar != '*');
\r
132 result.Remove(result.Length - 1, 1); // remove asterisk
\r
134 // Look for whitespace after pattern:
\r
135 int lastPatternSize = 0;
\r
136 for (int i = 0; i < counters.Length; i++)
\r
138 lastPatternSize += counters[i];
\r
140 int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
\r
141 // If 50% of last pattern size, following last pattern, is not whitespace, fail
\r
142 // (but if it's whitespace to the very end of the image, that's OK)
\r
143 if (nextStart != end && whiteSpaceAfterEnd / 2 < lastPatternSize)
\r
145 throw ReaderException.Instance;
\r
148 if (usingCheckDigit)
\r
150 int max = result.Length - 1;
\r
152 for (int i = 0; i < max; i++)
\r
154 total += ALPHABET_STRING.IndexOf((System.Char) result[i]);
\r
156 if (total % 43 != ALPHABET_STRING.IndexOf((System.Char) result[max]))
\r
158 throw ReaderException.Instance;
\r
160 result.Remove(max, 1);
\r
163 System.String resultString = result.ToString();
\r
166 resultString = decodeExtended(resultString);
\r
169 if (resultString.Length == 0)
\r
171 // Almost surely a false positive
\r
172 throw ReaderException.Instance;
\r
175 //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
\r
176 float left = (float) (start[1] + start[0]) / 2.0f;
\r
177 //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
\r
178 float right = (float) (nextStart + lastStart) / 2.0f;
\r
179 //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
\r
180 return new Result(resultString, null, new ResultPoint[]{new ResultPoint(left, (float) rowNumber), new ResultPoint(right, (float) rowNumber)}, BarcodeFormat.CODE_39);
\r
183 private static int[] findAsteriskPattern(BitArray row)
\r
185 int width = row.Size;
\r
187 while (rowOffset < width)
\r
189 if (row.get_Renamed(rowOffset))
\r
196 int counterPosition = 0;
\r
197 int[] counters = new int[9];
\r
198 int patternStart = rowOffset;
\r
199 bool isWhite = false;
\r
200 int patternLength = counters.Length;
\r
202 for (int i = rowOffset; i < width; i++)
\r
204 bool pixel = row.get_Renamed(i);
\r
205 if (pixel ^ isWhite)
\r
207 counters[counterPosition]++;
\r
211 if (counterPosition == patternLength - 1)
\r
213 if (toNarrowWidePattern(counters) == ASTERISK_ENCODING)
\r
215 // Look for whitespace before start pattern, >= 50% of width of start pattern
\r
216 if (row.isRange(System.Math.Max(0, patternStart - (i - patternStart) / 2), patternStart, false))
\r
218 return new int[]{patternStart, i};
\r
221 patternStart += counters[0] + counters[1];
\r
222 for (int y = 2; y < patternLength; y++)
\r
224 counters[y - 2] = counters[y];
\r
226 counters[patternLength - 2] = 0;
\r
227 counters[patternLength - 1] = 0;
\r
234 counters[counterPosition] = 1;
\r
235 isWhite = !isWhite;
\r
238 throw ReaderException.Instance;
\r
241 // For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions
\r
242 // per image when using some of our blackbox images.
\r
243 private static int toNarrowWidePattern(int[] counters)
\r
245 int numCounters = counters.Length;
\r
246 int maxNarrowCounter = 0;
\r
250 int minCounter = System.Int32.MaxValue;
\r
251 for (int i = 0; i < numCounters; i++)
\r
253 int counter = counters[i];
\r
254 if (counter < minCounter && counter > maxNarrowCounter)
\r
256 minCounter = counter;
\r
259 maxNarrowCounter = minCounter;
\r
261 int totalWideCountersWidth = 0;
\r
263 for (int i = 0; i < numCounters; i++)
\r
265 int counter = counters[i];
\r
266 if (counters[i] > maxNarrowCounter)
\r
268 pattern |= 1 << (numCounters - 1 - i);
\r
270 totalWideCountersWidth += counter;
\r
273 if (wideCounters == 3)
\r
275 // Found 3 wide counters, but are they close enough in width?
\r
276 // We can perform a cheap, conservative check to see if any individual
\r
277 // counter is more than 1.5 times the average:
\r
278 for (int i = 0; i < numCounters && wideCounters > 0; i++)
\r
280 int counter = counters[i];
\r
281 if (counters[i] > maxNarrowCounter)
\r
284 // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
\r
285 if ((counter << 1) >= totalWideCountersWidth)
\r
294 while (wideCounters > 3);
\r
298 private static char patternToChar(int pattern)
\r
300 for (int i = 0; i < CHARACTER_ENCODINGS.Length; i++)
\r
302 if (CHARACTER_ENCODINGS[i] == pattern)
\r
304 return ALPHABET[i];
\r
307 throw ReaderException.Instance;
\r
310 private static System.String decodeExtended(System.String encoded)
\r
312 int length = encoded.Length;
\r
313 System.Text.StringBuilder decoded = new System.Text.StringBuilder(length);
\r
314 for (int i = 0; i < length; i++)
\r
316 char c = encoded[i];
\r
317 if (c == '+' || c == '$' || c == '%' || c == '/')
\r
319 char next = encoded[i + 1];
\r
320 char decodedChar = '\x0000';
\r
325 // +A to +Z map to a to z
\r
326 if (next >= 'A' && next <= 'Z')
\r
328 decodedChar = (char) (next + 32);
\r
332 throw ReaderException.Instance;
\r
337 // $A to $Z map to control codes SH to SB
\r
338 if (next >= 'A' && next <= 'Z')
\r
340 decodedChar = (char) (next - 64);
\r
344 throw ReaderException.Instance;
\r
349 // %A to %E map to control codes ESC to US
\r
350 if (next >= 'A' && next <= 'E')
\r
352 decodedChar = (char) (next - 38);
\r
354 else if (next >= 'F' && next <= 'W')
\r
356 decodedChar = (char) (next - 11);
\r
360 throw ReaderException.Instance;
\r
365 // /A to /O map to ! to , and /Z maps to :
\r
366 if (next >= 'A' && next <= 'O')
\r
368 decodedChar = (char) (next - 32);
\r
370 else if (next == 'Z')
\r
376 throw ReaderException.Instance;
\r
380 decoded.Append(decodedChar);
\r
381 // bump up i again since we read two characters
\r
389 return decoded.ToString();
\r