View Javadoc

1   // Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   // This file is part of BarcodeProcessor.
3   //
4   // BarcodeProcessor is free software; you can redistribute it and/or modify
5   // it under the terms of the GNU General Public License as published by
6   // the Free Software Foundation; either version 2 of the License, or
7   // (at your option) any later version.
8   //
9   // BarcodeProcessor is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  // GNU General Public License for more details.
13  //
14  // You should have received a copy of the GNU General Public License
15  // along with Foobar; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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          // Sanity check the size
44          if ((size < 1) || (size > 4))
45              throw new RuntimeException("Space needs to be between 1 and 4");
46          // Sanity check the order: can't have two spaces or two bars in a row
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          // Sanity check the size
55          if ((size < 1) || (size > 4))
56              throw new RuntimeException("Space needs to be between 1 and 4");
57          // Sanity check the order: can't have two spaces or two bars in a row
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  }