mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Style changes to CodaBar writer
git-svn-id: https://zxing.googlecode.com/svn/trunk@1908 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
02b0816126
commit
015e2a5915
1
AUTHORS
1
AUTHORS
|
@ -39,6 +39,7 @@ Joseph Wain (Google)
|
||||||
Juho Mikkonen
|
Juho Mikkonen
|
||||||
jwicks
|
jwicks
|
||||||
Kamil Kaczmarczyk
|
Kamil Kaczmarczyk
|
||||||
|
Kazuki Nishiura
|
||||||
Kevin O'Sullivan (SITA)
|
Kevin O'Sullivan (SITA)
|
||||||
Kevin Xue (NetDragon Websoft Inc., China)
|
Kevin Xue (NetDragon Websoft Inc., China)
|
||||||
Lachezar Dobrev
|
Lachezar Dobrev
|
||||||
|
|
|
@ -32,14 +32,14 @@ import com.google.zxing.common.BitArray;
|
||||||
public final class CodaBarReader extends OneDReader {
|
public final class CodaBarReader extends OneDReader {
|
||||||
|
|
||||||
private static final String ALPHABET_STRING = "0123456789-$:/.+ABCDTN";
|
private static final String ALPHABET_STRING = "0123456789-$:/.+ABCDTN";
|
||||||
protected static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
|
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
|
* 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
|
* 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
|
* : c is equal to the * pattern NOTE : d is equal to the e pattern
|
||||||
*/
|
*/
|
||||||
protected static final int[] CHARACTER_ENCODINGS = {
|
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, 0x045, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
|
0x00c, 0x018, 0x045, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
|
||||||
0x01A, 0x029 //TN
|
0x01A, 0x029 //TN
|
||||||
|
@ -72,12 +72,13 @@ public final class CodaBarReader extends OneDReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuffer result = new StringBuffer();
|
StringBuffer result = new StringBuffer();
|
||||||
//int[] counters = new int[7];
|
int[] counters = new int[7];
|
||||||
int[] counters;
|
|
||||||
int lastStart;
|
int lastStart;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
counters = new int[]{0, 0, 0, 0, 0, 0, 0}; // reset counters
|
for (int i = 0; i < counters.length; i++) {
|
||||||
|
counters[i] = 0;
|
||||||
|
}
|
||||||
recordPattern(row, nextStart, counters);
|
recordPattern(row, nextStart, counters);
|
||||||
|
|
||||||
char decodedChar = toNarrowWidePattern(counters);
|
char decodedChar = toNarrowWidePattern(counters);
|
||||||
|
@ -110,44 +111,36 @@ public final class CodaBarReader extends OneDReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// valid result?
|
// valid result?
|
||||||
if (result.length() < 2)
|
if (result.length() < 2) {
|
||||||
{
|
|
||||||
throw NotFoundException.getNotFoundInstance();
|
throw NotFoundException.getNotFoundInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
char startchar = result.charAt(0);
|
char startchar = result.charAt(0);
|
||||||
if (!arrayContains(STARTEND_ENCODING, startchar))
|
if (!arrayContains(STARTEND_ENCODING, startchar)) {
|
||||||
{
|
|
||||||
// invalid start character
|
// invalid start character
|
||||||
throw NotFoundException.getNotFoundInstance();
|
throw NotFoundException.getNotFoundInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
// find stop character
|
// find stop character
|
||||||
for (int k = 1;k < result.length() ;k++)
|
for (int k = 1; k < result.length(); k++) {
|
||||||
{
|
if (result.charAt(k) == startchar) {
|
||||||
if (result.charAt(k) == startchar)
|
|
||||||
{
|
|
||||||
// found stop character -> discard rest of the string
|
// found stop character -> discard rest of the string
|
||||||
if ((k+1) != result.length())
|
if (k + 1 != result.length()) {
|
||||||
{
|
|
||||||
result.delete(k + 1, result.length() - 1);
|
result.delete(k + 1, result.length() - 1);
|
||||||
k = result.length();// break out of loop
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove stop/start characters character and check if a string longer than 5 characters is contained
|
// remove stop/start characters character and check if a string longer than 5 characters is contained
|
||||||
if (result.length() > minCharacterLength)
|
if (result.length() <= minCharacterLength) {
|
||||||
{
|
|
||||||
result.deleteCharAt(result.length()-1);
|
|
||||||
result.deleteCharAt(0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Almost surely a false positive ( start + stop + at least 1 character)
|
// Almost surely a false positive ( start + stop + at least 1 character)
|
||||||
throw NotFoundException.getNotFoundInstance();
|
throw NotFoundException.getNotFoundInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.deleteCharAt(result.length() - 1);
|
||||||
|
result.deleteCharAt(0);
|
||||||
|
|
||||||
float left = (float) (start[1] + start[0]) / 2.0f;
|
float left = (float) (start[1] + start[0]) / 2.0f;
|
||||||
float right = (float) (nextStart + lastStart) / 2.0f;
|
float right = (float) (nextStart + lastStart) / 2.0f;
|
||||||
return new Result(
|
return new Result(
|
||||||
|
@ -208,7 +201,7 @@ public final class CodaBarReader extends OneDReader {
|
||||||
throw NotFoundException.getNotFoundInstance();
|
throw NotFoundException.getNotFoundInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static boolean arrayContains(char[] array, char key) {
|
static boolean arrayContains(char[] array, char key) {
|
||||||
if (array != null) {
|
if (array != null) {
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
if (array[i] == key) {
|
if (array[i] == key) {
|
||||||
|
|
|
@ -33,11 +33,9 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @see com.google.zxing.oned.OneDimensionalCodeWriter#encode(java.lang.String)
|
* @see OneDimensionalCodeWriter#encode(java.lang.String)
|
||||||
*/
|
*/
|
||||||
public byte[] encode(String contents) {
|
public byte[] encode(String contents) {
|
||||||
int resultLength;
|
|
||||||
int position = 0;
|
|
||||||
|
|
||||||
// Verify input and calculate decoded length.
|
// Verify input and calculate decoded length.
|
||||||
if (!CodaBarReader.arrayContains(
|
if (!CodaBarReader.arrayContains(
|
||||||
|
@ -51,8 +49,8 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
"Codabar should end with one of the following: 'T', 'N', '*' or 'E'");
|
"Codabar should end with one of the following: 'T', 'N', '*' or 'E'");
|
||||||
}
|
}
|
||||||
// The start character and the end character are decoded to 10 length each.
|
// The start character and the end character are decoded to 10 length each.
|
||||||
resultLength = 20;
|
int resultLength = 20;
|
||||||
char[] charsWhichAreTenLengthEachAfterDecoded = new char[]{'/', ':', '+', '.'};
|
char[] charsWhichAreTenLengthEachAfterDecoded = {'/', ':', '+', '.'};
|
||||||
for (int i = 1; i < contents.length() - 1; i++) {
|
for (int i = 1; i < contents.length() - 1; i++) {
|
||||||
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-'
|
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-'
|
||||||
|| contents.charAt(i) == '$') {
|
|| contents.charAt(i) == '$') {
|
||||||
|
@ -61,16 +59,16 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
charsWhichAreTenLengthEachAfterDecoded, contents.charAt(i))) {
|
charsWhichAreTenLengthEachAfterDecoded, contents.charAt(i))) {
|
||||||
resultLength += 10;
|
resultLength += 10;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Cannot encode : '" + contents.charAt(i) + "'");
|
throw new IllegalArgumentException("Cannot encode : '" + contents.charAt(i) + '\'');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A blank is placed between each character.
|
// A blank is placed between each character.
|
||||||
resultLength += contents.length() - 1;
|
resultLength += contents.length() - 1;
|
||||||
|
|
||||||
byte[] result = new byte[resultLength];
|
byte[] result = new byte[resultLength];
|
||||||
|
int position = 0;
|
||||||
for (int index = 0; index < contents.length(); index++) {
|
for (int index = 0; index < contents.length(); index++) {
|
||||||
char c = Character.toUpperCase(contents.charAt(index));
|
char c = Character.toUpperCase(contents.charAt(index));
|
||||||
int code = 0;
|
|
||||||
if (index == contents.length() - 1) {
|
if (index == contents.length() - 1) {
|
||||||
// Neither * nor E are in the CodaBarReader.ALPHABET.
|
// Neither * nor E are in the CodaBarReader.ALPHABET.
|
||||||
// * is equal to the c pattern, and e is equal to the d pattern
|
// * is equal to the c pattern, and e is equal to the d pattern
|
||||||
|
@ -80,6 +78,7 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
c = 'D';
|
c = 'D';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
int code = 0;
|
||||||
for (int i = 0; i < CodaBarReader.ALPHABET.length; i++) {
|
for (int i = 0; i < CodaBarReader.ALPHABET.length; i++) {
|
||||||
// Found any, because I checked above.
|
// Found any, because I checked above.
|
||||||
if (c == CodaBarReader.ALPHABET[i]) {
|
if (c == CodaBarReader.ALPHABET[i]) {
|
||||||
|
@ -87,7 +86,6 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean isBlack = true;
|
|
||||||
byte color = 1;
|
byte color = 1;
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
int bit = 0;
|
int bit = 0;
|
||||||
|
|
|
@ -29,8 +29,10 @@ import java.util.Hashtable;
|
||||||
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
||||||
*/
|
*/
|
||||||
public abstract class OneDimensionalCodeWriter implements Writer {
|
public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
protected static int sidesMargin;
|
|
||||||
public OneDimensionalCodeWriter(int sidesMargin) {
|
private final int sidesMargin;
|
||||||
|
|
||||||
|
protected OneDimensionalCodeWriter(int sidesMargin) {
|
||||||
this.sidesMargin = sidesMargin;
|
this.sidesMargin = sidesMargin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +48,10 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
* {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
|
* {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
|
||||||
* or {@code height}, {@code IllegalArgumentException} is thrown.
|
* or {@code height}, {@code IllegalArgumentException} is thrown.
|
||||||
*/
|
*/
|
||||||
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
|
public BitMatrix encode(String contents,
|
||||||
|
BarcodeFormat format,
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
Hashtable hints) throws WriterException {
|
Hashtable hints) throws WriterException {
|
||||||
if (contents == null || contents.length() == 0) {
|
if (contents == null || contents.length() == 0) {
|
||||||
throw new IllegalArgumentException("Found empty contents");
|
throw new IllegalArgumentException("Found empty contents");
|
||||||
|
@ -61,8 +66,10 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
return renderResult(code, width, height);
|
return renderResult(code, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return a byte array of horizontal pixels (0 = white, 1 = black) */
|
/**
|
||||||
private static BitMatrix renderResult(byte[] code, int width, int height) {
|
* @return a byte array of horizontal pixels (0 = white, 1 = black)
|
||||||
|
*/
|
||||||
|
private BitMatrix renderResult(byte[] code, int width, int height) {
|
||||||
int inputWidth = code.length;
|
int inputWidth = code.length;
|
||||||
// Add quiet zone on both sides.
|
// Add quiet zone on both sides.
|
||||||
int fullWidth = inputWidth + sidesMargin;
|
int fullWidth = inputWidth + sidesMargin;
|
||||||
|
@ -85,8 +92,7 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
/**
|
/**
|
||||||
* Appends the given pattern to the target array starting at pos.
|
* Appends the given pattern to the target array starting at pos.
|
||||||
*
|
*
|
||||||
* @param startColor
|
* @param startColor starting color - 0 for white, 1 for black
|
||||||
* starting color - 0 for white, 1 for black
|
|
||||||
* @return the number of elements added to target.
|
* @return the number of elements added to target.
|
||||||
*/
|
*/
|
||||||
protected static int appendPattern(byte[] target, int pos, int[] pattern, int startColor) {
|
protected static int appendPattern(byte[] target, int pos, int[] pattern, int startColor) {
|
||||||
|
@ -111,8 +117,9 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
/**
|
/**
|
||||||
* Encode the contents to byte array expression of one-dimensional barcode.
|
* Encode the contents to byte array expression of one-dimensional barcode.
|
||||||
* Start code and end code should be included in result, and side margins should not be included.
|
* Start code and end code should be included in result, and side margins should not be included.
|
||||||
|
*
|
||||||
* @return a byte array of horizontal pixels (0 = white, 1 = black)
|
* @return a byte array of horizontal pixels (0 = white, 1 = black)
|
||||||
* */
|
*/
|
||||||
public abstract byte[] encode(String contents);
|
public abstract byte[] encode(String contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,10 @@ package com.google.zxing.oned;
|
||||||
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
||||||
*/
|
*/
|
||||||
public abstract class UPCEANWriter extends OneDimensionalCodeWriter {
|
public abstract class UPCEANWriter extends OneDimensionalCodeWriter {
|
||||||
public UPCEANWriter() {
|
|
||||||
|
protected UPCEANWriter() {
|
||||||
super(UPCEANReader.START_END_PATTERN.length << 1);
|
super(UPCEANReader.START_END_PATTERN.length << 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,7 @@ public final class CodaBarWriterTestCase extends Assert {
|
||||||
String resultStr = "0000000000" +
|
String resultStr = "0000000000" +
|
||||||
"1001001011011010100101010110010110101001010100110101100101010110110101101001001011"
|
"1001001011011010100101010110010110101001010100110101100101010110110101101001001011"
|
||||||
+ "0000000000";
|
+ "0000000000";
|
||||||
BitMatrix result = new CodaBarWriter().encode(
|
BitMatrix result = new CodaBarWriter().encode("B515-3/N", BarcodeFormat.CODABAR, resultStr.length(), 0);
|
||||||
"B515-3/N", BarcodeFormat.CODABAR, resultStr.length(), 0);
|
|
||||||
for (int i = 0; i < resultStr.length(); i++) {
|
for (int i = 0; i < resultStr.length(); i++) {
|
||||||
assertEquals("Element " + i, resultStr.charAt(i) == '1', result.get(i, 0));
|
assertEquals("Element " + i, resultStr.charAt(i) == '1', result.get(i, 0));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue