Issue 198
[zxing.git] / core / src / com / google / zxing / oned / EAN13Writer.java
1 /*
2  * Copyright 2009 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.oned;
18
19 import java.util.Hashtable;
20
21 import com.google.zxing.BarcodeFormat;
22 import com.google.zxing.WriterException;
23 import com.google.zxing.common.ByteMatrix;
24
25
26 /**
27  * This object renders an EAN13 code as a ByteMatrix 2D array of greyscale
28  * values.
29  * 
30  * @author aripollak@gmail.com (Ari Pollak)
31  */
32 public final class EAN13Writer extends AbstractUPCEANWriter {
33
34   private static final int codeWidth = 3 + // start guard
35       (7 * 6) + // left bars
36       5 + // middle guard
37       (7 * 6) + // right bars
38       3; // end guard
39
40   public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,
41       Hashtable hints) throws WriterException {
42     if (format != BarcodeFormat.EAN_13) {
43       throw new IllegalArgumentException("Can only encode EAN_13, but got " + format);
44     }
45     
46     return super.encode(contents, format, width, height, hints);
47   }
48
49   public byte[] encode(String contents) {
50     if (contents.length() != 13) {
51       throw new IllegalArgumentException(
52           "Requested contents should be 13 digits long, but got " + contents.length());
53     }
54
55     int firstDigit = Integer.parseInt(contents.substring(0, 1));
56     int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];
57     byte[] result = new byte[codeWidth];
58     int pos = 0;
59
60     pos += appendPattern(result, pos, AbstractUPCEANReader.START_END_PATTERN, 1);
61
62     // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded
63     for (int i = 1; i <= 6; i++) {
64       int digit = Integer.parseInt(contents.substring(i, i + 1));
65       if ((parities >> (6 - i) & 1) == 1) {
66         digit += 10;
67       }
68       pos += appendPattern(result, pos, AbstractUPCEANReader.L_AND_G_PATTERNS[digit], 0);
69     }
70
71     pos += appendPattern(result, pos, AbstractUPCEANReader.MIDDLE_PATTERN, 0);
72
73     for (int i = 7; i <= 12; i++) {
74       int digit = Integer.parseInt(contents.substring(i, i + 1));
75       pos += appendPattern(result, pos, AbstractUPCEANReader.L_PATTERNS[digit], 1);
76     }
77     pos += appendPattern(result, pos, AbstractUPCEANReader.START_END_PATTERN, 1);
78
79     return result;
80   }
81
82 }