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.UPC_E) ||
formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_13) ||
formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.EAN_8) ||
formats.contains(BarcodeFormat.CODABAR) || //formats.contains(BarcodeFormat.CODABAR) ||
formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_39) ||
formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_93) ||
formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.CODE_128) ||

View file

@ -16,13 +16,13 @@
package com.google.zxing.oned; package com.google.zxing.oned;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.ResultPoint; import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
import java.util.Hashtable;
/** /**
* <p>Decodes Codabar barcodes.</p> * <p>Decodes Codabar barcodes.</p>
@ -35,13 +35,10 @@ public final class CodaBarReader extends OneDReader {
private static final char[] ALPHABET = ALPHABET_STRING.toCharArray(); private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
/** /**
* These represent the encodings of characters, as patterns of wide and narrow bars. * These represent the encodings of characters, as patterns of wide and narrow bars. The 7 least-significant bits of
* The 7 least-significant bits of each int correspond to the pattern of wide and narrow, * each int correspond to the pattern of wide and narrow, with 1s representing "wide" and 0s representing narrow. NOTE
* with 1s representing "wide" and 0s representing narrow. * : c is equal to the * pattern NOTE : d is equal to the e pattern
* NOTE : c is equal to the * pattern
* NOTE : d is equal to the e pattern
*/ */
private static final int[] CHARACTER_ENCODINGS = { private static final int[] CHARACTER_ENCODINGS = {
0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, // 0-9 0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, // 0-9
0x00c, 0x018, 0x025, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD 0x00c, 0x018, 0x025, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
@ -50,70 +47,53 @@ public final class CodaBarReader extends OneDReader {
// multiple start/end patterns // multiple start/end patterns
// official start and 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 // 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'}; 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);
}
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? start[1] = 0; // BAS: settings this to 0 improves the recognition rate somehow?
int nextStart = start[1]; int nextStart = start[1];
int end = row.getSize(); int end = row.getSize();
// Read off white space // Read off white space
while (nextStart < end && !row.get(nextStart)) while (nextStart < end && !row.get(nextStart)) {
{
nextStart++; nextStart++;
} }
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
//int[] counters = new int[7]; //int[] counters = new int[7];
int[] counters; int[] counters;
char decodedChar;
int lastStart; int lastStart;
do do {
{
counters = new int[]{0, 0, 0, 0, 0, 0, 0}; // reset counters counters = new int[]{0, 0, 0, 0, 0, 0, 0}; // reset counters
recordPattern(row, nextStart, counters); recordPattern(row, nextStart, counters);
decodedChar = toNarrowWidePattern(counters); char decodedChar = toNarrowWidePattern(counters);
if (decodedChar == '!') if (decodedChar == '!') {
{
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }
result.append(decodedChar); result.append(decodedChar);
lastStart = nextStart; lastStart = nextStart;
for (int i = 0; i < counters.length; i++) for (int i = 0; i < counters.length; i++) {
{
nextStart += counters[i]; nextStart += counters[i];
} }
// Read off white space // Read off white space
while (nextStart < end && !row.get(nextStart)) while (nextStart < end && !row.get(nextStart)) {
{
nextStart++; nextStart++;
} }
} while (nextStart < end); // no fixed end pattern so keep on reading while data is available } while (nextStart < end); // no fixed end pattern so keep on reading while data is available
// find last character in STARTEND_ENCODING // find last character in STARTEND_ENCODING
for (int k = result.length()-1;k >= 0;k--) for (int k = result.length() - 1; k >= 0; k--) {
{ if (arrayContains(STARTEND_ENCODING, result.charAt(k))) {
if (arrayContains(STARTEND_ENCODING,result.charAt(k)))
{
// valid character -> remove and break out of loop // valid character -> remove and break out of loop
result.deleteCharAt(k); result.deleteCharAt(k);
k = -1;// break out of loop k = -1;// break out of loop
} } else {
else
{
// not a valid character -> remove anyway // not a valid character -> remove anyway
result.deleteCharAt(k); result.deleteCharAt(k);
} }
@ -121,26 +101,25 @@ public final class CodaBarReader extends OneDReader {
// remove first character // remove first character
if (result.length() > 0) {result.deleteCharAt(0);} if (result.length() > 0) {
result.deleteCharAt(0);
}
// Look for whitespace after pattern: // Look for whitespace after pattern:
int lastPatternSize = 0; int lastPatternSize = 0;
for (int i = 0; i < counters.length; i++) for (int i = 0; i < counters.length; i++) {
{
lastPatternSize += counters[i]; lastPatternSize += counters[i];
} }
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// If 50% of last pattern size, following last pattern, is not whitespace, fail // 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) // (but if it's whitespace to the very end of the image, that's OK)
if ((nextStart) != end && (whiteSpaceAfterEnd / 2 < lastPatternSize)) if ((nextStart) != end && (whiteSpaceAfterEnd / 2 < lastPatternSize)) {
{
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }
String resultString = result.toString(); String resultString = result.toString();
if (resultString.length() == 0) if (resultString.length() == 0) {
{
// Almost surely a false positive // Almost surely a false positive
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }
@ -179,8 +158,7 @@ public final class CodaBarReader extends OneDReader {
} else { } else {
if (counterPosition == patternLength - 1) { if (counterPosition == patternLength - 1) {
try { 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 // Look for whitespace before start pattern, >= 50% of width of start pattern
if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
return new int[]{patternStart, i}; return new int[]{patternStart, i};
@ -206,13 +184,12 @@ public final class CodaBarReader extends OneDReader {
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }
private static boolean arrayContains(char[] array, char key) private static boolean arrayContains(char[] array, char key) {
{ if (array != null) {
if (array != null) for (int i = 0; i < array.length; i++) {
{ if (array[i] == key) {
for (int i=0;i<array.length;i++) return true;
{ }
if (array[i] == key) { return true; }
} }
} }
return false; return false;
@ -224,37 +201,32 @@ public final class CodaBarReader extends OneDReader {
// ----------- change start // ----------- change start
int numCounters = counters.length; int numCounters = counters.length;
int maxNarrowCounter = 0; int maxNarrowCounter = 0;
int wideCounters;
int minCounter = Integer.MAX_VALUE; int minCounter = Integer.MAX_VALUE;
for (int i = 0; i < numCounters; i++) for (int i = 0; i < numCounters; i++) {
{ if (counters[i] < minCounter) {
if (counters[i] < minCounter) { minCounter = counters[i]; } minCounter = counters[i];
if (counters[i] > maxNarrowCounter) { maxNarrowCounter = counters[i]; } }
if (counters[i] > maxNarrowCounter) {
maxNarrowCounter = counters[i];
}
} }
// ---------- change end // ---------- change end
do do {
{ int wideCounters = 0;
wideCounters = 0;
int totalWideCountersWidth = 0;
int pattern = 0; int pattern = 0;
for (int i = 0; i < numCounters; i++) { for (int i = 0; i < numCounters; i++) {
int counter = counters[i];
if (counters[i] > maxNarrowCounter) { if (counters[i] > maxNarrowCounter) {
pattern |= 1 << (numCounters - 1 - i); pattern |= 1 << (numCounters - 1 - i);
wideCounters++; wideCounters++;
totalWideCountersWidth += counter;
} }
} }
if ((wideCounters == 2) || (wideCounters == 3)) if ((wideCounters == 2) || (wideCounters == 3)) {
{ for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) {
for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) if (CHARACTER_ENCODINGS[i] == pattern) {
{
if (CHARACTER_ENCODINGS[i] == pattern)
{
return ALPHABET[i]; return ALPHABET[i];
} }
} }

View file

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

View file

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

View file

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