Add basic Code 128, Code 39, ITF writers, per Erik
[zxing.git] / core / src / com / google / zxing / oned / ITFWriter.java
1 /*
2  * Copyright 2010 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 import com.google.zxing.BarcodeFormat;
21 import com.google.zxing.WriterException;
22 import com.google.zxing.common.BitMatrix;
23
24 /**
25  * This object renders a ITF code as a {@link BitMatrix}.
26  * 
27  * @author erik.barbara@gmail.com (Erik Barbara)
28  */
29 public final class ITFWriter extends UPCEANWriter {
30
31           public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
32               Hashtable hints) throws WriterException {
33             if (format != BarcodeFormat.ITF) {
34               throw new IllegalArgumentException("Can only encode ITF, but got " + format);
35             }
36             
37             return super.encode(contents, format, width, height, hints);
38           }
39
40           public byte[] encode(String contents) {
41       int length = contents.length();
42             if (length > 80) {
43               throw new IllegalArgumentException(
44                   "Requested contents should be less than 80 digits long, but got " + length);
45             }
46             byte[] result = new byte[9 + 9 * length];
47             int[] start = {1, 1, 1, 1};
48       int pos = appendPattern(result, 0, start, 1);
49             for (int i = 0; i < length; i += 2) {
50         int one = Character.digit(contents.charAt(i), 10);
51         int two = Character.digit(contents.charAt(i+1), 10);
52                 int[] encoding = new int[18];
53                 for (int j = 0; j < 10; j += 2) {
54                         encoding[j] = ITFReader.PATTERNS[one][j];
55                         encoding[j + 1] = ITFReader.PATTERNS[two][j];
56                 }
57                 pos += appendPattern(result, pos, encoding, 1);
58             }
59       int[] end = {3, 1, 1};
60       pos += appendPattern(result, pos, end, 1);
61
62             return result;
63           }
64 }