KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Canvas JavaDoc;
49 import java.awt.Color JavaDoc;
50 import java.awt.Image JavaDoc;
51 import java.awt.image.MemoryImageSource JavaDoc;
52
53 import com.lowagie.text.Rectangle;
54
55 /** Implements the Postnet and Planet barcodes. The default parameters are:
56  * <pre>
57  *n = 72f / 22f; // distance between bars
58  *x = 0.02f * 72f; // bar width
59  *barHeight = 0.125f * 72f; // height of the tall bars
60  *size = 0.05f * 72f; // height of the short bars
61  *codeType = POSTNET; // type of code
62  * </pre>
63  *
64  * @author Paulo Soares (psoares@consiste.pt)
65  */

66 public class BarcodePostnet extends Barcode{
67
68     /** The bars for each character.
69      */

70     private static final byte BARS[][] =
71     {
72         {1,1,0,0,0},
73         {0,0,0,1,1},
74         {0,0,1,0,1},
75         {0,0,1,1,0},
76         {0,1,0,0,1},
77         {0,1,0,1,0},
78         {0,1,1,0,0},
79         {1,0,0,0,1},
80         {1,0,0,1,0},
81         {1,0,1,0,0}
82     };
83     
84     /** Creates new BarcodePostnet */
85     public BarcodePostnet() {
86         n = 72f / 22f; // distance between bars
87
x = 0.02f * 72f; // bar width
88
barHeight = 0.125f * 72f; // height of the tall bars
89
size = 0.05f * 72f; // height of the short bars
90
codeType = POSTNET; // type of code
91
}
92     
93     /** Creates the bars for Postnet.
94      * @param text the code to be created without checksum
95      * @return the bars
96      */

97     public static byte[] getBarsPostnet(String JavaDoc text) {
98         int total = 0;
99         for (int k = text.length() - 1; k >= 0; --k) {
100             int n = text.charAt(k) - '0';
101             total += n;
102         }
103         text += (char)(((10 - (total % 10)) % 10) + '0');
104         byte bars[] = new byte[text.length() * 5 + 2];
105         bars[0] = 1;
106         bars[bars.length - 1] = 1;
107         for (int k = 0; k < text.length(); ++k) {
108             int c = text.charAt(k) - '0';
109             System.arraycopy(BARS[c], 0, bars, k * 5 + 1, 5);
110         }
111         return bars;
112     }
113
114     /** Gets the maximum area that the barcode and the text, if
115      * any, will occupy. The lower left corner is always (0, 0).
116      * @return the size the barcode occupies.
117      */

118     public Rectangle getBarcodeSize() {
119         float width = ((code.length() + 1) * 5 + 1) * n + x;
120         return new Rectangle(width, barHeight);
121     }
122     
123     /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
124      * barcode is always placed at coodinates (0, 0). Use the
125      * translation matrix to move it elsewhere.<p>
126      * The bars and text are written in the following colors:<p>
127      * <P><TABLE BORDER=1>
128      * <TR>
129      * <TH><P><CODE>barColor</CODE></TH>
130      * <TH><P><CODE>textColor</CODE></TH>
131      * <TH><P>Result</TH>
132      * </TR>
133      * <TR>
134      * <TD><P><CODE>null</CODE></TD>
135      * <TD><P><CODE>null</CODE></TD>
136      * <TD><P>bars and text painted with current fill color</TD>
137      * </TR>
138      * <TR>
139      * <TD><P><CODE>barColor</CODE></TD>
140      * <TD><P><CODE>null</CODE></TD>
141      * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
142      * </TR>
143      * <TR>
144      * <TD><P><CODE>null</CODE></TD>
145      * <TD><P><CODE>textColor</CODE></TD>
146      * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
147      * </TR>
148      * <TR>
149      * <TD><P><CODE>barColor</CODE></TD>
150      * <TD><P><CODE>textColor</CODE></TD>
151      * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
152      * </TR>
153      * </TABLE>
154      * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
155      * @param barColor the color of the bars. It can be <CODE>null</CODE>
156      * @param textColor the color of the text. It can be <CODE>null</CODE>
157      * @return the dimensions the barcode occupies
158      */

159     public Rectangle placeBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
160         if (barColor != null)
161             cb.setColorFill(barColor);
162         byte bars[] = getBarsPostnet(code);
163         byte flip = 1;
164         if (codeType == PLANET) {
165             flip = 0;
166             bars[0] = 0;
167             bars[bars.length - 1] = 0;
168         }
169         float startX = 0;
170         for (int k = 0; k < bars.length; ++k) {
171             cb.rectangle(startX, 0, x - inkSpreading, bars[k] == flip ? barHeight : size);
172             startX += n;
173         }
174         cb.fill();
175         return getBarcodeSize();
176     }
177     
178     /** Creates a <CODE>java.awt.Image</CODE>. This image only
179      * contains the bars without any text.
180      * @param foreground the color of the bars
181      * @param background the color of the background
182      * @return the image
183      *
184      */

185     public java.awt.Image JavaDoc createAwtImage(Color JavaDoc foreground, Color JavaDoc background) {
186         int f = foreground.getRGB();
187         int g = background.getRGB();
188         Canvas JavaDoc canvas = new Canvas JavaDoc();
189         int barWidth = (int)x;
190         if (barWidth <= 0)
191             barWidth = 1;
192         int barDistance = (int)n;
193         if (barDistance <= barWidth)
194             barDistance = barWidth + 1;
195         int barShort = (int)size;
196         if (barShort <= 0)
197             barShort = 1;
198         int barTall = (int)barHeight;
199         if (barTall <= barShort)
200             barTall = barShort + 1;
201         int width = ((code.length() + 1) * 5 + 1) * barDistance + barWidth;
202         int pix[] = new int[width * barTall];
203         byte bars[] = getBarsPostnet(code);
204         byte flip = 1;
205         if (codeType == PLANET) {
206             flip = 0;
207             bars[0] = 0;
208             bars[bars.length - 1] = 0;
209         }
210         int idx = 0;
211         for (int k = 0; k < bars.length; ++k) {
212             boolean dot = (bars[k] == flip);
213             for (int j = 0; j < barDistance; ++j) {
214                 pix[idx + j] = ((dot && j < barWidth) ? f : g);
215             }
216             idx += barDistance;
217         }
218         int limit = width * (barTall - barShort);
219         for (int k = width; k < limit; k += width)
220             System.arraycopy(pix, 0, pix, k, width);
221         idx = limit;
222         for (int k = 0; k < bars.length; ++k) {
223             for (int j = 0; j < barDistance; ++j) {
224                 pix[idx + j] = ((j < barWidth) ? f : g);
225             }
226             idx += barDistance;
227         }
228         for (int k = limit + width; k < pix.length; k += width)
229             System.arraycopy(pix, limit, pix, k, width);
230         Image JavaDoc img = canvas.createImage(new MemoryImageSource JavaDoc(width, barTall, pix, 0, width));
231         
232         return img;
233     }
234 }
Popular Tags