diff --git a/AUTHORS b/AUTHORS index b8a61098d..bb07abbae 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Aitor Almeida (University of Deusto) Alasdair Mackintosh (Google) Alexander Martin (Haase & Martin GmbH) Andreas Pillath +Andrew Walbran (Google) Andrey Sitnik Androida.hu / http://www.androida.hu/ Antonio Manuel Benjumea (Servinform S.A.) diff --git a/core/src/com/google/zxing/MultiFormatWriter.java b/core/src/com/google/zxing/MultiFormatWriter.java index 297b79edc..6a6a2f304 100644 --- a/core/src/com/google/zxing/MultiFormatWriter.java +++ b/core/src/com/google/zxing/MultiFormatWriter.java @@ -22,6 +22,7 @@ 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.oned.UPCAWriter; import com.google.zxing.qrcode.QRCodeWriter; import java.util.Hashtable; @@ -48,6 +49,8 @@ public final class MultiFormatWriter implements Writer { writer = new EAN8Writer(); } else if (format == BarcodeFormat.EAN_13) { writer = new EAN13Writer(); + } else if (format == BarcodeFormat.UPC_A) { + writer = new UPCAWriter(); } else if (format == BarcodeFormat.QR_CODE) { writer = new QRCodeWriter(); } else if (format == BarcodeFormat.CODE_39) { diff --git a/core/src/com/google/zxing/oned/UPCAWriter.java b/core/src/com/google/zxing/oned/UPCAWriter.java new file mode 100644 index 000000000..611e73dc1 --- /dev/null +++ b/core/src/com/google/zxing/oned/UPCAWriter.java @@ -0,0 +1,67 @@ +/* + * 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 com.google.zxing.BarcodeFormat; +import com.google.zxing.Writer; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; + +import java.util.Hashtable; + +/** + * This object renders a UPC-A code as a {@link BitMatrix}. + * + * @author qwandor@google.com (Andrew Walbran) + */ +public class UPCAWriter implements Writer { + + private final EAN13Writer subWriter = new EAN13Writer(); + + public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) + throws WriterException { + return encode(contents, format, width, height, null); + } + + public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Hashtable hints) + throws WriterException { + if (format != BarcodeFormat.UPC_A) { + throw new IllegalArgumentException("Can only encode UPC-A, but got " + format); + } + return subWriter.encode(preencode(contents), BarcodeFormat.EAN_13, width, height, hints); + } + + /** + * Transform a UPC-A code into the equivalent EAN-13 code, and add a check digit if it is not + * already present. + */ + private static String preencode(String contents) { + int length = contents.length(); + if (length == 11) { + // No check digit present, calculate it and add it + int sum = 0; + for (int i = 0; i < 11; ++i) { + sum += (contents.charAt(i) - '0') * (i % 2 == 0 ? 3 : 1); + } + contents += (1000 - sum) % 10; + } else if (length != 12) { + throw new IllegalArgumentException( + "Requested contents should be 11 or 12 digits long, but got " + contents.length()); + } + return '0' + contents; + } +} diff --git a/core/test/src/com/google/zxing/oned/UPCAWriterTestCase.java b/core/test/src/com/google/zxing/oned/UPCAWriterTestCase.java new file mode 100644 index 000000000..593a49260 --- /dev/null +++ b/core/test/src/com/google/zxing/oned/UPCAWriterTestCase.java @@ -0,0 +1,49 @@ +/* + * 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 com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author qwandor@google.com (Andrew Walbran) + */ +public final class UPCAWriterTestCase extends Assert { + + @Test + public void testEncode() throws WriterException { + String testStr = "00010101000110110111011000100010110101111011110101010111001011101001001110110011011011001011100101000"; + BitMatrix result = new UPCAWriter().encode("485963095124", BarcodeFormat.UPC_A, testStr.length(), 0); + for (int i = 0; i < testStr.length(); i++) { + assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0)); + } + } + + @Test + public void testAddChecksumAndEncode() throws WriterException { + String testStr = "00010100110010010011011110101000110110001010111101010100010010010001110100111001011001101101100101000"; + BitMatrix result = new UPCAWriter().encode("12345678901", BarcodeFormat.UPC_A, testStr.length(), 0); + for (int i = 0; i < testStr.length(); i++) { + assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0)); + } + } + +}