Update Codabar style and disable it as its causing too many false positives

git-svn-id: https://zxing.googlecode.com/svn/trunk@1403 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-05-31 12:49:29 +00:00
parent 8b60886e41
commit 492492b591
5 changed files with 135 additions and 163 deletions

View file

@ -99,7 +99,7 @@ public final class MultiFormatReader implements Reader {
formats.contains(BarcodeFormat.UPC_E) ||
formats.contains(BarcodeFormat.EAN_13) ||
formats.contains(BarcodeFormat.EAN_8) ||
formats.contains(BarcodeFormat.CODABAR) ||
//formats.contains(BarcodeFormat.CODABAR) ||
formats.contains(BarcodeFormat.CODE_39) ||
formats.contains(BarcodeFormat.CODE_93) ||
formats.contains(BarcodeFormat.CODE_128) ||

View file

@ -16,144 +16,123 @@
package com.google.zxing.oned;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitArray;
import java.util.Hashtable;
/**
* <p>Decodes Codabar barcodes. </p>
* <p>Decodes Codabar barcodes.</p>
*
* @author Bas Vijfwinkel
*/
public final class CodaBarReader extends OneDReader {
private static final String ALPHABET_STRING = "0123456789-$:/.+ABCDTN";
private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
private static final String ALPHABET_STRING = "0123456789-$:/.+ABCDTN";
private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
/**
* These represent the encodings of characters, as patterns of wide and narrow bars.
* The 7 least-significant bits of each int correspond to the pattern of wide and narrow,
* with 1s representing "wide" and 0s representing narrow.
* NOTE : c is equal to the * pattern
* NOTE : d is equal to the e pattern
*/
/**
* These represent the encodings of characters, as patterns of wide and narrow bars. The 7 least-significant bits of
* each int correspond to the pattern of wide and narrow, with 1s representing "wide" and 0s representing narrow. NOTE
* : c is equal to the * pattern NOTE : d is equal to the e pattern
*/
private static final int[] CHARACTER_ENCODINGS = {
0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, // 0-9
0x00c, 0x018, 0x025, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
0x01A, 0x029 //TN
};
private static final int[] CHARACTER_ENCODINGS = {
0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, // 0-9
0x00c, 0x018, 0x025, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
0x01A,0x029 //TN
};
// multiple start/end patterns
// official start and end patterns
//private static final char[] STARTEND_ENCODING = {'$','A','B','C','D','T','N','+'};
// some codabar generator allow the codabar string to be closed by every character
private static final char[] STARTEND_ENCODING = {'0','1','2','3','4','5','6','7','8','9','-','$',':','/','.','+','A','B','C','D','T','N'};
// multiple start/end patterns
// official start and end patterns
// some codabar generator allow the codabar string to be closed by every character
private static final char[] STARTEND_ENCODING = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '$', ':', '/', '.', '+', 'A', 'B', 'C', 'D', 'T', 'N'};
public CodaBarReader()
{
}
public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws NotFoundException {
int[] start = findAsteriskPattern(row);
start[1] = 0; // BAS: settings this to 0 improves the recognition rate somehow?
int nextStart = start[1];
int end = row.getSize();
// Read off white space
while (nextStart < end && !row.get(nextStart)) {
nextStart++;
}
StringBuffer result = new StringBuffer();
//int[] counters = new int[7];
int[] counters;
int lastStart;
do {
counters = new int[]{0, 0, 0, 0, 0, 0, 0}; // reset counters
recordPattern(row, nextStart, counters);
char decodedChar = toNarrowWidePattern(counters);
if (decodedChar == '!') {
throw NotFoundException.getNotFoundInstance();
}
result.append(decodedChar);
lastStart = nextStart;
for (int i = 0; i < counters.length; i++) {
nextStart += counters[i];
}
// Read off white space
while (nextStart < end && !row.get(nextStart)) {
nextStart++;
}
} while (nextStart < end); // no fixed end pattern so keep on reading while data is available
// find last character in STARTEND_ENCODING
for (int k = result.length() - 1; k >= 0; k--) {
if (arrayContains(STARTEND_ENCODING, result.charAt(k))) {
// valid character -> remove and break out of loop
result.deleteCharAt(k);
k = -1;// break out of loop
} else {
// not a valid character -> remove anyway
result.deleteCharAt(k);
}
}
public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws FormatException, NotFoundException
{
int[] start;
start = findAsteriskPattern(row);
start[1] = 0; // BAS: settings this to 0 improves the recognition rate somehow?
int nextStart = start[1];
int end = row.getSize();
// Read off white space
while (nextStart < end && !row.get(nextStart))
{
nextStart++;
}
// remove first character
if (result.length() > 0) {
result.deleteCharAt(0);
}
StringBuffer result = new StringBuffer();
//int[] counters = new int[7];
int[] counters;
char decodedChar;
int lastStart;
do
{
counters = new int[] {0,0,0,0,0,0,0} ; // reset counters
recordPattern(row, nextStart, counters);
decodedChar = toNarrowWidePattern(counters);
if (decodedChar == '!')
{
throw NotFoundException.getNotFoundInstance();
}
result.append(decodedChar);
lastStart = nextStart;
for (int i = 0; i < counters.length; i++)
{
nextStart += counters[i];
}
// Read off white space
while (nextStart < end && !row.get(nextStart))
{
nextStart++;
}
} while (nextStart < end); // no fixed end pattern so keep on reading while data is available
// Look for whitespace after pattern:
int lastPatternSize = 0;
for (int i = 0; i < counters.length; i++) {
lastPatternSize += counters[i];
}
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// If 50% of last pattern size, following last pattern, is not whitespace, fail
// (but if it's whitespace to the very end of the image, that's OK)
if ((nextStart) != end && (whiteSpaceAfterEnd / 2 < lastPatternSize)) {
throw NotFoundException.getNotFoundInstance();
}
// find last character in STARTEND_ENCODING
for (int k = result.length()-1;k >= 0;k--)
{
if (arrayContains(STARTEND_ENCODING,result.charAt(k)))
{
// valid character -> remove and break out of loop
result.deleteCharAt(k);
k=-1;// break out of loop
}
else
{
// not a valid character -> remove anyway
result.deleteCharAt(k);
}
}
// remove first character
if (result.length() > 0) {result.deleteCharAt(0);}
// Look for whitespace after pattern:
int lastPatternSize = 0;
for (int i = 0; i < counters.length; i++)
{
lastPatternSize += counters[i];
}
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// If 50% of last pattern size, following last pattern, is not whitespace, fail
// (but if it's whitespace to the very end of the image, that's OK)
if ((nextStart) != end && (whiteSpaceAfterEnd / 2 < lastPatternSize))
{
throw NotFoundException.getNotFoundInstance();
}
String resultString = result.toString();
if (resultString.length() == 0) {
// Almost surely a false positive
throw NotFoundException.getNotFoundInstance();
}
String resultString = result.toString();
if (resultString.length() == 0)
{
// Almost surely a false positive
throw NotFoundException.getNotFoundInstance();
}
float left = (float) (start[1] + start[0]) / 2.0f;
float right = (float) (nextStart + lastStart) / 2.0f;
return new Result(
resultString,
null,
new ResultPoint[]{
new ResultPoint(left, (float) rowNumber),
new ResultPoint(right, (float) rowNumber)},
BarcodeFormat.CODABAR);
float left = (float) (start[1] + start[0]) / 2.0f;
float right = (float) (nextStart + lastStart) / 2.0f;
return new Result(
resultString,
null,
new ResultPoint[]{
new ResultPoint(left, (float) rowNumber),
new ResultPoint(right, (float) rowNumber)},
BarcodeFormat.CODABAR);
}
private static int[] findAsteriskPattern(BitArray row) throws NotFoundException {
@ -179,8 +158,7 @@ public final class CodaBarReader extends OneDReader {
} else {
if (counterPosition == patternLength - 1) {
try {
if (arrayContains(STARTEND_ENCODING,toNarrowWidePattern(counters)))
{
if (arrayContains(STARTEND_ENCODING, toNarrowWidePattern(counters))) {
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
return new int[]{patternStart, i};
@ -206,60 +184,54 @@ public final class CodaBarReader extends OneDReader {
throw NotFoundException.getNotFoundInstance();
}
private static boolean arrayContains(char[] array, char key)
{
if (array != null)
{
for (int i=0;i<array.length;i++)
{
if (array[i] == key) { return true; }
}
}
return false;
private static boolean arrayContains(char[] array, char key) {
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key) {
return true;
}
}
}
return false;
}
private static char toNarrowWidePattern(int[] counters) {
// BAS : I have changed the following part because some codabar images would fail with the original routine
// I took from the Code39Reader.java file
// ----------- change start
private static char toNarrowWidePattern(int[] counters) {
// BAS : I have changed the following part because some codabar images would fail with the original routine
// I took from the Code39Reader.java file
// ----------- change start
int numCounters = counters.length;
int maxNarrowCounter = 0;
int wideCounters;
int minCounter = Integer.MAX_VALUE;
for (int i = 0; i < numCounters; i++)
{
if (counters[i] < minCounter) { minCounter = counters[i]; }
if (counters[i] > maxNarrowCounter) { maxNarrowCounter = counters[i]; }
}
// ---------- change end
int minCounter = Integer.MAX_VALUE;
for (int i = 0; i < numCounters; i++) {
if (counters[i] < minCounter) {
minCounter = counters[i];
}
if (counters[i] > maxNarrowCounter) {
maxNarrowCounter = counters[i];
}
}
// ---------- change end
do
{
wideCounters = 0;
int totalWideCountersWidth = 0;
do {
int wideCounters = 0;
int pattern = 0;
for (int i = 0; i < numCounters; i++) {
int counter = counters[i];
if (counters[i] > maxNarrowCounter) {
pattern |= 1 << (numCounters - 1 - i);
wideCounters++;
totalWideCountersWidth += counter;
}
}
if ((wideCounters == 2) || (wideCounters == 3))
{
for (int i = 0; i < CHARACTER_ENCODINGS.length; i++)
{
if (CHARACTER_ENCODINGS[i] == pattern)
{
return ALPHABET[i];
}
}
if ((wideCounters == 2) || (wideCounters == 3)) {
for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) {
if (CHARACTER_ENCODINGS[i] == pattern) {
return ALPHABET[i];
}
}
}
maxNarrowCounter--;
maxNarrowCounter--;
} while (maxNarrowCounter > minCounter);
return '!';
}

View file

@ -65,7 +65,6 @@ public final class MultiFormatOneDReader extends OneDReader {
if (possibleFormats.contains(BarcodeFormat.CODABAR)) {
readers.addElement(new CodaBarReader());
}
if (possibleFormats.contains(BarcodeFormat.RSS14)) {
readers.addElement(new RSS14Reader());
}
@ -76,7 +75,7 @@ public final class MultiFormatOneDReader extends OneDReader {
if (readers.isEmpty()) {
readers.addElement(new MultiFormatUPCEANReader(hints));
readers.addElement(new Code39Reader());
readers.addElement(new CodaBarReader());
//readers.addElement(new CodaBarReader());
readers.addElement(new Code93Reader());
readers.addElement(new Code128Reader());
readers.addElement(new ITFReader());

View file

@ -122,7 +122,7 @@ public final class CommandLineRunner {
vector.addElement(BarcodeFormat.QR_CODE);
vector.addElement(BarcodeFormat.DATAMATRIX);
vector.addElement(BarcodeFormat.PDF417);
vector.addElement(BarcodeFormat.CODABAR);
//vector.addElement(BarcodeFormat.CODABAR);
}
hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
if (tryHarder) {

View file

@ -101,7 +101,7 @@ public final class DecodeServlet extends HttpServlet {
static {
HINTS = new Hashtable<DecodeHintType, Object>(5);
HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Collection<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>();
Collection<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>(17);
possibleFormats.add(BarcodeFormat.UPC_A);
possibleFormats.add(BarcodeFormat.UPC_E);
possibleFormats.add(BarcodeFormat.EAN_8);
@ -109,6 +109,7 @@ public final class DecodeServlet extends HttpServlet {
possibleFormats.add(BarcodeFormat.CODE_39);
possibleFormats.add(BarcodeFormat.CODE_93);
possibleFormats.add(BarcodeFormat.CODE_128);
//possibleFormats.add(BarcodeFormat.CODABAR);
possibleFormats.add(BarcodeFormat.ITF);
possibleFormats.add(BarcodeFormat.RSS14);
possibleFormats.add(BarcodeFormat.QR_CODE);