Issue 537, don't return UPC-A for EAN-13 starting with 0 when UPC-A isn't allowed
[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 com.google.zxing.BarcodeFormat;
20 import com.google.zxing.WriterException;
21 import com.google.zxing.common.BitMatrix;
22
23 import java.util.Hashtable;
24
25
26 /**
27  * This object renders an EAN13 code as a {@link BitMatrix}.
28  *
29  * @author aripollak@gmail.com (Ari Pollak)
30  */
31 public final class EAN13Writer extends UPCEANWriter {
32
33   private static final int codeWidth = 3 + // start guard
34       (7 * 6) + // left bars
35       5 + // middle guard
36       (7 * 6) + // right bars
37       3; // end guard
38
39   public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
40       Hashtable hints) throws WriterException {
41     if (format != BarcodeFormat.EAN_13) {
42       throw new IllegalArgumentException("Can only encode EAN_13, but got " + format);
43     }
44
45     return super.encode(contents, format, width, height, hints);
46   }
47
48   public byte[] encode(String contents) {
49     if (contents.length() != 13) {
50       throw new IllegalArgumentException(
51           "Requested contents should be 13 digits long, but got " + contents.length());
52     }
53
54     int firstDigit = Integer.parseInt(contents.substring(0, 1));
55     int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];
56     byte[] result = new byte[codeWidth];
57     int pos = 0;
58
59     pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);
60
61     // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded
62     for (int i = 1; i <= 6; i++) {
63       int digit = Integer.parseInt(contents.substring(i, i + 1));
64       if ((parities >> (6 - i) & 1) == 1) {
65         digit += 10;
66       }
67       pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], 0);
68     }
69
70     pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0);
71
72     for (int i = 7; i <= 12; i++) {
73       int digit = Integer.parseInt(contents.substring(i, i + 1));
74       pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1);
75     }
76     pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);
77
78     return result;
79   }
80
81 }