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
18 using com.google.zxing.common;
\r
21 public sealed class Code39Reader : AbstractOneDReader
\r
23 private static String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
\r
24 private static char[] ALPHABET = ALPHABET_STRING.ToCharArray();
\r
27 * These represent the encodings of characters, as patterns of wide and narrow bars.
\r
28 * The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
\r
29 * with 1s representing "wide" and 0s representing narrow.
\r
31 private static int[] CHARACTER_ENCODINGS = {
\r
32 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9
\r
33 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, // A-J
\r
34 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, // K-T
\r
35 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, // U-*
\r
36 0x0A8, 0x0A2, 0x08A, 0x02A // $-%
\r
39 private static int ASTERISK_ENCODING = CHARACTER_ENCODINGS[39];
\r
41 private bool usingCheckDigit;
\r
42 private bool extendedMode;
\r
45 * Creates a reader that assumes all encoded data is data, and does not treat the
\r
46 * character as a check digit. It will not decoded "extended Code 39" sequences.
\r
48 public Code39Reader() {
\r
49 usingCheckDigit = false;
\r
50 extendedMode = false;
\r
54 * Creates a reader that can be configured to check the last character as a check digit.
\r
55 * It will not decoded "extended Code 39" sequences.
\r
57 * @param usingCheckDigit if true, treat the last data character as a check digit, not
\r
58 * data, and verify that the checksum passes.
\r
60 public Code39Reader(bool usingCheckDigit) {
\r
61 this.usingCheckDigit = usingCheckDigit;
\r
62 this.extendedMode = false;
\r
66 * Creates a reader that can be configured to check the last character as a check digit,
\r
67 * or optionally attempt to decode "extended Code 39" sequences that are used to encode
\r
68 * the full ASCII character set.
\r
70 * @param usingCheckDigit if true, treat the last data character as a check digit, not
\r
71 * data, and verify that the checksum passes.
\r
72 * @param extendedMode if true, will attempt to decode extended Code 39 sequences in the
\r
75 public Code39Reader(bool usingCheckDigit, bool extendedMode) {
\r
76 this.usingCheckDigit = usingCheckDigit;
\r
77 this.extendedMode = extendedMode;
\r
80 public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints) {
\r
82 int[] start = findAsteriskPattern(row);
\r
83 int nextStart = start[1];
\r
84 int end = row.getSize();
\r
86 // Read off white space
\r
87 while (nextStart < end && !row.get(nextStart)) {
\r
91 StringBuilder result = new StringBuilder();
\r
92 int[] counters = new int[9];
\r
96 recordPattern(row, nextStart, counters);
\r
97 int pattern = toNarrowWidePattern(counters);
\r
98 decodedChar = patternToChar(pattern);
\r
99 result.Append(decodedChar);
\r
100 lastStart = nextStart;
\r
101 for (int i = 0; i < counters.Length; i++) {
\r
102 nextStart += counters[i];
\r
104 // Read off white space
\r
105 while (nextStart < end && !row.get(nextStart)) {
\r
108 } while (decodedChar != '*');
\r
110 result.Remove(result.Length - 1, 1); // remove asterisk
\r
112 // Look for whitespace after pattern:
\r
113 int lastPatternSize = 0;
\r
114 for (int i = 0; i < counters.Length; i++) {
\r
115 lastPatternSize += counters[i];
\r
117 int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
\r
118 // If 50% of last pattern size, following last pattern, is not whitespace, fail
\r
119 // (but if it's whitespace to the very end of the image, that's OK)
\r
120 if (nextStart != end && whiteSpaceAfterEnd / 2 < lastPatternSize) {
\r
121 throw new ReaderException();
\r
124 if (usingCheckDigit) {
\r
125 int max = result.Length - 1;
\r
127 for (int i = 0; i < max; i++) {
\r
128 total += ALPHABET_STRING.IndexOf(result[i]);
\r
130 if (total % 43 != ALPHABET_STRING.IndexOf(result[max]))
\r
132 throw new ReaderException();
\r
134 result.Remove(max,1);
\r
137 String resultString = result.ToString();
\r
138 if (extendedMode) {
\r
139 resultString = decodeExtended(resultString);
\r
142 if (resultString.Length == 0) {
\r
143 // Almost surely a false positive
\r
144 throw new ReaderException();
\r
147 float left = (float) (start[1] + start[0]) / 2.0f;
\r
148 float right = (float) (nextStart + lastStart) / 2.0f;
\r
153 new GenericResultPoint(left, (float) rowNumber),
\r
154 new GenericResultPoint(right, (float) rowNumber)},
\r
155 BarcodeFormat.CODE_39);
\r
159 private static int[] findAsteriskPattern(BitArray row) {
\r
160 int width = row.getSize();
\r
162 while (rowOffset < width) {
\r
163 if (row.get(rowOffset)) {
\r
169 int counterPosition = 0;
\r
170 int[] counters = new int[9];
\r
171 int patternStart = rowOffset;
\r
172 bool isWhite = false;
\r
173 int patternLength = counters.Length;
\r
175 for (int i = rowOffset; i < width; i++) {
\r
176 bool pixel = row.get(i);
\r
177 if ((!pixel && isWhite) || (pixel && !isWhite)) {
\r
178 counters[counterPosition]++;
\r
180 if (counterPosition == patternLength - 1) {
\r
182 if (toNarrowWidePattern(counters) == ASTERISK_ENCODING) {
\r
183 // Look for whitespace before start pattern, >= 50% of width of start pattern
\r
184 if (row.isRange(Math.Max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
\r
185 return new int[]{patternStart, i};
\r
188 } catch (ReaderException re) {
\r
189 // no match, continue
\r
191 patternStart += counters[0] + counters[1];
\r
192 for (int y = 2; y < patternLength; y++) {
\r
193 counters[y - 2] = counters[y];
\r
195 counters[patternLength - 2] = 0;
\r
196 counters[patternLength - 1] = 0;
\r
201 counters[counterPosition] = 1;
\r
202 isWhite = !isWhite;
\r
205 throw new ReaderException();
\r
208 private static int toNarrowWidePattern(int[] counters) {
\r
209 int numCounters = counters.Length;
\r
210 int maxNarrowCounter = 0;
\r
213 int minCounter = int.MaxValue;
\r
214 for (int i = 0; i < numCounters; i++) {
\r
215 int counter = counters[i];
\r
216 if (counter < minCounter && counter > maxNarrowCounter) {
\r
217 minCounter = counter;
\r
220 maxNarrowCounter = minCounter;
\r
222 int totalWideCountersWidth = 0;
\r
224 for (int i = 0; i < numCounters; i++) {
\r
225 int counter = counters[i];
\r
226 if (counters[i] > maxNarrowCounter) {
\r
227 pattern |= 1 << (numCounters - 1 - i);
\r
229 totalWideCountersWidth += counter;
\r
232 if (wideCounters == 3) {
\r
233 // Found 3 wide counters, but are they close enough in width?
\r
234 // We can perform a cheap, conservative check to see if any individual
\r
235 // counter is more than 1.5 times the average:
\r
236 for (int i = 0; i < numCounters && wideCounters > 0; i++) {
\r
237 int counter = counters[i];
\r
238 if (counters[i] > maxNarrowCounter) {
\r
240 // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
\r
241 if ((counter << 1) >= totalWideCountersWidth) {
\r
242 throw new ReaderException();
\r
248 } while (wideCounters > 3);
\r
249 throw new ReaderException();
\r
252 private static char patternToChar(int pattern) {
\r
253 for (int i = 0; i < CHARACTER_ENCODINGS.Length; i++) {
\r
254 if (CHARACTER_ENCODINGS[i] == pattern) {
\r
255 return ALPHABET[i];
\r
258 throw new ReaderException();
\r
261 private static String decodeExtended(String encoded) {
\r
262 int Length = encoded.Length;
\r
263 StringBuilder decoded = new StringBuilder(Length);
\r
264 for (int i = 0; i < Length; i++) {
\r
265 char c = encoded[i];
\r
266 if (c == '+' || c == '$' || c == '%' || c == '/') {
\r
267 char next = encoded[i + 1];
\r
268 char decodedChar = '\0';
\r
271 // +A to +Z map to a to z
\r
272 if (next >= 'A' && next <= 'Z') {
\r
273 decodedChar = (char) (next + 32);
\r
275 throw new ReaderException();
\r
279 // $A to $Z map to control codes SH to SB
\r
280 if (next >= 'A' && next <= 'Z') {
\r
281 decodedChar = (char) (next - 64);
\r
283 throw new ReaderException();
\r
287 // %A to %E map to control codes ESC to US
\r
288 if (next >= 'A' && next <= 'E') {
\r
289 decodedChar = (char) (next - 38);
\r
290 } else if (next >= 'F' && next <= 'W') {
\r
291 decodedChar = (char) (next - 11);
\r
293 throw new ReaderException();
\r
297 // /A to /O map to ! to , and /Z maps to :
\r
298 if (next >= 'A' && next <= 'O') {
\r
299 decodedChar = (char) (next - 32);
\r
300 } else if (next == 'Z') {
\r
303 throw new ReaderException();
\r
307 decoded.Append(decodedChar);
\r
308 // bump up i again since we read two characters
\r
314 return decoded.ToString();
\r