Add basic Code 128, Code 39, ITF writers, per Erik
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 25 Mar 2010 12:49:27 +0000 (12:49 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 25 Mar 2010 12:49:27 +0000 (12:49 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1252 59b500cc-1b3d-0410-9834-0bbf25fbcc57

AUTHORS
core/src/com/google/zxing/MultiFormatWriter.java
core/src/com/google/zxing/oned/Code128Reader.java
core/src/com/google/zxing/oned/Code128Writer.java [new file with mode: 0644]
core/src/com/google/zxing/oned/Code39Reader.java
core/src/com/google/zxing/oned/Code39Writer.java [new file with mode: 0644]
core/src/com/google/zxing/oned/ITFReader.java
core/src/com/google/zxing/oned/ITFWriter.java [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
index 12ddabc..f9e7c4e 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -10,6 +10,7 @@ Christian Brunschen (Google)
 Daniel Switkin (Google)
 David Albert (Bug Labs)
 Diego Pierotto
+Erik Barbara
 Fred Lin (Anobiit)
 Hannes Erven
 hypest (Barcorama project)
index a93d07a..3ec8979 100644 (file)
 package com.google.zxing;
 
 import com.google.zxing.common.ByteMatrix;
+import com.google.zxing.oned.Code128Writer;
+import com.google.zxing.oned.Code39Writer;
 import com.google.zxing.oned.EAN13Writer;
 import com.google.zxing.oned.EAN8Writer;
+import com.google.zxing.oned.ITFWriter;
 import com.google.zxing.qrcode.QRCodeWriter;
 
 import java.util.Hashtable;
@@ -40,16 +43,23 @@ public final class MultiFormatWriter implements Writer {
   public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,
       Hashtable hints) throws WriterException {
 
+    Writer writer;
     if (format == BarcodeFormat.EAN_8) {
-      return new EAN8Writer().encode(contents, format, width, height, hints);
+      writer = new EAN8Writer();
     } else if (format == BarcodeFormat.EAN_13) {
-      return new EAN13Writer().encode(contents, format, width, height, hints);
+      writer = new EAN13Writer();
     } else if (format == BarcodeFormat.QR_CODE) {
-      return new QRCodeWriter().encode(contents, format, width, height, hints);
-    }
-    else {
+      writer = new QRCodeWriter();
+    } else if (format == BarcodeFormat.CODE_39) {
+      writer = new Code39Writer();
+    } else if (format == BarcodeFormat.CODE_128) {
+      writer = new Code128Writer();
+    } else if (format == BarcodeFormat.ITF) {
+      writer = new ITFWriter();
+    } else {
       throw new IllegalArgumentException("No encoder available for format " + format);
     }
+    return writer.encode(contents, format, width, height, hints);
   }
 
 }
index e87bd71..b41d954 100644 (file)
@@ -33,7 +33,7 @@ import java.util.Hashtable;
  */
 public final class Code128Reader extends OneDReader {
 
-  private static final int[][] CODE_PATTERNS = {
+  static final int[][] CODE_PATTERNS = {
       {2, 1, 2, 2, 2, 2}, // 0
       {2, 2, 2, 1, 2, 2},
       {2, 2, 2, 2, 2, 1},
diff --git a/core/src/com/google/zxing/oned/Code128Writer.java b/core/src/com/google/zxing/oned/Code128Writer.java
new file mode 100644 (file)
index 0000000..0c22626
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2010 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.oned;
+
+import java.util.Hashtable;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+
+/**
+ * This object renders a CODE128 code as a {@link BitMatrix}.
+ * 
+ * @author erik.barbara@gmail.com (Erik Barbara)
+ */
+public final class Code128Writer extends UPCEANWriter {
+
+         public BitMatrix encode(String contents,
+                            BarcodeFormat format,
+                            int width,
+                            int height,
+                            Hashtable hints) throws WriterException {
+           if (format != BarcodeFormat.CODE_128) {
+             throw new IllegalArgumentException("Can only encode CODE_128, but got " + format);
+           }
+           return super.encode(contents, format, width, height, hints);
+         }
+
+         public byte[] encode(String contents) {
+      int length = contents.length();
+           if (length > 80) {
+             throw new IllegalArgumentException(
+                 "Requested contents should be less than 80 digits long, but got " + length);
+           }
+
+           int codeWidth = 11 + 11 + 13; //start plus check plus stop character
+           //get total code width for this barcode
+           for (int i = 0; i < length; i++) {
+        int[] patterns = Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '];
+               for (int j = 0; j < patterns.length; j++)       {
+                       codeWidth += patterns[j];
+               }
+           }
+           byte[] result = new byte[codeWidth];
+      int pos = appendPattern(result, 0, Code128Reader.CODE_PATTERNS[104], 1);
+      int check = 104;
+           //append next character to bytematrix
+           for(int i = 0; i < length; i++) {
+        check += (contents.charAt(i) - ' ') * (i + 1);
+               pos += appendPattern(result, pos, Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '],1);
+           }
+           //compute checksum and append it along with end character and quiet space
+      check %= 103;
+           pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[check],1);
+           pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[106],1);
+
+           return result;
+         }
+
+}
\ No newline at end of file
index c997ec6..50c0ede 100644 (file)
@@ -33,7 +33,7 @@ import java.util.Hashtable;
  */
 public final class Code39Reader extends OneDReader {
 
-  private static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
+  static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
   private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
 
   /**
@@ -41,7 +41,7 @@ public final class Code39Reader extends OneDReader {
    * The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
    * with 1s representing "wide" and 0s representing narrow.
    */
-  private static final int[] CHARACTER_ENCODINGS = {
+  static final int[] CHARACTER_ENCODINGS = {
       0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9
       0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, // A-J
       0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, // K-T
diff --git a/core/src/com/google/zxing/oned/Code39Writer.java b/core/src/com/google/zxing/oned/Code39Writer.java
new file mode 100644 (file)
index 0000000..fab63b2
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2010 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.oned;
+
+import java.util.Hashtable;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+
+/**
+ * This object renders a CODE39 code as a {@link BitMatrix}.
+ * 
+ * @author erik.barbara@gmail.com (Erik Barbara)
+ */
+public final class Code39Writer extends UPCEANWriter {
+
+         public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
+             Hashtable hints) throws WriterException {
+           if (format != BarcodeFormat.CODE_39) {
+             throw new IllegalArgumentException("Can only encode CODE_39, but got " + format);
+           }
+           return super.encode(contents, format, width, height, hints);
+         }
+
+         public byte[] encode(String contents) {
+      int length = contents.length();
+           if (length > 80) {
+             throw new IllegalArgumentException(
+                 "Requested contents should be less than 80 digits long, but got " + length);
+           }
+
+      int[] widths = new int[9];
+           int codeWidth = 24 + 1 + length;
+           for (int i = 0; i < length; i++) {
+               int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
+               toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
+               for(int j = 0; j < widths.length; j++) {
+          codeWidth += widths[j];
+        }
+           }
+           byte[] result = new byte[codeWidth];
+      toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
+           int pos = appendPattern(result, 0, widths, 1);
+      int[] narrowWhite = {1};
+      pos += appendPattern(result, pos, narrowWhite, 0);
+           //append next character to bytematrix
+           for(int i = length-1; i >= 0; i--) {
+               int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i));
+               toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
+               pos += appendPattern(result, pos, widths, 1);
+               pos += appendPattern(result, pos, narrowWhite, 0);
+           }
+      toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
+           pos += appendPattern(result, pos, widths, 1);
+           return result;
+         }
+         
+         private static void toIntArray(int a, int[] toReturn) {
+                 for (int i = 0; i < 9; i++) {
+                         int temp = a & (1 << i);
+        toReturn[i] = (temp == 0) ? 1 : 2;
+                 }
+         }
+
+}
\ No newline at end of file
index 9a0a941..b380972 100644 (file)
@@ -63,7 +63,7 @@ public final class ITFReader extends OneDReader {
   /**\r
    * Patterns of Wide / Narrow lines to indicate each digit\r
    */\r
-  private static final int[][] PATTERNS = {\r
+  static final int[][] PATTERNS = {\r
       {N, N, W, W, N}, // 0\r
       {W, N, N, N, W}, // 1\r
       {N, W, N, N, W}, // 2\r
diff --git a/core/src/com/google/zxing/oned/ITFWriter.java b/core/src/com/google/zxing/oned/ITFWriter.java
new file mode 100644 (file)
index 0000000..b85a652
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2010 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.oned;
+
+import java.util.Hashtable;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+
+/**
+ * This object renders a ITF code as a {@link BitMatrix}.
+ * 
+ * @author erik.barbara@gmail.com (Erik Barbara)
+ */
+public final class ITFWriter extends UPCEANWriter {
+
+         public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
+             Hashtable hints) throws WriterException {
+           if (format != BarcodeFormat.ITF) {
+             throw new IllegalArgumentException("Can only encode ITF, but got " + format);
+           }
+           
+           return super.encode(contents, format, width, height, hints);
+         }
+
+         public byte[] encode(String contents) {
+      int length = contents.length();
+           if (length > 80) {
+             throw new IllegalArgumentException(
+                 "Requested contents should be less than 80 digits long, but got " + length);
+           }
+           byte[] result = new byte[9 + 9 * length];
+           int[] start = {1, 1, 1, 1};
+      int pos = appendPattern(result, 0, start, 1);
+           for (int i = 0; i < length; i += 2) {
+        int one = Character.digit(contents.charAt(i), 10);
+        int two = Character.digit(contents.charAt(i+1), 10);
+               int[] encoding = new int[18];
+               for (int j = 0; j < 10; j += 2) {
+                       encoding[j] = ITFReader.PATTERNS[one][j];
+                       encoding[j + 1] = ITFReader.PATTERNS[two][j];
+               }
+               pos += appendPattern(result, pos, encoding, 1);
+           }
+      int[] end = {3, 1, 1};
+      pos += appendPattern(result, pos, end, 1);
+
+           return result;
+         }
+}
\ No newline at end of file