KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > BarcodeEAN


1 /*
2  * Copyright 2002 by Paulo Soares.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47 package com.lowagie.text.pdf;
48
49 import java.awt.Canvas JavaDoc;
50 import java.awt.Color JavaDoc;
51 import java.awt.Image JavaDoc;
52 import java.awt.image.MemoryImageSource JavaDoc;
53 import java.util.Arrays JavaDoc;
54
55 import com.lowagie.text.ExceptionConverter;
56 import com.lowagie.text.Rectangle;
57
58 /** Generates barcodes in several formats: EAN13, EAN8, UPCA, UPCE,
59  * supplemental 2 and 5. The default parameters are:
60  * <pre>
61  *x = 0.8f;
62  *font = BaseFont.createFont("Helvetica", "winansi", false);
63  *size = 8;
64  *baseline = size;
65  *barHeight = size * 3;
66  *guardBars = true;
67  *codeType = EAN13;
68  *code = "";
69  * </pre>
70  *
71  * @author Paulo Soares (psoares@consiste.pt)
72  */

73 public class BarcodeEAN extends Barcode{
74         
75     /** The bar positions that are guard bars.*/
76     private static final int GUARD_EMPTY[] = {};
77     /** The bar positions that are guard bars.*/
78     private static final int GUARD_UPCA[] = {0, 2, 4, 6, 28, 30, 52, 54, 56, 58};
79     /** The bar positions that are guard bars.*/
80     private static final int GUARD_EAN13[] = {0, 2, 28, 30, 56, 58};
81     /** The bar positions that are guard bars.*/
82     private static final int GUARD_EAN8[] = {0, 2, 20, 22, 40, 42};
83     /** The bar positions that are guard bars.*/
84     private static final int GUARD_UPCE[] = {0, 2, 28, 30, 32};
85     /** The x coordinates to place the text.*/
86     private static final float TEXTPOS_EAN13[] = {6.5f, 13.5f, 20.5f, 27.5f, 34.5f, 41.5f, 53.5f, 60.5f, 67.5f, 74.5f, 81.5f, 88.5f};
87     /** The x coordinates to place the text.*/
88     private static final float TEXTPOS_EAN8[] = {6.5f, 13.5f, 20.5f, 27.5f, 39.5f, 46.5f, 53.5f, 60.5f};
89     /** The basic bar widths.*/
90     private static final byte BARS[][] =
91     {
92         {3, 2, 1, 1}, // 0
93
{2, 2, 2, 1}, // 1
94
{2, 1, 2, 2}, // 2
95
{1, 4, 1, 1}, // 3
96
{1, 1, 3, 2}, // 4
97
{1, 2, 3, 1}, // 5
98
{1, 1, 1, 4}, // 6
99
{1, 3, 1, 2}, // 7
100
{1, 2, 1, 3}, // 8
101
{3, 1, 1, 2} // 9
102
};
103     
104     /** The total number of bars for EAN13.*/
105     private static final int TOTALBARS_EAN13 = 11 + 12 * 4;
106     /** The total number of bars for EAN8.*/
107     private static final int TOTALBARS_EAN8 = 11 + 8 * 4;
108     /** The total number of bars for UPCE.*/
109     private static final int TOTALBARS_UPCE = 9 + 6 * 4;
110     /** The total number of bars for supplemental 2.*/
111     private static final int TOTALBARS_SUPP2 = 13;
112     /** The total number of bars for supplemental 5.*/
113     private static final int TOTALBARS_SUPP5 = 31;
114     /** Marker for odd parity.*/
115     private static final int ODD = 0;
116     /** Marker for even parity.*/
117     private static final int EVEN = 1;
118     
119     /** Sequence of parities to be used with EAN13.*/
120     private static final byte PARITY13[][] =
121     {
122         {ODD, ODD, ODD, ODD, ODD, ODD}, // 0
123
{ODD, ODD, EVEN, ODD, EVEN, EVEN}, // 1
124
{ODD, ODD, EVEN, EVEN, ODD, EVEN}, // 2
125
{ODD, ODD, EVEN, EVEN, EVEN, ODD}, // 3
126
{ODD, EVEN, ODD, ODD, EVEN, EVEN}, // 4
127
{ODD, EVEN, EVEN, ODD, ODD, EVEN}, // 5
128
{ODD, EVEN, EVEN, EVEN, ODD, ODD}, // 6
129
{ODD, EVEN, ODD, EVEN, ODD, EVEN}, // 7
130
{ODD, EVEN, ODD, EVEN, EVEN, ODD}, // 8
131
{ODD, EVEN, EVEN, ODD, EVEN, ODD} // 9
132
};
133     
134     /** Sequence of parities to be used with supplemental 2.*/
135     private static final byte PARITY2[][] =
136     {
137         {ODD, ODD}, // 0
138
{ODD, EVEN}, // 1
139
{EVEN, ODD}, // 2
140
{EVEN, EVEN} // 3
141
};
142     
143     /** Sequence of parities to be used with supplemental 2.*/
144     private static final byte PARITY5[][] =
145     {
146         {EVEN, EVEN, ODD, ODD, ODD}, // 0
147
{EVEN, ODD, EVEN, ODD, ODD}, // 1
148
{EVEN, ODD, ODD, EVEN, ODD}, // 2
149
{EVEN, ODD, ODD, ODD, EVEN}, // 3
150
{ODD, EVEN, EVEN, ODD, ODD}, // 4
151
{ODD, ODD, EVEN, EVEN, ODD}, // 5
152
{ODD, ODD, ODD, EVEN, EVEN}, // 6
153
{ODD, EVEN, ODD, EVEN, ODD}, // 7
154
{ODD, EVEN, ODD, ODD, EVEN}, // 8
155
{ODD, ODD, EVEN, ODD, EVEN} // 9
156
};
157     
158     /** Sequence of parities to be used with UPCE.*/
159     private static final byte PARITYE[][] =
160     {
161         {EVEN, EVEN, EVEN, ODD, ODD, ODD}, // 0
162
{EVEN, EVEN, ODD, EVEN, ODD, ODD}, // 1
163
{EVEN, EVEN, ODD, ODD, EVEN, ODD}, // 2
164
{EVEN, EVEN, ODD, ODD, ODD, EVEN}, // 3
165
{EVEN, ODD, EVEN, EVEN, ODD, ODD}, // 4
166
{EVEN, ODD, ODD, EVEN, EVEN, ODD}, // 5
167
{EVEN, ODD, ODD, ODD, EVEN, EVEN}, // 6
168
{EVEN, ODD, EVEN, ODD, EVEN, ODD}, // 7
169
{EVEN, ODD, EVEN, ODD, ODD, EVEN}, // 8
170
{EVEN, ODD, ODD, EVEN, ODD, EVEN} // 9
171
};
172     
173     /** Creates new BarcodeEAN */
174     public BarcodeEAN() {
175         try {
176             x = 0.8f;
177             font = BaseFont.createFont("Helvetica", "winansi", false);
178             size = 8;
179             baseline = size;
180             barHeight = size * 3;
181             guardBars = true;
182             codeType = EAN13;
183             code = "";
184         }
185         catch (Exception JavaDoc e) {
186             throw new ExceptionConverter(e);
187         }
188     }
189     
190     /** Calculates the EAN parity character.
191      * @param code the code
192      * @return the parity character
193      */

194     public static int calculateEANParity(String JavaDoc code) {
195         int mul = 3;
196         int total = 0;
197         for (int k = code.length() - 1; k >= 0; --k) {
198             int n = code.charAt(k) - '0';
199             total += mul * n;
200             mul ^= 2;
201         }
202         return (10 - (total % 10)) % 10;
203     }
204     
205     /** Converts an UPCA code into an UPCE code. If the code can not
206      * be converted a <CODE>null</CODE> is returned.
207      * @param text the code to convert. It must have 12 numeric characters
208      * @return the 8 converted digits or <CODE>null</CODE> if the
209      * code could not be converted
210      */

211     static public String JavaDoc convertUPCAtoUPCE(String JavaDoc text) {
212         if (text.length() != 12 || !(text.startsWith("0") || text.startsWith("1")))
213             return null;
214         if (text.substring(3, 6).equals("000") || text.substring(3, 6).equals("100")
215             || text.substring(3, 6).equals("200")) {
216                 if (text.substring(6, 8).equals("00"))
217                     return text.substring(0, 1) + text.substring(1, 3) + text.substring(8, 11) + text.substring(3, 4) + text.substring(11);
218         }
219         else if (text.substring(4, 6).equals("00")) {
220             if (text.substring(6, 9).equals("000"))
221                 return text.substring(0, 1) + text.substring(1, 4) + text.substring(9, 11) + "3" + text.substring(11);
222         }
223         else if (text.substring(5, 6).equals("0")) {
224             if (text.substring(6, 10).equals("0000"))
225                 return text.substring(0, 1) + text.substring(1, 5) + text.substring(10, 11) + "4" + text.substring(11);
226         }
227         else if (text.charAt(10) >= '5') {
228             if (text.substring(6, 10).equals("0000"))
229                 return text.substring(0, 1) + text.substring(1, 6) + text.substring(10, 11) + text.substring(11);
230         }
231         return null;
232     }
233     
234     /** Creates the bars for the barcode EAN13 and UPCA.
235      * @param _code the text with 13 digits
236      * @return the barcode
237      */

238     public static byte[] getBarsEAN13(String JavaDoc _code) {
239         int code[] = new int[_code.length()];
240         for (int k = 0; k < code.length; ++k)
241             code[k] = _code.charAt(k) - '0';
242         byte bars[] = new byte[TOTALBARS_EAN13];
243         int pb = 0;
244         bars[pb++] = 1;
245         bars[pb++] = 1;
246         bars[pb++] = 1;
247         byte sequence[] = PARITY13[code[0]];
248         for (int k = 0; k < sequence.length; ++k) {
249             int c = code[k + 1];
250             byte stripes[] = BARS[c];
251             if (sequence[k] == ODD) {
252                 bars[pb++] = stripes[0];
253                 bars[pb++] = stripes[1];
254                 bars[pb++] = stripes[2];
255                 bars[pb++] = stripes[3];
256             }
257             else {
258                 bars[pb++] = stripes[3];
259                 bars[pb++] = stripes[2];
260                 bars[pb++] = stripes[1];
261                 bars[pb++] = stripes[0];
262             }
263         }
264         bars[pb++] = 1;
265         bars[pb++] = 1;
266         bars[pb++] = 1;
267         bars[pb++] = 1;
268         bars[pb++] = 1;
269         for (int k = 7; k < 13; ++k) {
270             int c = code[k];
271             byte stripes[] = BARS[c];
272             bars[pb++] = stripes[0];
273             bars[pb++] = stripes[1];
274             bars[pb++] = stripes[2];
275             bars[pb++] = stripes[3];
276         }
277         bars[pb++] = 1;
278         bars[pb++] = 1;
279         bars[pb++] = 1;
280         return bars;
281     }
282     
283     /** Creates the bars for the barcode EAN8.
284      * @param _code the text with 8 digits
285      * @return the barcode
286      */

287     public static byte[] getBarsEAN8(String JavaDoc _code) {
288         int code[] = new int[_code.length()];
289         for (int k = 0; k < code.length; ++k)
290             code[k] = _code.charAt(k) - '0';
291         byte bars[] = new byte[TOTALBARS_EAN8];
292         int pb = 0;
293         bars[pb++] = 1;
294         bars[pb++] = 1;
295         bars[pb++] = 1;
296         for (int k = 0; k < 4; ++k) {
297             int c = code[k];
298             byte stripes[] = BARS[c];
299             bars[pb++] = stripes[0];
300             bars[pb++] = stripes[1];
301             bars[pb++] = stripes[2];
302             bars[pb++] = stripes[3];
303         }
304         bars[pb++] = 1;
305         bars[pb++] = 1;
306         bars[pb++] = 1;
307         bars[pb++] = 1;
308         bars[pb++] = 1;
309         for (int k = 4; k < 8; ++k) {
310             int c = code[k];
311             byte stripes[] = BARS[c];
312             bars[pb++] = stripes[0];
313             bars[pb++] = stripes[1];
314             bars[pb++] = stripes[2];
315             bars[pb++] = stripes[3];
316         }
317         bars[pb++] = 1;
318         bars[pb++] = 1;
319         bars[pb++] = 1;
320         return bars;
321     }
322     
323     /** Creates the bars for the barcode UPCE.
324      * @param _code the text with 8 digits
325      * @return the barcode
326      */

327     public static byte[] getBarsUPCE(String JavaDoc _code) {
328         int code[] = new int[_code.length()];
329         for (int k = 0; k < code.length; ++k)
330             code[k] = _code.charAt(k) - '0';
331         byte bars[] = new byte[TOTALBARS_UPCE];
332         boolean flip = (code[0] != 0);
333         int pb = 0;
334         bars[pb++] = 1;
335         bars[pb++] = 1;
336         bars[pb++] = 1;
337         byte sequence[] = PARITYE[code[code.length - 1]];
338         for (int k = 1; k < code.length - 1; ++k) {
339             int c = code[k];
340             byte stripes[] = BARS[c];
341             if (sequence[k - 1] == (flip ? EVEN : ODD)) {
342                 bars[pb++] = stripes[0];
343                 bars[pb++] = stripes[1];
344                 bars[pb++] = stripes[2];
345                 bars[pb++] = stripes[3];
346             }
347             else {
348                 bars[pb++] = stripes[3];
349                 bars[pb++] = stripes[2];
350                 bars[pb++] = stripes[1];
351                 bars[pb++] = stripes[0];
352             }
353         }
354         bars[pb++] = 1;
355         bars[pb++] = 1;
356         bars[pb++] = 1;
357         bars[pb++] = 1;
358         bars[pb++] = 1;
359         bars[pb++] = 1;
360         return bars;
361     }
362
363     /** Creates the bars for the barcode supplemental 2.
364      * @param _code the text with 2 digits
365      * @return the barcode
366      */

367     public static byte[] getBarsSupplemental2(String JavaDoc _code) {
368         int code[] = new int[2];
369         for (int k = 0; k < code.length; ++k)
370             code[k] = _code.charAt(k) - '0';
371         byte bars[] = new byte[TOTALBARS_SUPP2];
372         int pb = 0;
373         int parity = (code[0] * 10 + code[1]) % 4;
374         bars[pb++] = 1;
375         bars[pb++] = 1;
376         bars[pb++] = 2;
377         byte sequence[] = PARITY2[parity];
378         for (int k = 0; k < sequence.length; ++k) {
379             if (k == 1) {
380                 bars[pb++] = 1;
381                 bars[pb++] = 1;
382             }
383             int c = code[k];
384             byte stripes[] = BARS[c];
385             if (sequence[k] == ODD) {
386                 bars[pb++] = stripes[0];
387                 bars[pb++] = stripes[1];
388                 bars[pb++] = stripes[2];
389                 bars[pb++] = stripes[3];
390             }
391             else {
392                 bars[pb++] = stripes[3];
393                 bars[pb++] = stripes[2];
394                 bars[pb++] = stripes[1];
395                 bars[pb++] = stripes[0];
396             }
397         }
398         return bars;
399     }
400
401     /** Creates the bars for the barcode supplemental 5.
402      * @param _code the text with 5 digits
403      * @return the barcode
404      */

405     public static byte[] getBarsSupplemental5(String JavaDoc _code) {
406         int code[] = new int[5];
407         for (int k = 0; k < code.length; ++k)
408             code[k] = _code.charAt(k) - '0';
409         byte bars[] = new byte[TOTALBARS_SUPP5];
410         int pb = 0;
411         int parity = (((code[0] + code[2] + code[4]) * 3) + ((code[1] + code[3]) * 9)) % 10;
412         bars[pb++] = 1;
413         bars[pb++] = 1;
414         bars[pb++] = 2;
415         byte sequence[] = PARITY5[parity];
416         for (int k = 0; k < sequence.length; ++k) {
417             if (k != 0) {
418                 bars[pb++] = 1;
419                 bars[pb++] = 1;
420             }
421             int c = code[k];
422             byte stripes[] = BARS[c];
423             if (sequence[k] == ODD) {
424                 bars[pb++] = stripes[0];
425                 bars[pb++] = stripes[1];
426                 bars[pb++] = stripes[2];
427                 bars[pb++] = stripes[3];
428             }
429             else {
430                 bars[pb++] = stripes[3];
431                 bars[pb++] = stripes[2];
432                 bars[pb++] = stripes[1];
433                 bars[pb++] = stripes[0];
434             }
435         }
436         return bars;
437     }
438     
439     /** Gets the maximum area that the barcode and the text, if
440      * any, will occupy. The lower left corner is always (0, 0).
441      * @return the size the barcode occupies.
442      */

443     public Rectangle getBarcodeSize() {
444         float width = 0;
445         float height = barHeight;
446         if (font != null) {
447             if (baseline <= 0)
448                 height += -baseline + size;
449             else
450                 height += baseline - font.getFontDescriptor(BaseFont.DESCENT, size);
451         }
452         switch (codeType) {
453             case EAN13:
454                 width = x * (11 + 12 * 7);
455                 if (font != null) {
456                     width += font.getWidthPoint(code.charAt(0), size);
457                 }
458                 break;
459             case EAN8:
460                 width = x * (11 + 8 * 7);
461                 break;
462             case UPCA:
463                 width = x * (11 + 12 * 7);
464                 if (font != null) {
465                     width += font.getWidthPoint(code.charAt(0), size) + font.getWidthPoint(code.charAt(11), size);
466                 }
467                 break;
468             case UPCE:
469                 width = x * (9 + 6 * 7);
470                 if (font != null) {
471                     width += font.getWidthPoint(code.charAt(0), size) + font.getWidthPoint(code.charAt(7), size);
472                 }
473                 break;
474             case SUPP2:
475                 width = x * (6 + 2 * 7);
476                 break;
477             case SUPP5:
478                 width = x * (4 + 5 * 7 + 4 * 2);
479                 break;
480             default:
481                 throw new RuntimeException JavaDoc("Invalid code type.");
482         }
483         return new Rectangle(width, height);
484     }
485     
486     /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
487      * barcode is always placed at coodinates (0, 0). Use the
488      * translation matrix to move it elsewhere.<p>
489      * The bars and text are written in the following colors:<p>
490      * <P><TABLE BORDER=1>
491      * <TR>
492      * <TH><P><CODE>barColor</CODE></TH>
493      * <TH><P><CODE>textColor</CODE></TH>
494      * <TH><P>Result</TH>
495      * </TR>
496      * <TR>
497      * <TD><P><CODE>null</CODE></TD>
498      * <TD><P><CODE>null</CODE></TD>
499      * <TD><P>bars and text painted with current fill color</TD>
500      * </TR>
501      * <TR>
502      * <TD><P><CODE>barColor</CODE></TD>
503      * <TD><P><CODE>null</CODE></TD>
504      * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
505      * </TR>
506      * <TR>
507      * <TD><P><CODE>null</CODE></TD>
508      * <TD><P><CODE>textColor</CODE></TD>
509      * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
510      * </TR>
511      * <TR>
512      * <TD><P><CODE>barColor</CODE></TD>
513      * <TD><P><CODE>textColor</CODE></TD>
514      * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
515      * </TR>
516      * </TABLE>
517      * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
518      * @param barColor the color of the bars. It can be <CODE>null</CODE>
519      * @param textColor the color of the text. It can be <CODE>null</CODE>
520      * @return the dimensions the barcode occupies
521      */

522     public Rectangle placeBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
523         Rectangle rect = getBarcodeSize();
524         float barStartX = 0;
525         float barStartY = 0;
526         float textStartY = 0;
527         if (font != null) {
528             if (baseline <= 0)
529                 textStartY = barHeight - baseline;
530             else {
531                 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size);
532                 barStartY = textStartY + baseline;
533             }
534         }
535         switch (codeType) {
536             case EAN13:
537             case UPCA:
538             case UPCE:
539                 if (font != null)
540                     barStartX += font.getWidthPoint(code.charAt(0), size);
541                 break;
542         }
543         byte bars[] = null;
544         int guard[] = GUARD_EMPTY;
545         switch (codeType) {
546             case EAN13:
547                 bars = getBarsEAN13(code);
548                 guard = GUARD_EAN13;
549                 break;
550             case EAN8:
551                 bars = getBarsEAN8(code);
552                 guard = GUARD_EAN8;
553                 break;
554             case UPCA:
555                 bars = getBarsEAN13("0" + code);
556                 guard = GUARD_UPCA;
557                 break;
558             case UPCE:
559                 bars = getBarsUPCE(code);
560                 guard = GUARD_UPCE;
561                 break;
562             case SUPP2:
563                 bars = getBarsSupplemental2(code);
564                 break;
565             case SUPP5:
566                 bars = getBarsSupplemental5(code);
567                 break;
568         }
569         float keepBarX = barStartX;
570         boolean print = true;
571         float gd = 0;
572         if (font != null && baseline > 0 && guardBars) {
573             gd = baseline / 2;
574         }
575         if (barColor != null)
576             cb.setColorFill(barColor);
577         for (int k = 0; k < bars.length; ++k) {
578             float w = bars[k] * x;
579             if (print) {
580                 if (Arrays.binarySearch(guard, k) >= 0)
581                     cb.rectangle(barStartX, barStartY - gd, w - inkSpreading, barHeight + gd);
582                 else
583                     cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight);
584             }
585             print = !print;
586             barStartX += w;
587         }
588         cb.fill();
589         if (font != null) {
590             if (textColor != null)
591                 cb.setColorFill(textColor);
592             cb.beginText();
593             cb.setFontAndSize(font, size);
594             switch (codeType) {
595                 case EAN13:
596                     cb.setTextMatrix(0, textStartY);
597                     cb.showText(code.substring(0, 1));
598                     for (int k = 1; k < 13; ++k) {
599                         String JavaDoc c = code.substring(k, k + 1);
600                         float len = font.getWidthPoint(c, size);
601                         float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2;
602                         cb.setTextMatrix(pX, textStartY);
603                         cb.showText(c);
604                     }
605                     break;
606                 case EAN8:
607                     for (int k = 0; k < 8; ++k) {
608                         String JavaDoc c = code.substring(k, k + 1);
609                         float len = font.getWidthPoint(c, size);
610                         float pX = TEXTPOS_EAN8[k] * x - len / 2;
611                         cb.setTextMatrix(pX, textStartY);
612                         cb.showText(c);
613                     }
614                     break;
615                 case UPCA:
616                     cb.setTextMatrix(0, textStartY);
617                     cb.showText(code.substring(0, 1));
618                     for (int k = 1; k < 11; ++k) {
619                         String JavaDoc c = code.substring(k, k + 1);
620                         float len = font.getWidthPoint(c, size);
621                         float pX = keepBarX + TEXTPOS_EAN13[k] * x - len / 2;
622                         cb.setTextMatrix(pX, textStartY);
623                         cb.showText(c);
624                     }
625                     cb.setTextMatrix(keepBarX + x * (11 + 12 * 7), textStartY);
626                     cb.showText(code.substring(11, 12));
627                     break;
628                 case UPCE:
629                     cb.setTextMatrix(0, textStartY);
630                     cb.showText(code.substring(0, 1));
631                     for (int k = 1; k < 7; ++k) {
632                         String JavaDoc c = code.substring(k, k + 1);
633                         float len = font.getWidthPoint(c, size);
634                         float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2;
635                         cb.setTextMatrix(pX, textStartY);
636                         cb.showText(c);
637                     }
638                     cb.setTextMatrix(keepBarX + x * (9 + 6 * 7), textStartY);
639                     cb.showText(code.substring(7, 8));
640                     break;
641                 case SUPP2:
642                 case SUPP5:
643                     for (int k = 0; k < code.length(); ++k) {
644                         String JavaDoc c = code.substring(k, k + 1);
645                         float len = font.getWidthPoint(c, size);
646                         float pX = (7.5f + (9 * k)) * x - len / 2;
647                         cb.setTextMatrix(pX, textStartY);
648                         cb.showText(c);
649                     }
650                     break;
651             }
652             cb.endText();
653         }
654         return rect;
655     }
656     
657     /** Creates a <CODE>java.awt.Image</CODE>. This image only
658      * contains the bars without any text.
659      * @param foreground the color of the bars
660      * @param background the color of the background
661      * @return the image
662      */

663     public java.awt.Image JavaDoc createAwtImage(Color JavaDoc foreground, Color JavaDoc background) {
664         int f = foreground.getRGB();
665         int g = background.getRGB();
666         Canvas JavaDoc canvas = new Canvas JavaDoc();
667
668         int width = 0;
669         byte bars[] = null;
670         switch (codeType) {
671             case EAN13:
672                 bars = getBarsEAN13(code);
673                 width = 11 + 12 * 7;
674                 break;
675             case EAN8:
676                 bars = getBarsEAN8(code);
677                 width = 11 + 8 * 7;
678                 break;
679             case UPCA:
680                 bars = getBarsEAN13("0" + code);
681                 width = 11 + 12 * 7;
682                 break;
683             case UPCE:
684                 bars = getBarsUPCE(code);
685                 width = 9 + 6 * 7;
686                 break;
687             case SUPP2:
688                 bars = getBarsSupplemental2(code);
689                 width = 6 + 2 * 7;
690                 break;
691             case SUPP5:
692                 bars = getBarsSupplemental5(code);
693                 width = 4 + 5 * 7 + 4 * 2;
694                 break;
695             default:
696                 throw new RuntimeException JavaDoc("Invalid code type.");
697         }
698
699         boolean print = true;
700         int ptr = 0;
701         int height = (int)barHeight;
702         int pix[] = new int[width * height];
703         for (int k = 0; k < bars.length; ++k) {
704             int w = bars[k];
705             int c = g;
706             if (print)
707                 c = f;
708             print = !print;
709             for (int j = 0; j < w; ++j)
710                 pix[ptr++] = c;
711         }
712         for (int k = width; k < pix.length; k += width) {
713             System.arraycopy(pix, 0, pix, k, width);
714         }
715         Image JavaDoc img = canvas.createImage(new MemoryImageSource JavaDoc(width, height, pix, 0, width));
716         
717         return img;
718     }
719 }
720
Popular Tags