2 * Copyright 2009 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.oned;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Writer;
21 import com.google.zxing.WriterException;
22 import com.google.zxing.common.BitMatrix;
24 import java.util.Hashtable;
27 * <p>Encapsulates functionality and implementation that is common to UPC and EAN families
28 * of one-dimensional barcodes.</p>
30 * @author aripollak@gmail.com (Ari Pollak)
32 public abstract class UPCEANWriter implements Writer {
34 public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
35 throws WriterException {
36 return encode(contents, format, width, height, null);
39 public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
40 Hashtable hints) throws WriterException {
41 if (contents == null || contents.length() == 0) {
42 throw new IllegalArgumentException("Found empty contents");
45 if (width < 0 || height < 0) {
46 throw new IllegalArgumentException("Requested dimensions are too small: "
47 + width + 'x' + height);
50 byte[] code = encode(contents);
51 return renderResult(code, width, height);
54 /** @return a byte array of horizontal pixels (0 = white, 1 = black) */
55 private static BitMatrix renderResult(byte[] code, int width, int height) {
56 int inputWidth = code.length;
57 // Add quiet zone on both sides
58 int fullWidth = inputWidth + (UPCEANReader.START_END_PATTERN.length << 1);
59 int outputWidth = Math.max(width, fullWidth);
60 int outputHeight = Math.max(1, height);
62 int multiple = outputWidth / fullWidth;
63 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
65 BitMatrix output = new BitMatrix(outputWidth, outputHeight);
66 for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
67 if (code[inputX] == 1) {
68 output.setRegion(outputX, 0, multiple, outputHeight);
76 * Appends the given pattern to the target array starting at pos.
79 * starting color - 0 for white, 1 for black
80 * @return the number of elements added to target.
82 protected static int appendPattern(byte[] target, int pos, int[] pattern, int startColor) {
83 if (startColor != 0 && startColor != 1) {
84 throw new IllegalArgumentException(
85 "startColor must be either 0 or 1, but got: " + startColor);
88 byte color = (byte) startColor;
90 for (int i = 0; i < pattern.length; i++) {
91 for (int j = 0; j < pattern[i]; j++) {
96 color ^= 1; // flip color after each segment
101 /** @return a byte array of horizontal pixels (0 = white, 1 = black) */
102 public abstract byte[] encode(String contents);