KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ImageIOBitmapEncoder.java,v 1.6 2003/10/31 07:57:05 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.image.BufferedImage JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.OutputStream JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import javax.imageio.IIOImage JavaDoc;
66 import javax.imageio.ImageIO JavaDoc;
67 import javax.imageio.ImageTypeSpecifier JavaDoc;
68 import javax.imageio.ImageWriter JavaDoc;
69 import javax.imageio.metadata.IIOMetadata JavaDoc;
70 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
71 import javax.imageio.stream.ImageOutputStream JavaDoc;
72
73 import org.krysalis.barcode.tools.DebugUtil;
74 import org.krysalis.barcode.tools.MimeTypes;
75 import org.krysalis.barcode.tools.UnitConv;
76
77 /**
78  * BitmapEncoder implementation using ImageIO.
79  *
80  * @author Jeremias Maerki
81  */

82 public class ImageIOBitmapEncoder implements BitmapEncoder {
83
84     /**
85      * Constructs the BitmapEncoder. The constructor checks if the ImageIO
86      * API is available so it doesn't get registered in case it's not
87      * there.
88      * @throws ClassNotFoundException if the ImageIO API is unavailable
89      */

90     public ImageIOBitmapEncoder() throws ClassNotFoundException JavaDoc {
91         Class.forName("javax.imageio.ImageIO");
92     }
93
94     /** {@inheritDoc} */
95     public String JavaDoc[] getSupportedMIMETypes() {
96         return ImageIO.getWriterMIMETypes();
97     }
98
99     /** {@inheritDoc} */
100     public void encode(BufferedImage JavaDoc image, OutputStream JavaDoc out,
101                 String JavaDoc mime, int resolution) throws IOException JavaDoc {
102
103         //Simply get first offered writer
104
Iterator JavaDoc i = ImageIO.getImageWritersByMIMEType(mime);
105         ImageWriter JavaDoc writer = (ImageWriter JavaDoc)i.next();
106         
107         //Prepare output
108
ImageOutputStream JavaDoc imout = ImageIO.createImageOutputStream(out);
109         writer.setOutput(imout);
110     
111         //Prepare metadata
112
IIOMetadata JavaDoc iiometa = setupMetadata(image, writer, mime, resolution);
113         
114         //Write image
115
IIOImage JavaDoc iioimage = new IIOImage JavaDoc(image, null, iiometa);
116         writer.write(iioimage);
117         writer.dispose();
118         imout.close();
119     }
120
121     private IIOMetadata JavaDoc setupMetadata(BufferedImage JavaDoc image, ImageWriter JavaDoc writer,
122                 String JavaDoc mime, int resolution) throws IOException JavaDoc {
123         IIOMetadata JavaDoc iiometa = writer.getDefaultImageMetadata(
124                 new ImageTypeSpecifier JavaDoc(image),
125                 writer.getDefaultWriteParam());
126         if (iiometa == null) return null; //Some JAI-codecs don't support metadata
127

128         /*
129         String[] metanames = iiometa.getMetadataFormatNames();
130         for (int j = 0; j < metanames.length; j++) System.out.println(metanames[j]);
131         */

132         final String JavaDoc stdmeta = "javax_imageio_1.0";
133         final String JavaDoc jpegmeta = "javax_imageio_jpeg_image_1.0";
134
135         if (MimeTypes.MIME_JPEG.equals(mime)
136                 && jpegmeta.equals(iiometa.getNativeMetadataFormatName())) {
137                     
138             /* JPEG gets special treatment because I believe there's a bug in
139              * the JPEG codec in ImageIO converting the pixel size incorrectly
140              * when using standard metadata format. JM, 2003-10-28
141              */

142              
143             checkWritable(iiometa);
144             
145             IIOMetadataNode JavaDoc rootnode = (IIOMetadataNode JavaDoc)iiometa.getAsTree(jpegmeta);
146             IIOMetadataNode JavaDoc variety = (IIOMetadataNode JavaDoc)rootnode.
147                     getElementsByTagName("JPEGvariety").item(0);
148             
149             IIOMetadataNode JavaDoc jfif = (IIOMetadataNode JavaDoc)variety.
150                     getElementsByTagName("app0JFIF").item(0);
151             jfif.setAttribute("resUnits", "1"); //dots per inch
152
jfif.setAttribute("Xdensity", Integer.toString(resolution));
153             jfif.setAttribute("Ydensity", Integer.toString(resolution));
154
155             //dumpMetadata(iiometa);
156
//DebugUtil.dumpNode(rootnode);
157

158             iiometa.setFromTree(jpegmeta, rootnode);
159
160             //dumpMetadata(iiometa);
161

162         } else if (iiometa.isStandardMetadataFormatSupported()) {
163             checkWritable(iiometa);
164             
165             IIOMetadataNode JavaDoc rootnode = new IIOMetadataNode JavaDoc(stdmeta);
166
167             IIOMetadataNode JavaDoc imagedim = new IIOMetadataNode JavaDoc("Dimension");
168             IIOMetadataNode JavaDoc child = new IIOMetadataNode JavaDoc("HorizontalPixelSize");
169             double effResolution = 1 / (UnitConv.in2mm(1) / resolution);
170             child.setAttribute("value", Double.toString(effResolution));
171             imagedim.appendChild(child);
172             child = new IIOMetadataNode JavaDoc("VerticalPixelSize");
173             child.setAttribute("value", Double.toString(effResolution));
174             imagedim.appendChild(child);
175
176             IIOMetadataNode JavaDoc textNode = new IIOMetadataNode JavaDoc("Text");
177             child = new IIOMetadataNode JavaDoc("TextEntry");
178             child.setAttribute("keyword", "Software");
179             child.setAttribute("value", "Krysalis Barcode");
180             child.setAttribute("encoding", "Unicode");
181             child.setAttribute("language", "en");
182             child.setAttribute("compression", "none");
183             textNode.appendChild(child);
184             
185             rootnode.appendChild(imagedim);
186             rootnode.appendChild(textNode);
187             
188             //dumpMetadata(iiometa);
189
//DebugUtil.dumpNode(rootnode);
190

191             iiometa.mergeTree(stdmeta, rootnode);
192             
193             //dumpMetadata(iiometa);
194
}
195         return iiometa;
196     }
197
198     private void checkWritable(IIOMetadata JavaDoc iiometa) throws IOException JavaDoc {
199         if (iiometa.isReadOnly()) {
200             //System.out.println("Metadata is read-only");
201
throw new IOException JavaDoc("Metadata is read-only. Cannot modify");
202         }
203     }
204
205     private void dumpMetadata(IIOMetadata JavaDoc iiometa) {
206         String JavaDoc[] metanames = iiometa.getMetadataFormatNames();
207         for (int j = 0; j < metanames.length; j++) {
208             System.out.println("--->" + metanames[j]);
209             DebugUtil.dumpNode(iiometa.getAsTree(metanames[j]));
210         }
211     }
212
213 }
214
Popular Tags