KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > image > JPEGTranscoder


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.transcoder.image;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.image.BufferedImage JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 import org.apache.batik.transcoder.TranscoderException;
26 import org.apache.batik.transcoder.TranscoderOutput;
27 import org.apache.batik.transcoder.TranscodingHints;
28 import org.apache.batik.transcoder.image.resources.Messages;
29
30 import com.sun.image.codec.jpeg.JPEGCodec;
31 import com.sun.image.codec.jpeg.JPEGEncodeParam;
32 import com.sun.image.codec.jpeg.JPEGImageEncoder;
33
34 /**
35  * This class is an <tt>ImageTranscoder</tt> that produces a JPEG image.
36  *
37  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
38  * @version $Id: JPEGTranscoder.java,v 1.11 2005/03/27 08:58:36 cam Exp $
39  */

40 public class JPEGTranscoder extends ImageTranscoder {
41
42     /**
43      * Constructs a new transcoder that produces jpeg images.
44      */

45     public JPEGTranscoder() {
46         hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);
47     }
48
49     /**
50      * Creates a new ARGB image with the specified dimension.
51      * @param width the image width in pixels
52      * @param height the image height in pixels
53      */

54     public BufferedImage JavaDoc createImage(int width, int height) {
55         return new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_RGB);
56     }
57
58     /**
59      * Writes the specified image to the specified output.
60      * @param img the image to write
61      * @param output the output where to store the image
62      * @throws TranscoderException if an error occured while storing the image
63      */

64     public void writeImage(BufferedImage JavaDoc img, TranscoderOutput output)
65             throws TranscoderException {
66         OutputStream JavaDoc ostream = output.getOutputStream();
67         // The outputstream wrapper protects the JPEG encoder from
68
// exceptions due to stream closings. If it gets an exception
69
// it nulls out the stream and just ignores any future calls.
70
ostream = new OutputStreamWrapper(ostream);
71
72         if (ostream == null) {
73             throw new TranscoderException(
74                 Messages.formatMessage("jpeg.badoutput", null));
75         }
76
77         try {
78             float quality;
79             if (hints.containsKey(KEY_QUALITY)) {
80                 quality = ((Float JavaDoc)hints.get(KEY_QUALITY)).floatValue();
81             } else {
82                 TranscoderException te;
83                 te = new TranscoderException
84                     (Messages.formatMessage("jpeg.unspecifiedQuality", null));
85                 handler.error(te);
86                 quality = .75f;
87             }
88
89             JPEGImageEncoder jpegEncoder;
90             JPEGEncodeParam params;
91             jpegEncoder = JPEGCodec.createJPEGEncoder(ostream);
92             params = JPEGCodec.getDefaultJPEGEncodeParam(img);
93             params.setQuality(quality, true);
94
95             float PixSzMM = userAgent.getPixelUnitToMillimeter();
96             int PixSzInch = (int)(25.4/PixSzMM+0.5);
97             params.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
98             params.setXDensity(PixSzInch);
99             params.setYDensity(PixSzInch);
100             jpegEncoder.encode(img, params);
101             ostream.flush();
102         } catch (IOException JavaDoc ex) {
103             throw new TranscoderException(ex);
104         }
105     }
106
107     // --------------------------------------------------------------------
108
// Keys definition
109
// --------------------------------------------------------------------
110

111     /**
112      * The encoder quality factor key.
113      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
114      * <TR>
115      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
116      * <TD VALIGN="TOP">KEY_QUALITY</TD></TR>
117      * <TR>
118      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
119      * <TD VALIGN="TOP">Float (between 0 and 1)</TD></TR>
120      * <TR>
121      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
122      * <TD VALIGN="TOP">1 (no lossy)</TD></TR>
123      * <TR>
124      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
125      * <TD VALIGN="TOP">Recommended</TD></TR>
126      * <TR>
127      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
128      * <TD VALIGN="TOP">Specify the JPEG image encoding quality.</TD></TR>
129      * </TABLE>
130      */

131     public static final TranscodingHints.Key KEY_QUALITY
132         = new QualityKey();
133
134     /**
135      * A transcoding Key represented the JPEG image quality.
136      */

137     private static class QualityKey extends TranscodingHints.Key {
138         public boolean isCompatibleValue(Object JavaDoc v) {
139             if (v instanceof Float JavaDoc) {
140                 float q = ((Float JavaDoc)v).floatValue();
141                 return (q > 0 && q <= 1f);
142             } else {
143                 return false;
144             }
145         }
146     }
147
148     /**
149      * This class will never throw an IOException, instead it eats
150      * them and then ignores any future calls to it's interface.
151      */

152     private static class OutputStreamWrapper extends OutputStream JavaDoc {
153         OutputStream JavaDoc os;
154         /**
155          * Constructs a wrapper around <tt>os</tt> that will not throw
156          * IOExceptions.
157          * <@param os>The Stream to wrap.
158          */

159         OutputStreamWrapper(OutputStream JavaDoc os) {
160             this.os = os;
161         }
162
163         public void close() throws IOException JavaDoc {
164             if (os == null) return;
165             try {
166                 os.close();
167             } catch (IOException JavaDoc ioe) {
168                 os = null;
169             }
170         }
171
172         public void flush() throws IOException JavaDoc {
173             if (os == null) return;
174             try {
175                 os.flush();
176             } catch (IOException JavaDoc ioe) {
177                 os = null;
178             }
179         }
180
181         public void write(byte[] b) throws IOException JavaDoc {
182             if (os == null) return;
183             try {
184                 os.write(b);
185             } catch (IOException JavaDoc ioe) {
186                 os = null;
187             }
188         }
189         
190         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
191             if (os == null) return;
192             try {
193                 os.write(b, off, len);
194             } catch (IOException JavaDoc ioe) {
195                 os = null;
196             }
197         }
198         
199         public void write(int b) throws IOException JavaDoc {
200             if (os == null) return;
201             try {
202                 os.write(b);
203             } catch (IOException JavaDoc ioe) {
204                 os = null;
205             }
206         }
207     }
208 }
209
Popular Tags