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.WriterException;
21 import com.google.zxing.common.BitMatrix;
23 import java.util.Hashtable;
26 * This object renders an EAN8 code as a {@link BitMatrix}.
28 * @author aripollak@gmail.com (Ari Pollak)
30 public final class EAN8Writer extends UPCEANWriter {
32 private static final int codeWidth = 3 + // start guard
33 (7 * 4) + // left bars
35 (7 * 4) + // right bars
38 public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
39 Hashtable hints) throws WriterException {
40 if (format != BarcodeFormat.EAN_8) {
41 throw new IllegalArgumentException("Can only encode EAN_8, but got "
45 return super.encode(contents, format, width, height, hints);
48 /** @return a byte array of horizontal pixels (0 = white, 1 = black) */
49 public byte[] encode(String contents) {
50 if (contents.length() != 8) {
51 throw new IllegalArgumentException(
52 "Requested contents should be 8 digits long, but got " + contents.length());
55 byte[] result = new byte[codeWidth];
58 pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);
60 for (int i = 0; i <= 3; i++) {
61 int digit = Integer.parseInt(contents.substring(i, i + 1));
62 pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 0);
65 pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0);
67 for (int i = 4; i <= 7; i++) {
68 int digit = Integer.parseInt(contents.substring(i, i + 1));
69 pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1);
71 pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);