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 com.google.zxing.BarcodeFormat; 20 import com.google.zxing.common.BitMatrix; 21 22 import java.util.Collection; 23 import java.util.Collections; 24 25 /** 26 * This object renders a ITF code as a {@link BitMatrix}. 27 * 28 * @author erik.barbara@gmail.com (Erik Barbara) 29 */ 30 public final class ITFWriter extends OneDimensionalCodeWriter { 31 32 private static final int[] START_PATTERN = {1, 1, 1, 1}; 33 private static final int[] END_PATTERN = {3, 1, 1}; 34 35 private static final int W = 3; // Pixel width of a 3x wide line 36 private static final int N = 1; // Pixed width of a narrow line 37 38 // See ITFReader.PATTERNS 39 40 private static final int[][] PATTERNS = { 41 {N, N, W, W, N}, // 0 42 {W, N, N, N, W}, // 1 43 {N, W, N, N, W}, // 2 44 {W, W, N, N, N}, // 3 45 {N, N, W, N, W}, // 4 46 {W, N, W, N, N}, // 5 47 {N, W, W, N, N}, // 6 48 {N, N, N, W, W}, // 7 49 {W, N, N, W, N}, // 8 50 {N, W, N, W, N} // 9 51 }; 52 53 @Override getSupportedWriteFormats()54 protected Collection<BarcodeFormat> getSupportedWriteFormats() { 55 return Collections.singleton(BarcodeFormat.ITF); 56 } 57 58 @Override encode(String contents)59 public boolean[] encode(String contents) { 60 int length = contents.length(); 61 if (length % 2 != 0) { 62 throw new IllegalArgumentException("The length of the input should be even"); 63 } 64 if (length > 80) { 65 throw new IllegalArgumentException( 66 "Requested contents should be less than 80 digits long, but got " + length); 67 } 68 69 checkNumeric(contents); 70 71 boolean[] result = new boolean[9 + 9 * length]; 72 int pos = appendPattern(result, 0, START_PATTERN, true); 73 for (int i = 0; i < length; i += 2) { 74 int one = Character.digit(contents.charAt(i), 10); 75 int two = Character.digit(contents.charAt(i + 1), 10); 76 int[] encoding = new int[10]; 77 for (int j = 0; j < 5; j++) { 78 encoding[2 * j] = PATTERNS[one][j]; 79 encoding[2 * j + 1] = PATTERNS[two][j]; 80 } 81 pos += appendPattern(result, pos, encoding, true); 82 } 83 appendPattern(result, pos, END_PATTERN, true); 84 85 return result; 86 } 87 88 } 89