mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Minor changes to support EAN-13.
git-svn-id: https://zxing.googlecode.com/svn/trunk@101 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
34c8f125ce
commit
776c3db869
|
@ -25,10 +25,10 @@ public final class UPCParsedResult extends ParsedReaderResult {
|
||||||
|
|
||||||
public UPCParsedResult(String rawText) {
|
public UPCParsedResult(String rawText) {
|
||||||
super(ParsedReaderResultType.UPC);
|
super(ParsedReaderResultType.UPC);
|
||||||
if (rawText.length() != 12) {
|
if (rawText.length() != 12 && rawText.length() != 13) {
|
||||||
throw new IllegalArgumentException("Wrong number of digits for UPC");
|
throw new IllegalArgumentException("Wrong number of digits for UPC");
|
||||||
}
|
}
|
||||||
for (int x = 0; x < 12; x++) {
|
for (int x = 0; x < rawText.length(); x++) {
|
||||||
char c = rawText.charAt(x);
|
char c = rawText.charAt(x);
|
||||||
if (c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
throw new IllegalArgumentException("Invalid character found in UPC");
|
throw new IllegalArgumentException("Invalid character found in UPC");
|
||||||
|
|
|
@ -218,17 +218,17 @@ final class UPCDecoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
int checksum = 0;
|
int checksum = 0;
|
||||||
int end = result.length()-2;
|
int end = result.length() - 2;
|
||||||
int factor = 3;
|
int factor = 3;
|
||||||
// Calculate from penultimate digit down to first. This avoids having to
|
// Calculate from penultimate digit down to first. This avoids having to
|
||||||
// account for the optional '0' on the front, which won't actually affect
|
// account for the optional '0' on the front, which won't actually affect
|
||||||
// the calculation.
|
// the calculation.
|
||||||
for (int i = end; i >= 0; i--) {
|
for (int i = end; i >= 0; i--) {
|
||||||
int value = (result.charAt(i) - (int) '0') * factor;
|
int value = (result.charAt(i) - (int)'0') * factor;
|
||||||
checksum += value;
|
checksum += value;
|
||||||
factor = factor == 3 ? 1 : 3;
|
factor = factor == 3 ? 1 : 3;
|
||||||
}
|
}
|
||||||
int endValue = (result.charAt(end+1) - (int) '0');
|
int endValue = (result.charAt(end + 1) - (int)'0');
|
||||||
//Log("checksum + endValue = " + (checksum + endValue));
|
//Log("checksum + endValue = " + (checksum + endValue));
|
||||||
return (checksum + endValue) % 10 == 0;
|
return (checksum + endValue) % 10 == 0;
|
||||||
}
|
}
|
||||||
|
@ -236,12 +236,12 @@ final class UPCDecoder {
|
||||||
private int decodeOneSide(BitArray rowData, int rowOffset, boolean checkBothParities) {
|
private int decodeOneSide(BitArray rowData, int rowOffset, boolean checkBothParities) {
|
||||||
int[] counters = new int[4];
|
int[] counters = new int[4];
|
||||||
byte firstDigitPattern = 0;
|
byte firstDigitPattern = 0;
|
||||||
|
CharResult foundChar = new CharResult();
|
||||||
for (int x = 0; x < 6 && rowOffset < width; x++) {
|
for (int x = 0; x < 6 && rowOffset < width; x++) {
|
||||||
recordPattern(rowData, rowOffset, counters, 4);
|
recordPattern(rowData, rowOffset, counters, 4);
|
||||||
for (int y = 0; y < 4; y++) {
|
for (int y = 0; y < 4; y++) {
|
||||||
rowOffset += counters[y];
|
rowOffset += counters[y];
|
||||||
}
|
}
|
||||||
CharResult foundChar = new CharResult();
|
|
||||||
findDigit(counters, foundChar, checkBothParities);
|
findDigit(counters, foundChar, checkBothParities);
|
||||||
if (foundChar.parity == UNKNOWN_PARITY) {
|
if (foundChar.parity == UNKNOWN_PARITY) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -254,7 +254,7 @@ final class UPCDecoder {
|
||||||
char firstDigit = '-';
|
char firstDigit = '-';
|
||||||
for (int i = 0; i < FIRST_DIGIT_ENCODINGS.length; i++) {
|
for (int i = 0; i < FIRST_DIGIT_ENCODINGS.length; i++) {
|
||||||
if (firstDigitPattern == FIRST_DIGIT_ENCODINGS[i]) {
|
if (firstDigitPattern == FIRST_DIGIT_ENCODINGS[i]) {
|
||||||
firstDigit = (char) ((int) '0' + i);
|
firstDigit = (char)((int)'0' + i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -363,7 +363,7 @@ final class UPCDecoder {
|
||||||
}
|
}
|
||||||
if (match) {
|
if (match) {
|
||||||
result.parity = ODD_PARITY;
|
result.parity = ODD_PARITY;
|
||||||
result.character = (char) ((int) '0' + x);
|
result.character = (char)((int)'0' + x);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -381,7 +381,7 @@ final class UPCDecoder {
|
||||||
}
|
}
|
||||||
if (match) {
|
if (match) {
|
||||||
result.parity = EVEN_PARITY;
|
result.parity = EVEN_PARITY;
|
||||||
result.character = (char) ((int) '0' + x);
|
result.character = (char)((int)'0' + x);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue