1
2 package net.sourceforge.barbecue;
3
4 import net.sourceforge.barbecue.env.DefaultEnvironment;
5 import net.sourceforge.barbecue.output.SVGOutput;
6 import net.sourceforge.barbecue.output.EPSOutput;
7
8 import java.io.OutputStream;
9 import java.io.FileOutputStream;
10 import java.io.OutputStreamWriter;
11
12 /**
13 Provide a command line interface to simulate GNU barcode. Options available are
14
15 <h2>Command Line Options</h2>
16 <ul>
17 <li> -b barcode_data. [Required] Specify the barcode data. If the EAN128 encoding is
18 specified, then this data will be parsed (See EAN128 parsing below).
19 <li> -o filename. Write the barcode output to the specified file.
20 <li> -e encoding. Use the specified encoding for the barcode.<br>
21 Options available are:
22 <ul><li> 'ean128'
23 <li> 'code128'
24 </ul>
25 <li> -E. Write the output as Encapsulated Postscript (EPS). The default
26 output format is a PNG image.
27 <li> -label label_file. Write the human readable text output to the specified file.
28 This is useful for situations where barbecue calculates a check digit.
29 </ul>
30
31 <h2>EAN 128 Parsing</h2>
32 Barbecue will parse barcode data if the encoding is specified as ean128. The
33 application identifiers should be enclosed in round brackets. For example:<br>
34 <pre>(01)0941919600001(10)012004(21)000001</pre>
35 Specifies a GTIN (application id 01) with a lot number of 012004 and an item number
36 of 000001.
37
38 <h2>Examples</h2>
39 <pre>java -jar barbecue.jar -e ean128 -b "(01)0941919600001(10)012004(21)000001" -E -o test.eps<pre>
40 */
41 public class Main
42 {
43 public static void main(String[] args)
44 {
45 String barcode_text = null;
46 String encoding = null;
47 String outfile = null;
48 String label_outfile = null;
49
50 boolean output_EPS = false;;
51
52 if (args.length == 0)
53 {
54 Usage();
55 }
56
57 int i=0;
58 while (i<args.length)
59 {
60 String command = args[i++];
61
62 if (command.equals("-b"))
63 barcode_text = args[i++];
64 if (command.equals("-e"))
65 encoding = args[i++];
66 if (command.equals("-o"))
67 outfile = args[i++];
68 if (command.equals("-E"))
69 output_EPS = true;
70 if (command.equals("-label"))
71 label_outfile = args[i++];
72 }
73
74 if (null == barcode_text)
75 {
76 System.err.println("Some barcode text must be specified with the -b option");
77 Usage();
78 return;
79 }
80 try
81 {
82 Barcode barcode = null;
83
84 if (encoding.equals("ean128"))
85 {
86 barcode = BarcodeFactory.parseEAN128(barcode_text);
87 }
88 else if (encoding.equals("code128"))
89 {
90 barcode = BarcodeFactory.createCode128(barcode_text);
91 }
92 else if (encoding.equals("upca"))
93 {
94 barcode = BarcodeFactory.createUPCA(barcode_text);
95 }
96 else if (encoding.equals("codabar"))
97 {
98 barcode = BarcodeFactory.createCodabar(barcode_text);
99 }
100 else if (encoding.equals("ean13"))
101 {
102 barcode = BarcodeFactory.createEAN13(barcode_text);
103 }
104 else
105 {
106 System.err.println("Unknown encoding: " + encoding);
107 }
108
109 if (null == barcode)
110 {
111 return;
112 }
113
114 OutputStream fos = null;
115 if (null != outfile)
116 {
117 fos = new FileOutputStream(outfile);
118 }
119 else
120 {
121 fos = System.out;
122 }
123
124 if (output_EPS)
125 {
126 outputEPS(barcode, fos);
127 }
128 else
129 {
130 outputPNG(barcode, fos);
131 }
132
133 if (null != label_outfile)
134 {
135 OutputStreamWriter pw = new OutputStreamWriter(new FileOutputStream(label_outfile));
136 pw.write(barcode.getLabel());
137 pw.flush();
138 pw.close();
139 }
140 }
141 catch (Exception e)
142 {
143 e.printStackTrace();
144 }
145 }
146
147 public static void Usage()
148 {
149 System.err.println("Barbecue Barcode generator");
150 System.err.println("Usage:");
151 System.err.println("java -jar barbecue.jar -b <barcode text> -e <encoding> -o <outfile> {-E}");
152 System.err.println("\tAvailable Encodings are:");
153 System.err.println("\t\t \"ean128\"");
154 System.err.println("\t-E: Write output as EPS.");
155 System.err.println("\t-label <filename>: Write human readable (including check digit) text to the file.");
156 System.err.println("\t-o <filename>: Write the barcode image to the file.");
157 System.err.println("Example:\n\tjava -jar barbecue.jar -E -e ean128 -o test.eps\\ \n\t\t-label test_label.txt -b \"(01)0941919600001(10)012004(21)000123\"");
158 }
159
160 public static void outputPNG(Barcode barcode, OutputStream fos)
161 {
162 try
163 {
164 BarcodeImageHandler.writePNG(barcode, fos);
165 }
166 catch (Exception e)
167 {
168 e.printStackTrace();
169 }
170 }
171
172 public static void outputSVG(Barcode barcode,OutputStream fos)
173 {
174 try
175 {
176
177 OutputStreamWriter osw = new OutputStreamWriter(fos);
178 SVGOutput svg_out = new SVGOutput(osw, DefaultEnvironment.DEFAULT_FONT, java.awt.Color.black, java.awt.Color.white, 1, "in");
179
180 barcode.output(svg_out);
181 }
182 catch (Exception e)
183 {
184 e.printStackTrace();
185 }
186 }
187
188 public static void outputEPS(Barcode barcode, OutputStream fos)
189 {
190 try
191 {
192
193 OutputStreamWriter osw = new OutputStreamWriter(fos);
194 EPSOutput eps_out = new EPSOutput(osw);
195
196 barcode.output(eps_out);
197 }
198 catch (Exception e)
199 {
200 e.printStackTrace();
201 }
202 }
203 }