Add basic Code 128, Code 39, ITF writers, per Erik

git-svn-id: https://zxing.googlecode.com/svn/trunk@1252 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-03-25 12:49:27 +00:00
parent a0381b760d
commit 6e05578429
8 changed files with 236 additions and 9 deletions

View file

@ -10,6 +10,7 @@ Christian Brunschen (Google)
Daniel Switkin (Google)
David Albert (Bug Labs)
Diego Pierotto
Erik Barbara
Fred Lin (Anobiit)
Hannes Erven
hypest (Barcorama project)

View file

@ -17,8 +17,11 @@
package com.google.zxing;
import com.google.zxing.common.ByteMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.oned.Code39Writer;
import com.google.zxing.oned.EAN13Writer;
import com.google.zxing.oned.EAN8Writer;
import com.google.zxing.oned.ITFWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;
@ -40,16 +43,23 @@ public final class MultiFormatWriter implements Writer {
public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws WriterException {
Writer writer;
if (format == BarcodeFormat.EAN_8) {
return new EAN8Writer().encode(contents, format, width, height, hints);
writer = new EAN8Writer();
} else if (format == BarcodeFormat.EAN_13) {
return new EAN13Writer().encode(contents, format, width, height, hints);
writer = new EAN13Writer();
} else if (format == BarcodeFormat.QR_CODE) {
return new QRCodeWriter().encode(contents, format, width, height, hints);
}
else {
writer = new QRCodeWriter();
} else if (format == BarcodeFormat.CODE_39) {
writer = new Code39Writer();
} else if (format == BarcodeFormat.CODE_128) {
writer = new Code128Writer();
} else if (format == BarcodeFormat.ITF) {
writer = new ITFWriter();
} else {
throw new IllegalArgumentException("No encoder available for format " + format);
}
return writer.encode(contents, format, width, height, hints);
}
}

View file

@ -33,7 +33,7 @@ import java.util.Hashtable;
*/
public final class Code128Reader extends OneDReader {
private static final int[][] CODE_PATTERNS = {
static final int[][] CODE_PATTERNS = {
{2, 1, 2, 2, 2, 2}, // 0
{2, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 2, 1},

View file

@ -0,0 +1,73 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.oned;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
* This object renders a CODE128 code as a {@link BitMatrix}.
*
* @author erik.barbara@gmail.com (Erik Barbara)
*/
public final class Code128Writer extends UPCEANWriter {
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Hashtable hints) throws WriterException {
if (format != BarcodeFormat.CODE_128) {
throw new IllegalArgumentException("Can only encode CODE_128, but got " + format);
}
return super.encode(contents, format, width, height, hints);
}
public byte[] encode(String contents) {
int length = contents.length();
if (length > 80) {
throw new IllegalArgumentException(
"Requested contents should be less than 80 digits long, but got " + length);
}
int codeWidth = 11 + 11 + 13; //start plus check plus stop character
//get total code width for this barcode
for (int i = 0; i < length; i++) {
int[] patterns = Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '];
for (int j = 0; j < patterns.length; j++) {
codeWidth += patterns[j];
}
}
byte[] result = new byte[codeWidth];
int pos = appendPattern(result, 0, Code128Reader.CODE_PATTERNS[104], 1);
int check = 104;
//append next character to bytematrix
for(int i = 0; i < length; i++) {
check += (contents.charAt(i) - ' ') * (i + 1);
pos += appendPattern(result, pos, Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '],1);
}
//compute checksum and append it along with end character and quiet space
check %= 103;
pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[check],1);
pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[106],1);
return result;
}
}

View file

@ -33,7 +33,7 @@ import java.util.Hashtable;
*/
public final class Code39Reader extends OneDReader {
private static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
/**
@ -41,7 +41,7 @@ public final class Code39Reader extends OneDReader {
* The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
* with 1s representing "wide" and 0s representing narrow.
*/
private static final int[] CHARACTER_ENCODINGS = {
static final int[] CHARACTER_ENCODINGS = {
0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9
0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, // A-J
0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, // K-T

View file

@ -0,0 +1,79 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.oned;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
* This object renders a CODE39 code as a {@link BitMatrix}.
*
* @author erik.barbara@gmail.com (Erik Barbara)
*/
public final class Code39Writer extends UPCEANWriter {
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws WriterException {
if (format != BarcodeFormat.CODE_39) {
throw new IllegalArgumentException("Can only encode CODE_39, but got " + format);
}
return super.encode(contents, format, width, height, hints);
}
public byte[] encode(String contents) {
int length = contents.length();
if (length > 80) {
throw new IllegalArgumentException(
"Requested contents should be less than 80 digits long, but got " + length);
}
int[] widths = new int[9];
int codeWidth = 24 + 1 + length;
for (int i = 0; i < length; i++) {
int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
for(int j = 0; j < widths.length; j++) {
codeWidth += widths[j];
}
}
byte[] result = new byte[codeWidth];
toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
int pos = appendPattern(result, 0, widths, 1);
int[] narrowWhite = {1};
pos += appendPattern(result, pos, narrowWhite, 0);
//append next character to bytematrix
for(int i = length-1; i >= 0; i--) {
int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
pos += appendPattern(result, pos, widths, 1);
pos += appendPattern(result, pos, narrowWhite, 0);
}
toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
pos += appendPattern(result, pos, widths, 1);
return result;
}
private static void toIntArray(int a, int[] toReturn) {
for (int i = 0; i < 9; i++) {
int temp = a & (1 << i);
toReturn[i] = (temp == 0) ? 1 : 2;
}
}
}

View file

@ -63,7 +63,7 @@ public final class ITFReader extends OneDReader {
/**
* Patterns of Wide / Narrow lines to indicate each digit
*/
private static final int[][] PATTERNS = {
static final int[][] PATTERNS = {
{N, N, W, W, N}, // 0
{W, N, N, N, W}, // 1
{N, W, N, N, W}, // 2

View file

@ -0,0 +1,64 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.oned;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
* This object renders a ITF code as a {@link BitMatrix}.
*
* @author erik.barbara@gmail.com (Erik Barbara)
*/
public final class ITFWriter extends UPCEANWriter {
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws WriterException {
if (format != BarcodeFormat.ITF) {
throw new IllegalArgumentException("Can only encode ITF, but got " + format);
}
return super.encode(contents, format, width, height, hints);
}
public byte[] encode(String contents) {
int length = contents.length();
if (length > 80) {
throw new IllegalArgumentException(
"Requested contents should be less than 80 digits long, but got " + length);
}
byte[] result = new byte[9 + 9 * length];
int[] start = {1, 1, 1, 1};
int pos = appendPattern(result, 0, start, 1);
for (int i = 0; i < length; i += 2) {
int one = Character.digit(contents.charAt(i), 10);
int two = Character.digit(contents.charAt(i+1), 10);
int[] encoding = new int[18];
for (int j = 0; j < 10; j += 2) {
encoding[j] = ITFReader.PATTERNS[one][j];
encoding[j + 1] = ITFReader.PATTERNS[two][j];
}
pos += appendPattern(result, pos, encoding, 1);
}
int[] end = {3, 1, 1};
pos += appendPattern(result, pos, end, 1);
return result;
}
}