Style changes mostly

git-svn-id: https://zxing.googlecode.com/svn/trunk@985 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-06-24 13:19:53 +00:00
parent 372dfa1145
commit 969088fa82
6 changed files with 2353 additions and 2481 deletions

View file

@ -1,3 +1,19 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.pdf417;
import com.google.zxing.BarcodeFormat;
@ -21,6 +37,7 @@ import java.util.Hashtable;
* @author SITA Lab (kevin.osullivan@sita.aero)
*/
public final class PDF417Reader implements Reader {
private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
private final Decoder decoder = new Decoder();
@ -48,8 +65,7 @@ public final class PDF417Reader implements Reader {
decoderResult = decoder.decode(detectorResult.getBits());
points = detectorResult.getPoints();
}
Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.PDF417);
return result;
return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.PDF417);
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright 2007 ZXing authors
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.pdf417.decoder;
import com.google.zxing.ReaderException;
@ -26,7 +27,7 @@ import com.google.zxing.common.BitMatrix;
* @author SITA Lab (kevin.osullivan@sita.aero)
*/
final class BitMatrixParser {
public static final boolean debug = false;
private static final int MAX_ROW_DIFFERENCE = 6;
private static final int MAX_ROWS = 90;
private static final int MAX_COLUMNS = 30;
@ -35,14 +36,14 @@ final class BitMatrixParser {
private static final int MODULES_IN_SYMBOL = 17;
private final BitMatrix bitMatrix;
int rows = 0;
int columns = 0;
private int rows = 0;
private int columns = 0;
int leftColumnECData = 0;
int rightColumnECData = 0;
int eraseCount = 0;
int erasures[] = null;
int ecLevel = -1;
private int leftColumnECData = 0;
private int rightColumnECData = 0;
private int eraseCount = 0;
private int[] erasures = null;
private int ecLevel = -1;
BitMatrixParser(BitMatrix bitMatrix) {
this.bitMatrix = bitMatrix;
@ -62,20 +63,19 @@ final class BitMatrixParser {
// TODO should be a rectangular matrix
int height = width;
int rowHeight = 0;
int rowNumber = 0;
boolean rowInProgress = false;
int matchingConsecutiveScans = 0;
int next = 0;
int codewords[] = new int[MAX_CW_CAPACITY];
erasures = new int[MAX_CW_CAPACITY];
// Get the number of pixels in a module across the X dimension
//float moduleWidth = bitMatrix.getModuleWidth();
float moduleWidth = 1.0f; // Image has been sampled and reduced
int rowCounters[] = new int[width];
int[] rowCounters = new int[width];
int[] codewords = new int[MAX_CW_CAPACITY];
int next = 0;
int matchingConsecutiveScans = 0;
boolean rowInProgress = false;
int rowNumber = 0;
int rowHeight = 0;
for (int i = 1; i < height; i++) {
if (rowNumber >= MAX_ROWS) {
// Something is wrong, since we have exceeded
@ -113,9 +113,6 @@ final class BitMatrixParser {
} else {
if (rowInProgress) {
// Process Row
if (debug)
System.out.println("Row Height=" + rowHeight + " Row Number="
+ rowNumber);
next = processRow(rowCounters, rowNumber, rowHeight,
moduleWidth, codewords, next);
if (next == -1) {
@ -139,9 +136,6 @@ final class BitMatrixParser {
// Check for a row that was in progress before we exited above.
if (rowInProgress) {
// Process Row
if (debug)
System.out.println("Row Height=" + rowHeight + " Row Number="
+ rowNumber);
if (rowNumber >= MAX_ROWS) {
// Something is wrong, since we have exceeded
// the maximum rows in the specification.
@ -153,8 +147,6 @@ final class BitMatrixParser {
rowNumber++;
rows = rowNumber;
}
if (debug)
System.out.println("Errors=" + eraseCount);
erasures = trimArray(erasures, eraseCount);
return trimArray(codewords, next);
}
@ -162,13 +154,11 @@ final class BitMatrixParser {
/**
* Trim the array to the required size.
*
* @param array
* the array
* @param size
* the size to trim it to
* @param array the array
* @param size the size to trim it to
* @return the new trimmed array
*/
private int[] trimArray(int array[], int size) {
private static int[] trimArray(int[] array, int size) {
if (size > 0) {
int a[] = new int[size];
for (int i = 0; i < size; i++) {
@ -183,19 +173,13 @@ final class BitMatrixParser {
/**
* Convert the symbols in the row to codewords.
*
* @param rowCounters
* an array containing the counts of black pixels for each column
* @param rowCounters an array containing the counts of black pixels for each column
* in the row.
* @param rowNumber
* the current row number of codewords.
* @param rowHeight
* the height of this row in pixels.
* @param moduleWidth
* the size of a module in pixels.
* @param codewords
* the codeword array to save codewords into.
* @param next
* the next available index into the codewords array.
* @param rowNumber the current row number of codewords.
* @param rowHeight the height of this row in pixels.
* @param moduleWidth the size of a module in pixels.
* @param codewords the codeword array to save codewords into.
* @param next the next available index into the codewords array.
* @return the next available index into the codeword array after processing
* this row.
* @throws ReaderException
@ -205,8 +189,8 @@ final class BitMatrixParser {
* elements, each of which can be one to six modules wide. The four bar and
* four space elements shall measure 17 modules in total.
*/
int processRow(int rowCounters[], int rowNumber, int rowHeight,
float moduleWidth, int codewords[], int next) throws ReaderException {
int processRow(int[] rowCounters, int rowNumber, int rowHeight,
float moduleWidth, int[] codewords, int next) throws ReaderException {
int width = bitMatrix.getDimension();
int columnNumber = 0;
long symbol = 0;
@ -277,8 +261,7 @@ final class BitMatrixParser {
/**
* Build a symbol from the pixels
*
* @param counters
* array of pixel counter corresponding to each Bar/Space pattern.
* @param counters array of pixel counter corresponding to each Bar/Space pattern.
* @param rowNumber
* @return the symbol
*/
@ -287,17 +270,15 @@ final class BitMatrixParser {
* represents the module widths of the eight elements of that symbol
* character.
*/
private long getSymbol(int counters[], int rowNumber, float moduleWidth) {
int symbol = 0;
private static long getSymbol(int[] counters, int rowNumber, float moduleWidth) {
int pixelsInSymbol = 0;
for (int j = 0; j < counters.length; j++) {
pixelsInSymbol += counters[j];
}
float avgModuleWidth = (pixelsInSymbol / 17.0f);
if (debug)
System.out.println("Average Module Width = " + avgModuleWidth);
boolean toggle = true;
int shift = 0;
int symbol = 0;
for (int j = 0; j < counters.length; j++) {
if (counters[j] < moduleWidth && counters[j] > 0) {
// Give a very narrow bar/space a chance
@ -335,18 +316,16 @@ final class BitMatrixParser {
* @throws ReaderException
*/
private int getCodeword(long symbol, int row) throws ReaderException {
long cw = 0;
long sym = symbol;
sym &= 0x3ffff;
int i = findCodewordIndex(sym);
if (i != -1) {
cw = codewordTable[i] - 1;
cw = cw % 929;
} else {
if (debug)
System.out.println("UNABLE TO FIND " + symbol); // Long.toBinaryString(symbol));
long cw = 0;
if (i == -1) {
return -1;
} else {
cw = CODEWORD_TABLE[i] - 1;
cw %= 929;
}
return (int) cw;
}
@ -355,18 +334,17 @@ final class BitMatrixParser {
* Use a binary search to find the index of the codeword corresponding to
* this symbol.
*
* @param symbol
* the symbol from the barcode.
* @param symbol the symbol from the barcode.
* @return the index into the codeword table.
*/
private int findCodewordIndex(long symbol) throws ReaderException {
private int findCodewordIndex(long symbol) {
int first = 0;
int upto = symbolTable.length;
int upto = SYMBOL_TABLE.length;
while (first < upto) {
int mid = (first + upto) / 2; // Compute mid point.
if (symbol < symbolTable[mid]) {
if (symbol < SYMBOL_TABLE[mid]) {
upto = mid; // repeat search in bottom half.
} else if (symbol > symbolTable[mid]) {
} else if (symbol > SYMBOL_TABLE[mid]) {
first = mid + 1; // Repeat search in top half.
} else {
return mid; // Found it. return position
@ -405,33 +383,24 @@ final class BitMatrixParser {
/**
* Convert the symbols in the row to codewords.
* Each PDF417 symbol character consists of four bar elements and four space
* elements, each of which can be one to six modules wide. The four bar and
* four space elements shall measure 17 modules in total.
*
* @param rowCounters
* an array containing the counts of black pixels for each column
* @param rowCounters an array containing the counts of black pixels for each column
* in the row.
* @param rowNumber
* the current row number of codewords.
* @param rowHeight
* the height of this row in pixels.
* @param moduleWidth
* the size of a module in pixels.
* @param codewords
* the codeword array to save codewords into.
* @param next
* the next available index into the codewords array.
* @param rowNumber the current row number of codewords.
* @param rowHeight the height of this row in pixels.
* @param moduleWidth the size of a module in pixels.
* @param codewords the codeword array to save codewords into.
* @param next the next available index into the codewords array.
* @return the next available index into the codeword array after processing
* this row.
* @throws ReaderException
*/
/*
* Each PDF417 symbol character consists of four bar elements and four space
* elements, each of which can be one to six modules wide. The four bar and
* four space elements shall measure 17 modules in total.
*/
int processRow1(int rowCounters[], int rowNumber, int rowHeight,
float moduleWidth, int codewords[], int next) throws ReaderException {
int processRow1(int[] rowCounters, int rowNumber, int rowHeight,
float moduleWidth, int[] codewords, int next) throws ReaderException {
int width = bitMatrix.getDimension();
int columnNumber = 0;
int firstBlack = 0;
for (firstBlack = 0; firstBlack < width; firstBlack++) {
@ -441,9 +410,10 @@ final class BitMatrixParser {
}
}
int counters[] = new int[8];
int[] counters = new int[8];
int state = 0; // In black pixels, looking for white, first or second time
long symbol = 0;
int columnNumber = 0;
for (int i = firstBlack; i < width; i++) {
if (state == 1 || state == 3 || state == 5 || state == 7) { // In white
// pixels,
@ -508,10 +478,6 @@ final class BitMatrixParser {
// Left row indicator column
symbol = getSymbol(counters, rowNumber, moduleWidth);
int cw = getCodeword(symbol, rowNumber);
if (debug) {
System.out.println("Left Column CW=" + cw + " Cluster="
+ rowNumber % 3);
}
if (ecLevel < 0) {
switch (rowNumber % 3) {
case 0:
@ -564,9 +530,6 @@ final class BitMatrixParser {
}
}
codewords[next] = 0;
if (debug)
System.out.println("Right Column CW=" + codewords[next]
+ " Cluster=" + rowNumber % 3);
}
return next;
}
@ -576,7 +539,7 @@ final class BitMatrixParser {
* specification. The index of a symbol in this table corresponds to the
* index into the codeword table.
*/
private static final int symbolTable[] = { 0x1025e, 0x1027a, 0x1029e,
private static final int[] SYMBOL_TABLE = {0x1025e, 0x1027a, 0x1029e,
0x102bc, 0x102f2, 0x102f4, 0x1032e, 0x1034e, 0x1035c, 0x10396,
0x103a6, 0x103ac, 0x10422, 0x10428, 0x10436, 0x10442, 0x10444,
0x10448, 0x10450, 0x1045e, 0x10466, 0x1046c, 0x1047a, 0x10482,
@ -979,7 +942,7 @@ final class BitMatrixParser {
/**
* This table contains to codewords for all symbols.
*/
private static final int codewordTable[] = { 2627, 1819, 2622, 2621, 1813,
private static final int[] CODEWORD_TABLE = {2627, 1819, 2622, 2621, 1813,
1812, 2729, 2724, 2723, 2779, 2774, 2773, 902, 896, 908, 868, 865,
861, 859, 2511, 873, 871, 1780, 835, 2493, 825, 2491, 842, 837, 844,
1764, 1762, 811, 810, 809, 2483, 807, 2482, 806, 2480, 815, 814, 813,

View file

@ -1,5 +1,5 @@
/*
* Copyright 2007 ZXing authors
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.pdf417.decoder;
import com.google.zxing.ReaderException;
@ -24,7 +25,6 @@ import com.google.zxing.common.DecoderResult;
* @author SITA Lab (kevin.osullivan@sita.aero)
*/
final class DecodedBitStreamParser {
public static final boolean debug = true;
private static final int TEXT_COMPACTION_MODE_LATCH = 900;
private static final int BYTE_COMPACTION_MODE_LATCH = 901;
@ -50,17 +50,17 @@ final class DecodedBitStreamParser {
private static final int PS = 29;
private static final int PAL = 29;
private static final char punct_chars[] = {';', '<', '>', '@', '[', 92, '}', '_', 96, '~', '!',
private static final char[] PUNCT_CHARS = {';', '<', '>', '@', '[', 92, '}', '_', 96, '~', '!',
13, 9, ',', ':', 10, '-', '.', '$', '/', 34, '|', '*',
'(', ')', '?', '{', '}', 39};
private static final char mixed_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '&',
private static final char[] MIXED_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '&',
13, 9, ',', ':', '#', '-', '.', '$', '/', '+', '%', '*',
'=', '^'};
// Table containing values for the exponent of 900.
// This is used in the numeric compaction decode algorithm.
private static final String exp900[] =
private static final String[] EXP900 =
{ "000000000000000000000000000000000000000000001",
"000000000000000000000000000000000000000000900",
"000000000000000000000000000000000000000810000",
@ -81,7 +81,7 @@ final class DecodedBitStreamParser {
private DecodedBitStreamParser() {
}
static DecoderResult decode(int codewords[]) throws ReaderException {
static DecoderResult decode(int[] codewords) throws ReaderException {
StringBuffer result = new StringBuffer(100);
// Get compaction mode
int codeIndex = 1;
@ -123,7 +123,6 @@ final class DecodedBitStreamParser {
throw ReaderException.getInstance();
}
}
if (debug) System.out.println(result);
return new DecoderResult(null, result.toString(), null);
}
@ -137,11 +136,11 @@ final class DecodedBitStreamParser {
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
private static int textCompaction(int codewords[], int codeIndex, StringBuffer result) {
private static int textCompaction(int[] codewords, int codeIndex, StringBuffer result) {
// 2 character per codeword
int textCompactionData[] = new int[codewords[0] * 2];
int[] textCompactionData = new int[codewords[0] * 2];
// Used to hold the byte compaction value if there is a mode shift
int byteCompactionData[] = new int[codewords[0] * 2];
int[] byteCompactionData = new int[codewords[0] * 2];
int index = 0;
int code = 0;
@ -209,8 +208,8 @@ final class DecodedBitStreamParser {
* @param length The size of the text compaction and byte compaction data.
* @param result The decoded data is appended to the result.
*/
private static void decodeTextCompaction(int textCompactionData[],
int byteCompactionData[],
private static void decodeTextCompaction(int[] textCompactionData,
int[] byteCompactionData,
int length,
StringBuffer result) {
// Beginning from an initial state of the Alpha sub-mode
@ -270,7 +269,7 @@ final class DecodedBitStreamParser {
case MIXED:
// Mixed (numeric and some punctuation)
if (subModeCh < PL) {
ch = mixed_chars[subModeCh];
ch = MIXED_CHARS[subModeCh];
} else {
if (subModeCh == PL) {
subMode = PUNCT;
@ -293,7 +292,7 @@ final class DecodedBitStreamParser {
case PUNCT:
// Punctuation
if (subModeCh < PS) {
ch = punct_chars[subModeCh];
ch = PUNCT_CHARS[subModeCh];
} else {
if (subModeCh == PAL) {
subMode = ALPHA;
@ -307,7 +306,7 @@ final class DecodedBitStreamParser {
// Restore sub-mode
subMode = priorToShiftMode;
if (subModeCh < PS) {
ch = punct_chars[subModeCh];
ch = PUNCT_CHARS[subModeCh];
} else {
if (subModeCh == PAL) {
subMode = ALPHA;
@ -327,20 +326,21 @@ final class DecodedBitStreamParser {
* Byte Compaction mode (see 5.4.3) permits all 256 possible 8-bit byte values to be encoded.
* This includes all ASCII characters value 0 to 127 inclusive and provides for international
* character set support.
*
* @param mode The byte compaction mode i.e. 901 or 924
* @param codewords The array of codewords (data + error)
* @param codeIndex The current index into the codeword array.
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
private static int byteCompaction(int mode, int codewords[], int codeIndex, StringBuffer result) {
private static int byteCompaction(int mode, int[] codewords, int codeIndex, StringBuffer result) {
if (mode == BYTE_COMPACTION_MODE_LATCH) {
// Total number of Byte Compaction characters to be encoded
// is not a multiple of 6
int count = 0;
long value = 0;
char decodedData[] = new char[6];
int byteCompactedCodewords[] = new int[6];
char[] decodedData = new char[6];
int[] byteCompactedCodewords = new int[6];
int code = 0;
boolean end = false;
while ((codeIndex < codewords[0]) && !end) {
@ -390,7 +390,6 @@ final class DecodedBitStreamParser {
boolean end = false;
while ((codeIndex < codewords[0]) && !end) {
code = codewords[codeIndex++];
char decodedData[] = new char[6];
if (code < TEXT_COMPACTION_MODE_LATCH) {
count += 1;
// Base 900
@ -411,6 +410,7 @@ final class DecodedBitStreamParser {
if ((count % 5 == 0) && (count > 0)) {
// Decode every 5 codewords
// Convert to Base 256
char[] decodedData = new char[6];
for (int j = 0; j < 6; ++j) {
decodedData[5 - j] = (char) (value % 256);
value >>= 8;
@ -430,15 +430,14 @@ final class DecodedBitStreamParser {
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
private static int numericCompaction(int codewords[], int codeIndex, StringBuffer result) {
int code;
private static int numericCompaction(int[] codewords, int codeIndex, StringBuffer result) {
int count = 0;
boolean end = false;
int numericCodewords[] = new int[MAX_NUMERIC_CODEWORDS];
int[] numericCodewords = new int[MAX_NUMERIC_CODEWORDS];
while ((codeIndex < codewords.length) && !end) {
code = codewords[codeIndex++];
int code = codewords[codeIndex++];
if (code < TEXT_COMPACTION_MODE_LATCH) {
numericCodewords[count] = code;
count++;
@ -469,6 +468,7 @@ final class DecodedBitStreamParser {
/**
* Convert a list of Numeric Compacted codewords from Base 900 to Base 10.
*
* @param codewords The array of codewords
* @param count The number of codewords
* @return The decoded string representing the Numeric data.
@ -513,11 +513,11 @@ final class DecodedBitStreamParser {
tokens for the numbers.
BigDecimal is not supported by J2ME.
*/
private static String decodeBase900toBase10(int codewords[], int count) {
private static String decodeBase900toBase10(int[] codewords, int count) {
StringBuffer accum = null;
StringBuffer value = null;
for (int i = 0; i < count; i++) {
value = multiply(exp900[count - i - 1], codewords[i]);
value = multiply(EXP900[count - i - 1], codewords[i]);
if (accum == null) {
// First time in accum=0
accum = value;
@ -539,12 +539,12 @@ final class DecodedBitStreamParser {
// No leading 1 => just write the converted number.
result = accum.toString();
}
if (debug) System.out.println("Big Integer=" + result);
return result;
}
/**
* Multiplies two String numbers
*
* @param value1 Any number represented as a string.
* @param value2 A number <= 999.
* @return the result of value1 * value2.
@ -564,7 +564,7 @@ final class DecodedBitStreamParser {
}
// Multiply by tens
for (int j = 0; j < tens; j++) {
result = add(result.toString(), (value1 + "0").substring(1));
result = add(result.toString(), (value1 + '0').substring(1));
}
// Multiply by hundreds
for (int j = 0; j < hundreds; j++) {
@ -575,6 +575,7 @@ final class DecodedBitStreamParser {
/**
* Add two numbers which are represented as strings.
*
* @param value1
* @param value2
* @return the result of value1 + value2

View file

@ -1,5 +1,5 @@
/*
* Copyright 2007 ZXing authors
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.pdf417.decoder;
import com.google.zxing.ReaderException;
@ -27,7 +28,7 @@ import com.google.zxing.common.DecoderResult;
* @author SITA Lab (kevin.osullivan@sita.aero)
*/
public final class Decoder {
public static final boolean debug = false;
private static final int MAX_ERRORS = 3;
private static final int MAX_EC_CODEWORDS = 512;
//private final ReedSolomonDecoder rsDecoder;
@ -68,70 +69,48 @@ public final class Decoder {
*/
public DecoderResult decode(BitMatrix bits) throws ReaderException {
// Construct a parser to read the data codewords and error-correction level
long t2;
long t1 = System.currentTimeMillis();
BitMatrixParser parser = new BitMatrixParser(bits);
int[] codewords = parser.readCodewords();
if (codewords == null || codewords.length == 0) {
if (debug) System.out.println("No codewords read");
throw ReaderException.getInstance();
}
if (debug) {
t2 = System.currentTimeMillis();
System.out.println("Elapsed time in ms - BitMatrixParser " +(t2-t1));
}
int ecLevel = parser.getECLevel();
int numECCodewords = 1 << (ecLevel + 1);
int erasures[] = parser.getErasures();
int[] erasures = parser.getErasures();
t1 = System.currentTimeMillis();
correctErrors(codewords, erasures, numECCodewords);
if (debug) {
t2 = System.currentTimeMillis();
System.out.println("Elapsed time in ms - Reed Solomon " +(t2-t1));
}
verifyCodewordCount(codewords, numECCodewords);
t1 = System.currentTimeMillis();
DecoderResult dr = DecodedBitStreamParser.decode(codewords);
if (debug) {
t2 = System.currentTimeMillis();
System.out.println("Elapsed time in ms - DecodeBitStreamParser " +(t2-t1));
}
// Decode the codewords
return dr;
return DecodedBitStreamParser.decode(codewords);
}
/**
* Verify that all is OK with the codeword array.
*
* @param codewords
* @return an index to the first data codeword.
* @throws ReaderException
*/
private int verifyCodewordCount(int codewords[], int numECCodewords) throws ReaderException {
int numberOfCodewords = 0;
private static int verifyCodewordCount(int[] codewords, int numECCodewords) throws ReaderException {
if (codewords.length < 4) {
// Codeword array size should be at least 4 allowing for
// Count CW, At least one Data CW, Error Correction CW, Error Correction CW
if (debug) System.out.println("Not enough codewords " + codewords.length);
throw ReaderException.getInstance();
}
// The first codeword, the Symbol Length Descriptor, shall always encode the total number of data
// codewords in the symbol, including the Symbol Length Descriptor itself, data codewords and pad
// codewords, but excluding the number of error correction codewords.
numberOfCodewords = codewords[0];
int numberOfCodewords = codewords[0];
if (numberOfCodewords > codewords.length) {
if (debug) System.out.println("Invalid number of codewords[0]=" + numberOfCodewords + " codewords.length=" + codewords.length);
throw ReaderException.getInstance();
}
if (numberOfCodewords == 0) {
// Reset to the length of the array - 8 (Allow for at least level 3 Error Correction (8 Error Codewords)
if (numECCodewords < codewords.length) {
codewords[0] = codewords.length - numECCodewords;
if (debug) System.out.println("Codewords[0] is zero, resetting to " +(codewords[0]));
} else {
if (debug) System.out.println("Codewords[0] is zero, no data codewords");
throw ReaderException.getInstance();
}
}
@ -142,11 +121,10 @@ public final class Decoder {
* <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
* correct the errors in-place using Reed-Solomon error correction.</p>
*
* @param codewordBytes data and error correction codewords
* @param numDataCodewords number of codewords that are data bytes
* @param codewords data and error correction codewords
* @throws ReaderException if error correction fails
*/
private int correctErrors(int[] codewords, int[] erasures, int numECCodewords) throws ReaderException {
private static int correctErrors(int[] codewords, int[] erasures, int numECCodewords) throws ReaderException {
if ((erasures != null && erasures.length > numECCodewords / 2 + MAX_ERRORS) ||
(numECCodewords < 0 || numECCodewords > MAX_EC_CODEWORDS)) {
// Too many errors or EC Codewords is corrupted
@ -154,13 +132,10 @@ public final class Decoder {
}
// Try to correct the errors
int result = 0; // rsDecoder.correctErrors(codewords, numECCodewords);
if (debug) {
System.out.println("Corrected errors: " + result);
}
if (erasures != null) {
int numErasures = erasures.length;
if (result > 0) {
numErasures = numErasures - result;
numErasures -= result;
}
if (numErasures > MAX_ERRORS) {
// Still too many errors

View file

@ -1,5 +1,5 @@
/*
* Copyright 2007 ZXing authors
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package com.google.zxing.pdf417.detector;
import java.util.Hashtable;
import com.google.zxing.BlackPointEstimationMethod;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.ReaderException;
@ -27,6 +25,8 @@ import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.DetectorResult;
import com.google.zxing.common.GridSampler;
import java.util.Hashtable;
/**
* <p>
* Encapsulates logic that can detect a PDF417 Code in an image, even if the
@ -36,7 +36,7 @@ import com.google.zxing.common.GridSampler;
* @author SITA Lab (kevin.osullivan@sita.aero)
*/
public final class Detector {
public static final boolean debug = false;
public static final int MAX_AVG_VARIANCE = (int) ((1 << 8) * 0.42f);
public static final int MAX_INDIVIDUAL_VARIANCE = (int) ((1 << 8) * 0.8f);
// B S B S B S B S Bar/Space pattern
@ -63,8 +63,7 @@ public final class Detector {
*
* @return {@link DetectorResult} encapsulating results of detecting a PDF417
* Code
* @throws ReaderException
* if no QR Code can be found
* @throws ReaderException if no QR Code can be found
*/
public DetectorResult detect() throws ReaderException {
return detect(null);
@ -75,27 +74,17 @@ public final class Detector {
* Detects a PDF417 Code in an image, simply.
* </p>
*
* @param hints
* optional hints to detector
* @param hints optional hints to detector
* @return {@link DetectorResult} encapsulating results of detecting a PDF417
* Code
* @throws ReaderException
* if no PDF417 Code can be found
* @throws ReaderException if no PDF417 Code can be found
*/
public DetectorResult detect(Hashtable hints) throws ReaderException {
if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(image
.getLastEstimationMethod())) {
image.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
}
int width = image.getWidth();
int height = image.getHeight();
if (debug)
System.out.println("Width= " + width);
if (debug)
System.out.println("Height= " + height);
long t2;
long t1 = System.currentTimeMillis();
ResultPoint[] vertices = findVertices(image);
if (vertices == null) { // Couldn't find the vertices
// Maybe the image is rotated 180 degrees?
@ -110,43 +99,21 @@ public final class Detector {
*/
}
if (vertices != null) {
if (debug) {
t2 = System.currentTimeMillis();
System.out.println("Elapsed time in ms - Find Vertices "
+ (t2 - t1));
}
float moduleWidth = computeModuleWidth(vertices);
if (moduleWidth < 1.0f) {
if (debug)
System.out.println("Module Size is less than 1.0f="
+ moduleWidth);
throw ReaderException.getInstance();
}
if (debug)
System.out.println("Module Size=" + moduleWidth);
int dimension = computeDimension(vertices[4], vertices[6],
vertices[5], vertices[7], moduleWidth);
if (debug)
System.out.println("Dimension=" + dimension);
// Deskew and sample image
t1 = System.currentTimeMillis();
BitMatrix bits = sampleGrid(image, vertices[4], vertices[5],
vertices[6], vertices[7], dimension);
//bits.setModuleWidth(moduleWidth);
if (debug) {
t2 = System.currentTimeMillis();
System.out
.println("Elapsed time in ms - Sampled Grid " + (t2 - t1));
}
return new DetectorResult(bits, new ResultPoint[]{vertices[4],
vertices[5], vertices[6], vertices[7]});
} else {
if (debug) {
System.out.println("Unable to locate vertices");
}
throw ReaderException.getInstance();
}
}
@ -155,8 +122,7 @@ public final class Detector {
* Locate the vertices and the codewords area of a black blob using the Start
* and Stop patterns as locators.
*
* @param image
* the scanned barcode image.
* @param image the scanned barcode image.
* @return the an array containing the vertices. vertices[0] x, y top left
* barcode vertices[1] x, y bottom left barcode vertices[2] x, y top
* right barcode vertices[3] x, y bottom right barcode vertices[4] x,
@ -164,7 +130,7 @@ public final class Detector {
* area vertices[6] x, y top right codeword area vertices[7] x, y
* bottom right codeword area
*/
private ResultPoint[] findVertices(MonochromeBitmapSource image) {
private static ResultPoint[] findVertices(MonochromeBitmapSource image) {
int height = image.getHeight();
int width = image.getWidth();
@ -178,9 +144,6 @@ public final class Detector {
row = image.getBlackRow(i, null, 0, width / 4);
loc = findGuardPattern(row, 0, START_PATTERN);
if (loc != null) {
if (debug)
System.out.println("Found START pattern at X=" + loc[0]
+ " End X=" + loc[1] + ", Y=" + i);
result[0] = new ResultPoint(loc[0], i);
result[4] = new ResultPoint(loc[1], i);
found = true;
@ -194,9 +157,6 @@ public final class Detector {
row = image.getBlackRow(i, null, 0, width / 4);
loc = findGuardPattern(row, 0, START_PATTERN);
if (loc != null) {
if (debug)
System.out.println("Found START pattern at X=" + loc[0]
+ " End X=" + loc[1] + ", Y=" + i);
result[1] = new ResultPoint(loc[0], i);
result[5] = new ResultPoint(loc[1], i);
found = true;
@ -212,10 +172,6 @@ public final class Detector {
row.reverse();
loc = findGuardPattern(row, 0, STOP_PATTERN_REVERSE);
if (loc != null) {
if (debug)
System.out.println("Found STOP pattern at X="
+ (width - loc[0]) + " End X=" + (width - loc[1])
+ ", Y=" + i);
result[2] = new ResultPoint(width - loc[0], i);
result[6] = new ResultPoint(width - loc[1], i);
found = true;
@ -231,10 +187,6 @@ public final class Detector {
row.reverse();
loc = findGuardPattern(row, 0, STOP_PATTERN_REVERSE);
if (loc != null) {
if (debug)
System.out.println("Found STOP pattern at X="
+ (width - loc[0]) + " End X=" + (width - loc[1])
+ ", Y=" + i);
result[3] = new ResultPoint(width - loc[0], i);
result[7] = new ResultPoint(width - loc[1], i);
found = true;
@ -242,11 +194,7 @@ public final class Detector {
}
}
}
if (found) {
return result;
} else {
return null;
}
return found ? result : null;
}
/**
@ -255,8 +203,7 @@ public final class Detector {
* degrees and if it locates the start and stop patterns at it will re-map
* the vertices for a 0 degree rotation.
*
* @param image
* the scanned barcode image.
* @param image the scanned barcode image.
* @return the an array containing the vertices. vertices[0] x, y top left
* barcode vertices[1] x, y bottom left barcode vertices[2] x, y top
* right barcode vertices[3] x, y bottom right barcode vertices[4] x,
@ -264,7 +211,7 @@ public final class Detector {
* area vertices[6] x, y top right codeword area vertices[7] x, y
* bottom right codeword area
*/
private ResultPoint[] findVertices180(MonochromeBitmapSource image) {
private static ResultPoint[] findVertices180(MonochromeBitmapSource image) {
int height = image.getHeight();
int width = image.getWidth();
@ -279,10 +226,6 @@ public final class Detector {
row.reverse();
loc = findGuardPattern(row, 0, START_PATTERN);
if (loc != null) {
if (debug)
System.out.println("Found START pattern at X="
+ (width - loc[0]) + " End X=" + (width - loc[1]) + ", Y="
+ i);
result[0] = new ResultPoint(width - loc[0], i);
result[4] = new ResultPoint(width - loc[1], i);
found = true;
@ -297,10 +240,6 @@ public final class Detector {
row.reverse();
loc = findGuardPattern(row, 0, START_PATTERN);
if (loc != null) {
if (debug)
System.out.println("Found START pattern at X="
+ (width - loc[0]) + " End X=" + (width - loc[1])
+ ", Y=" + i);
result[1] = new ResultPoint(width - loc[0], i);
result[5] = new ResultPoint(width - loc[1], i);
found = true;
@ -315,9 +254,6 @@ public final class Detector {
row = image.getBlackRow(i, null, (width * 3) / 4, width / 4);
loc = findGuardPattern(row, 0, STOP_PATTERN_REVERSE);
if (loc != null) {
if (debug)
System.out.println("Found STOP pattern at X=" + loc[0]
+ " End X=" + loc[1] + ", Y=" + i);
result[2] = new ResultPoint(loc[0], i);
result[6] = new ResultPoint(loc[1], i);
found = true;
@ -332,9 +268,6 @@ public final class Detector {
row = image.getBlackRow(i, null, (width * 3) / 4, width / 4);
loc = findGuardPattern(row, 0, STOP_PATTERN_REVERSE);
if (loc != null) {
if (debug)
System.out.println("Found STOP pattern at X=" + loc[0]
+ " End X=" + loc[1] + ", Y=" + i);
result[3] = new ResultPoint(loc[0], i);
result[7] = new ResultPoint(loc[1], i);
found = true;
@ -352,19 +285,16 @@ public final class Detector {
/**
* <p>
* Estimates module size (pixels in a module) based on the Start and End
* finder patterns -- it uses
* {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)} to figure the width
* of each.
* finder patterns.</p>
*
* @param vertices
* [] vertices[0] x, y top left barcode vertices[1] x, y bottom
* @param vertices [] vertices[0] x, y top left barcode vertices[1] x, y bottom
* left barcode vertices[2] x, y top right barcode vertices[3] x, y
* bottom right barcode vertices[4] x, y top left Codeword area
* vertices[5] x, y bottom left Codeword area vertices[6] x, y top
* right Codeword area vertices[7] x, y bottom right Codeword area
* @return the module size.
*/
private float computeModuleWidth(ResultPoint[] vertices) {
private static float computeModuleWidth(ResultPoint[] vertices) {
float pixels1 = ResultPoint.distance(vertices[0], vertices[4]);
float pixels2 = ResultPoint.distance(vertices[1], vertices[5]);
float moduleWidth1 = (pixels1 + pixels2) / (17 * 2.0f);
@ -375,20 +305,14 @@ public final class Detector {
}
/**
*
* Computes the dimension (number of modules in a row) of the PDF417 Code
* based on vertices of the codeword area and estimated module size.
*
* @param topLeft
* of codeword area
* @param topRight
* of codeword area
* @param bottomLeft
* of codeword area
* @param bottomRight
* of codeword are
* @param moduleWidth
* estimated module size
* @param topLeft of codeword area
* @param topRight of codeword area
* @param bottomLeft of codeword area
* @param bottomRight of codeword are
* @param moduleWidth estimated module size
* @return the number of modules in a row.
*/
private static int computeDimension(ResultPoint topLeft,
@ -401,8 +325,7 @@ public final class Detector {
int bottomRowDimension = round(ResultPoint.distance(bottomLeft,
bottomRight)
/ moduleWidth);
int dimension = ((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17;
return dimension;
return ((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17;
/*
* int topRowDimension = round(ResultPoint.distance(topLeft,
* topRight)); //moduleWidth); int bottomRowDimension =
@ -450,17 +373,14 @@ public final class Detector {
}
/**
* @param row
* row of black/white values to search
* @param rowOffset
* position to start search
* @param pattern
* pattern of counts of number of black and white pixels that are
* @param row row of black/white values to search
* @param rowOffset position to start search
* @param pattern pattern of counts of number of black and white pixels that are
* being searched for as a pattern
* @return start/end horizontal offset of guard pattern, as an array of two
* ints.
*/
int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) {
static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) {
int patternLength = pattern.length;
int[] counters = new int[patternLength];
int width = row.getSize();
@ -470,7 +390,7 @@ public final class Detector {
int patternStart = rowOffset;
for (int x = rowOffset; x < width; x++) {
boolean pixel = row.get(x);
if ((!pixel && isWhite) || (pixel && !isWhite)) {
if (pixel ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
@ -501,12 +421,9 @@ public final class Detector {
* the total variance from the expected pattern proportions across all
* pattern elements, to the length of the pattern.
*
* @param counters
* observed counters
* @param pattern
* expected pattern
* @param maxIndividualVariance
* The most any counter can differ before we give up
* @param counters observed counters
* @param pattern expected pattern
* @param maxIndividualVariance The most any counter can differ before we give up
* @return ratio of total variance between counters and pattern compared to
* total pattern size, where the ratio has been multiplied by 256.
* So, 0 means no variance (perfect match); 256 means the total

View file

@ -37,12 +37,12 @@ public final class PDF417BlackBox1TestCase extends AbstractBlackBoxTestCase {
//addTest(1, 1, 270.0f);
}
@Override
protected Hashtable<DecodeHintType, Object> getHints() {
Hashtable<DecodeHintType, Object> table = new Hashtable<DecodeHintType, Object>();
Vector v = new Vector();
Hashtable<DecodeHintType, Object> table = new Hashtable<DecodeHintType, Object>(3);
Vector<BarcodeFormat> v = new Vector<BarcodeFormat>(1);
v.add(BarcodeFormat.PDF417);
table.put(DecodeHintType.POSSIBLE_FORMATS, v);
return table;
}