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:
srowen 2011-09-15 09:22:48 +00:00
parent 02b0816126
commit 015e2a5915
6 changed files with 72 additions and 72 deletions

View file

@ -39,6 +39,7 @@ Joseph Wain (Google)
Juho Mikkonen
jwicks
Kamil Kaczmarczyk
Kazuki Nishiura
Kevin O'Sullivan (SITA)
Kevin Xue (NetDragon Websoft Inc., China)
Lachezar Dobrev

View file

@ -32,14 +32,14 @@ import com.google.zxing.common.BitArray;
public final class CodaBarReader extends OneDReader {
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
* 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
*/
protected static final int[] CHARACTER_ENCODINGS = {
static final int[] CHARACTER_ENCODINGS = {
0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, // 0-9
0x00c, 0x018, 0x045, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, // -$:/.+ABCD
0x01A, 0x029 //TN
@ -72,12 +72,13 @@ public final class CodaBarReader extends OneDReader {
}
StringBuffer result = new StringBuffer();
//int[] counters = new int[7];
int[] counters;
int[] counters = new int[7];
int lastStart;
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);
char decodedChar = toNarrowWidePattern(counters);
@ -109,44 +110,36 @@ public final class CodaBarReader extends OneDReader {
throw NotFoundException.getNotFoundInstance();
}
// valid result?
if (result.length() < 2)
{
throw NotFoundException.getNotFoundInstance();
}
char startchar = result.charAt(0);
if (!arrayContains(STARTEND_ENCODING, startchar))
{
//invalid start character
throw NotFoundException.getNotFoundInstance();
}
// find stop character
for (int k = 1;k < result.length() ;k++)
{
if (result.charAt(k) == startchar)
{
// found stop character -> discard rest of the string
if ((k+1) != result.length())
{
result.delete(k+1,result.length()-1);
k = result.length();// break out of loop
}
}
// valid result?
if (result.length() < 2) {
throw NotFoundException.getNotFoundInstance();
}
// remove stop/start characters character and check if a string longer than 5 characters is contained
if (result.length() > minCharacterLength)
{
result.deleteCharAt(result.length()-1);
result.deleteCharAt(0);
}
else
{
// Almost surely a false positive ( start + stop + at least 1 character)
throw NotFoundException.getNotFoundInstance();
}
char startchar = result.charAt(0);
if (!arrayContains(STARTEND_ENCODING, startchar)) {
// invalid start character
throw NotFoundException.getNotFoundInstance();
}
// find stop character
for (int k = 1; k < result.length(); k++) {
if (result.charAt(k) == startchar) {
// found stop character -> discard rest of the string
if (k + 1 != result.length()) {
result.delete(k + 1, result.length() - 1);
break;
}
}
}
// remove stop/start characters character and check if a string longer than 5 characters is contained
if (result.length() <= minCharacterLength) {
// Almost surely a false positive ( start + stop + at least 1 character)
throw NotFoundException.getNotFoundInstance();
}
result.deleteCharAt(result.length() - 1);
result.deleteCharAt(0);
float left = (float) (start[1] + start[0]) / 2.0f;
float right = (float) (nextStart + lastStart) / 2.0f;
@ -208,7 +201,7 @@ public final class CodaBarReader extends OneDReader {
throw NotFoundException.getNotFoundInstance();
}
protected static boolean arrayContains(char[] array, char key) {
static boolean arrayContains(char[] array, char key) {
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key) {

View file

@ -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) {
int resultLength;
int position = 0;
// Verify input and calculate decoded length.
if (!CodaBarReader.arrayContains(
@ -46,32 +44,32 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
"Codabar should start with one of the following: 'A', 'B', 'C' or 'D'");
}
if (!CodaBarReader.arrayContains(new char[]{'T', 'N', '*', 'E'},
Character.toUpperCase(contents.charAt(contents.length() - 1)))) {
Character.toUpperCase(contents.charAt(contents.length() - 1)))) {
throw new IllegalArgumentException(
"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.
resultLength = 20;
char[] charsWhichAreTenLengthEachAfterDecoded = new char[]{'/', ':', '+', '.'};
for (int i = 1; i < contents.length()-1; i++) {
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-'
int resultLength = 20;
char[] charsWhichAreTenLengthEachAfterDecoded = {'/', ':', '+', '.'};
for (int i = 1; i < contents.length() - 1; i++) {
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-'
|| contents.charAt(i) == '$') {
resultLength += 9;
} else if(CodaBarReader.arrayContains(
} else if (CodaBarReader.arrayContains(
charsWhichAreTenLengthEachAfterDecoded, contents.charAt(i))) {
resultLength += 10;
} else {
throw new IllegalArgumentException("Cannot encode : '" + contents.charAt(i) + "'");
throw new IllegalArgumentException("Cannot encode : '" + contents.charAt(i) + '\'');
}
}
// A blank is placed between each character.
resultLength += contents.length() - 1;
byte[] result = new byte[resultLength];
int position = 0;
for (int index = 0; index < contents.length(); 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.
// * is equal to the c pattern, and e is equal to the d pattern
if (c == '*') {
@ -80,6 +78,7 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
c = 'D';
}
}
int code = 0;
for (int i = 0; i < CodaBarReader.ALPHABET.length; i++) {
// Found any, because I checked above.
if (c == CodaBarReader.ALPHABET[i]) {
@ -87,14 +86,13 @@ public class CodaBarWriter extends OneDimensionalCodeWriter {
break;
}
}
boolean isBlack = true;
byte color = 1;
int counter = 0;
int bit = 0;
while (bit < 7){ // A character consists of 7 digit.
while (bit < 7) { // A character consists of 7 digit.
result[position] = color;
position++;
if (((code >> (6-bit)) & 1) == 0 || counter == 1){
if (((code >> (6 - bit)) & 1) == 0 || counter == 1) {
color ^= 1; // Flip the color.
bit++;
counter = 0;

View file

@ -29,13 +29,15 @@ import java.util.Hashtable;
* @author dsbnatut@gmail.com (Kazuki Nishiura)
*/
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;
}
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
throws WriterException {
throws WriterException {
return encode(contents, format, width, height, null);
}
@ -46,23 +48,28 @@ public abstract class OneDimensionalCodeWriter implements Writer {
* {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
* or {@code height}, {@code IllegalArgumentException} is thrown.
*/
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws WriterException {
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Hashtable hints) throws WriterException {
if (contents == null || contents.length() == 0) {
throw new IllegalArgumentException("Found empty contents");
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Negative size is not allowed. Input: "
+ width + 'x' + height);
+ width + 'x' + height);
}
byte[] code = encode(contents);
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;
// Add quiet zone on both sides.
int fullWidth = inputWidth + sidesMargin;
@ -85,11 +92,10 @@ public abstract class OneDimensionalCodeWriter implements Writer {
/**
* Appends the given pattern to the target array starting at pos.
*
* @param startColor
* starting color - 0 for white, 1 for black
* @param startColor starting color - 0 for white, 1 for black
* @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) {
if (startColor != 0 && startColor != 1) {
throw new IllegalArgumentException(
"startColor must be either 0 or 1, but got: " + startColor);
@ -111,8 +117,9 @@ public abstract class OneDimensionalCodeWriter implements Writer {
/**
* 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.
*
* @return a byte array of horizontal pixels (0 = white, 1 = black)
* */
*/
public abstract byte[] encode(String contents);
}

View file

@ -25,8 +25,10 @@ package com.google.zxing.oned;
* @author dsbnatut@gmail.com (Kazuki Nishiura)
*/
public abstract class UPCEANWriter extends OneDimensionalCodeWriter {
public UPCEANWriter() {
protected UPCEANWriter() {
super(UPCEANReader.START_END_PATTERN.length << 1);
}
}

View file

@ -34,8 +34,7 @@ public final class CodaBarWriterTestCase extends Assert {
String resultStr = "0000000000" +
"1001001011011010100101010110010110101001010100110101100101010110110101101001001011"
+ "0000000000";
BitMatrix result = new CodaBarWriter().encode(
"B515-3/N", BarcodeFormat.CODABAR, resultStr.length(), 0);
BitMatrix result = new CodaBarWriter().encode("B515-3/N", BarcodeFormat.CODABAR, resultStr.length(), 0);
for (int i = 0; i < resultStr.length(); i++) {
assertEquals("Element " + i, resultStr.charAt(i) == '1', result.get(i, 0));
}