Add basic Code 128, Code 39, ITF writers, per Erik
[zxing.git] / core / src / com / google / zxing / oned / Code39Writer.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 CODE39 code as a {@link BitMatrix}.
26  * 
27  * @author erik.barbara@gmail.com (Erik Barbara)
28  */
29 public final class Code39Writer extends UPCEANWriter {
30
31           public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
32               Hashtable hints) throws WriterException {
33             if (format != BarcodeFormat.CODE_39) {
34               throw new IllegalArgumentException("Can only encode CODE_39, but got " + format);
35             }
36             return super.encode(contents, format, width, height, hints);
37           }
38
39           public byte[] encode(String contents) {
40       int length = contents.length();
41             if (length > 80) {
42               throw new IllegalArgumentException(
43                   "Requested contents should be less than 80 digits long, but got " + length);
44             }
45
46       int[] widths = new int[9];
47             int codeWidth = 24 + 1 + length;
48             for (int i = 0; i < length; i++) {
49                 int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
50                 toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
51                 for(int j = 0; j < widths.length; j++) {
52           codeWidth += widths[j];
53         }
54             }
55             byte[] result = new byte[codeWidth];
56       toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
57             int pos = appendPattern(result, 0, widths, 1);
58       int[] narrowWhite = {1};
59       pos += appendPattern(result, pos, narrowWhite, 0);
60             //append next character to bytematrix
61             for(int i = length-1; i >= 0; i--) {
62                 int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
63                 toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
64                 pos += appendPattern(result, pos, widths, 1);
65                 pos += appendPattern(result, pos, narrowWhite, 0);
66             }
67       toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
68             pos += appendPattern(result, pos, widths, 1);
69             return result;
70           }
71           
72           private static void toIntArray(int a, int[] toReturn) {
73                   for (int i = 0; i < 9; i++) {
74                           int temp = a & (1 << i);
75         toReturn[i] = (temp == 0) ? 1 : 2;
76                   }
77           }
78
79 }