KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > output > bitmap > BitmapBuilder


1 /*
2  * $Id: BitmapBuilder.java,v 1.6 2003/10/29 07:50:27 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.output.bitmap;
59
60 import java.awt.Color JavaDoc;
61 import java.awt.Graphics2D JavaDoc;
62 import java.awt.RenderingHints JavaDoc;
63 import java.awt.image.BufferedImage JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.OutputStream JavaDoc;
66
67 import org.krysalis.barcode.BarcodeDimension;
68 import org.krysalis.barcode.BarcodeGenerator;
69 import org.krysalis.barcode.output.java2d.Java2DCanvasProvider;
70 import org.krysalis.barcode.tools.UnitConv;
71
72 /**
73  * Helper class for bitmap generation.
74  *
75  * @author Jeremias Maerki
76  */

77 public class BitmapBuilder {
78
79     /**
80      * Utility class: Constructor prevents instantiating when subclassed.
81      */

82     protected BitmapBuilder() {
83         throw new UnsupportedOperationException JavaDoc();
84     }
85     
86     /**
87      * Prepares a BufferedImage to paint to.
88      * @param dim the barcode dimensions
89      * @param resolution the desired image resolution (dots per inch)
90      * @param imageType the desired image type (Values: BufferedImage.TYPE_*)
91      * @return the requested BufferedImage
92      */

93     public static BufferedImage JavaDoc prepareImage(BarcodeDimension dim,
94                         int resolution, int imageType) {
95         int bmw = UnitConv.mm2px(dim.getWidthPlusQuiet(), resolution);
96         int bmh = UnitConv.mm2px(dim.getHeightPlusQuiet(), resolution);
97         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(
98                 bmw,
99                 bmh,
100                 imageType);
101         return bi;
102     }
103
104     /**
105      * Prepares a Graphics2D object for painting on a given BufferedImage. The
106      * coordinate system is adjusted to the demands of the Java2DCanvasProvider.
107      * @param image the BufferedImage instance
108      * @param dim the barcode dimensions
109      * @param antiAlias true enables anti-aliasing
110      * @return the Graphics2D object to paint on
111      */

112     public static Graphics2D JavaDoc prepareGraphics2D(BufferedImage JavaDoc image,
113                 BarcodeDimension dim, boolean antiAlias) {
114         Graphics2D JavaDoc g2d = image.createGraphics();
115         if (antiAlias) {
116             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
117                 RenderingHints.VALUE_ANTIALIAS_ON);
118         }
119         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
120             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
121         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
122             RenderingHints.VALUE_FRACTIONALMETRICS_ON);
123         g2d.setBackground(Color.white);
124         g2d.setColor(Color.black);
125         g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
126         g2d.scale(image.getWidth() / dim.getWidthPlusQuiet(),
127                 image.getHeight() / dim.getHeightPlusQuiet());
128         return g2d;
129     }
130
131     /**
132      * Generates a barcode as bitmap image.
133      * @param bargen the BarcodeGenerator to use
134      * @param msg the message to encode
135      * @param resolution the desired image resolution (dots per inch)
136      * @return the requested BufferedImage
137      */

138     public static BufferedImage JavaDoc getImage(BarcodeGenerator bargen, String JavaDoc msg, int resolution) {
139         BarcodeDimension dim = bargen.calcDimensions(msg);
140         BufferedImage JavaDoc bi = prepareImage(dim, resolution, BufferedImage.TYPE_BYTE_GRAY);
141         Graphics2D JavaDoc g2d = prepareGraphics2D(bi, dim, true);
142         Java2DCanvasProvider provider = new Java2DCanvasProvider(g2d);
143         bargen.generateBarcode(provider, msg);
144         bi.flush();
145         return bi;
146     }
147     
148     /**
149      * Convenience method for save a bitmap to a file/OutputStream. It uses
150      * BitmapEncoderRegistry to look up a suitable BitmapEncoder.
151      * @param image image to save
152      * @param out OutputStream to write to
153      * @param mime MIME type of the desired output format (ex. "image/png")
154      * @param resolution the image resolution (dots per inch)
155      * @throws IOException In case of an I/O problem
156      * @see org.krysalis.barcode.output.bitmap.BitmapEncoderRegistry
157      */

158     public static void saveImage(BufferedImage JavaDoc image,
159                 OutputStream JavaDoc out, String JavaDoc mime, int resolution) throws IOException JavaDoc {
160         BitmapEncoder encoder = BitmapEncoderRegistry.getInstance(mime);
161         /* DEBUG
162         String[] mimes = encoder.getSupportedMIMETypes();
163         for (int i = 0; i < mimes.length; i++) {
164             System.out.println(mimes[i]);
165         }*/

166         encoder.encode(image, out, mime, resolution);
167     }
168
169     /**
170      * Generates a barcode as bitmap image file.
171      * @param bargen the BarcodeGenerator to use
172      * @param msg the message to encode
173      * @param out the OutputStream to write to
174      * @param mime MIME type of the desired output format (ex. "image/png")
175      * @param resolution the desired image resolution (dots per inch)
176      * @throws IOException In case of an I/O problem
177      */

178     public static void outputBarcodeImage(BarcodeGenerator bargen,
179                                             String JavaDoc msg,
180                                             OutputStream JavaDoc out,
181                                             String JavaDoc mime,
182                                             int resolution)
183                 throws IOException JavaDoc {
184         BufferedImage JavaDoc image = getImage(bargen, msg, resolution);
185         saveImage(image, out, mime, resolution);
186     }
187
188 }
189
Popular Tags