mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Issue 582
git-svn-id: https://zxing.googlecode.com/svn/trunk@1625 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
a44f07ce90
commit
e9ae6dd4d3
1
AUTHORS
1
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.)
|
||||
|
|
|
@ -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) {
|
||||
|
|
67
core/src/com/google/zxing/oned/UPCAWriter.java
Normal file
67
core/src/com/google/zxing/oned/UPCAWriter.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
49
core/test/src/com/google/zxing/oned/UPCAWriterTestCase.java
Normal file
49
core/test/src/com/google/zxing/oned/UPCAWriterTestCase.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue