Add support encoding UPC-E format

This commit is contained in:
RX1226 2015-09-04 21:59:41 +08:00
parent c44ee5e587
commit 17676d064a
4 changed files with 111 additions and 2 deletions

View file

@ -26,6 +26,7 @@ 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.oned.UPCEWriter;
import com.google.zxing.pdf417.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
@ -58,6 +59,9 @@ public final class MultiFormatWriter implements Writer {
case EAN_8:
writer = new EAN8Writer();
break;
case UPC_E:
writer = new UPCEWriter();
break;
case EAN_13:
writer = new EAN13Writer();
break;

View file

@ -56,7 +56,10 @@ public abstract class UPCEANReader extends OneDReader {
* Pattern marking the middle of a UPC/EAN pattern, separating the two halves.
*/
static final int[] MIDDLE_PATTERN = {1, 1, 1, 1, 1};
/**
* end guard pattern.
*/
static final int[] END_PATTERN = {1, 1, 1, 1, 1, 1};
/**
* "Odd", or "L" patterns used to encode UPC/EAN digits.
*/

View file

@ -29,7 +29,32 @@ import com.google.zxing.common.BitArray;
* @author Sean Owen
*/
public final class UPCEReader extends UPCEANReader {
// For an UPC-E barcode, the final digit is represented by the parities used
// to encode the middle six digits, according to the table below.
//
// Parity of next 6 digits
// Digit 0 1 2 3 4 5
// 0 Even Even Even Odd Odd Odd
// 1 Even Even Odd Even Odd Odd
// 2 Even Even Odd Odd Even Odd
// 3 Even Even Odd Odd Odd Even
// 4 Even Odd Even Even Odd Odd
// 5 Even Odd Odd Even Even Odd
// 6 Even Odd Odd Odd Even Even
// 7 Even Odd Even Odd Even Odd
// 8 Even Odd Even Odd Odd Even
// 9 Even Odd Odd Even Odd Even
//
// The encoding is represented by the following array, which is a bit pattern
// using Odd = 0 and Even = 1. For example, 5 is represented by:
//
// Odd Even Even Odd Odd Even
// in binary:
// 0 1 1 0 0 1 == 0x19
//
static final int[] CHECK_DIGIT_ENCODINGS = {
0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25
};
/**
* The pattern that marks the middle, and end, of a UPC-E pattern.
* There is no "second half" to a UPC-E barcode.

View file

@ -0,0 +1,77 @@
/*
* Copyright 2009 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.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
* This object renders an UPC-E code as a {@link BitMatrix}.
*
* @author 0979097955s@gmail.com (RX)
*/
public final class UPCEWriter extends UPCEANWriter {
private static final int CODE_WIDTH = 3 + // start guard
(7 * 6) + // bars
6; // end guard
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType, ?> hints) throws WriterException {
if (format != BarcodeFormat.UPC_E) {
throw new IllegalArgumentException("Can only encode UPC_E, but got " + format);
}
return super.encode(contents, format, width, height, hints);
}
@Override
public boolean[] encode(String contents) {
if (contents.length() != 8) {
throw new IllegalArgumentException(
"Requested contents should be 8 digits long, but got " + contents.length());
}
int checkDigit = Integer.parseInt(contents.substring(7, 8));
int parities = UPCEReader.CHECK_DIGIT_ENCODINGS[checkDigit];
boolean[] result = new boolean[CODE_WIDTH];
int pos = 0;
pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);
for (int i = 1; i <= 6; i++) {
int digit = Integer.parseInt(contents.substring(i, i + 1));
if ((parities >> (6 - i) & 1) == 1) {
digit += 10;
}
pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false);
}
appendPattern(result, pos, UPCEANReader.END_PATTERN, false);
return result;
}
}