1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.ninjasoft.barcodeprocessor;
18
19 import java.util.*;
20
21 /***
22 * Class to hold parsed barcode image data. Supports not just narrow/wide,
23 * but 1x, 2x, 3x, and 4x.
24 *
25 * @author enigma
26 */
27 public class BarcodeData {
28 private Vector data = new Vector();
29
30 public void addNarrowSpace() {
31 addSpace(1);
32 }
33 public void addWideSpace() {
34 addSpace(2);
35 }
36 public void addNarrowBar() {
37 addBar(1);
38 }
39 public void addWideBar() {
40 addBar(2);
41 }
42 public void addSpace(int size) throws RuntimeException {
43
44 if ((size < 1) || (size > 4))
45 throw new RuntimeException("Space needs to be between 1 and 4");
46
47 if (this.data.size() > 0) {
48 if (((String) data.get(data.size() - 1)).endsWith("s"))
49 throw new RuntimeException("Can't have two spaces in a row");
50 }
51 this.data.add(size + "s");
52 }
53 public void addBar(int size) throws RuntimeException {
54
55 if ((size < 1) || (size > 4))
56 throw new RuntimeException("Space needs to be between 1 and 4");
57
58 if (this.data.size() > 0) {
59 if (((String) data.get(data.size() - 1)).endsWith("b"))
60 throw new RuntimeException("Can't have two spaces in a row");
61 }
62 data.add(size + "b");
63 }
64 public String toString() {
65 StringBuffer result = new StringBuffer();
66 for (Iterator i = this.data.iterator(); i.hasNext(); ) {
67 result.append(i.next().toString());
68 result.append(" ");
69 }
70 return result.toString();
71 }
72 public String toCompactString() {
73 StringBuffer result = new StringBuffer();
74 for (Iterator i = this.data.iterator(); i.hasNext(); ) {
75 String element = (String) i.next();
76 if (element.startsWith("1"))
77 result.append(".");
78 else
79 result.append("-");
80 }
81 result.append("\n");
82 boolean bar = ((String) this.data.get(0)).endsWith("b");
83 for (int i=0; i<this.data.size(); i++) {
84 result.append(bar ? "|" : " ");
85 bar = !bar;
86 }
87 result.append("\n");
88 return result.toString();
89
90 }
91 }