KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > ImageHandlerJPEGEncoder


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.svggen;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.image.BufferedImage JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 import com.sun.image.codec.jpeg.JPEGCodec;
28 import com.sun.image.codec.jpeg.JPEGEncodeParam;
29 import com.sun.image.codec.jpeg.JPEGImageEncoder;
30
31 /**
32  * This implementation of the abstract AbstractImageHandlerEncoder
33  * class creates JPEG images in the image directory and sets the
34  * url pointing to that file in the xlink:href attributes of the
35  * image elements it handles.
36  *
37  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
38  * @version $Id: ImageHandlerJPEGEncoder.java,v 1.12 2004/08/18 07:14:59 vhardy Exp $
39  * @see org.apache.batik.svggen.SVGGraphics2D
40  * @see org.apache.batik.svggen.ImageHandlerJPEGEncoder
41  * @see org.apache.batik.svggen.ImageHandlerPNGEncoder
42  */

43 public class ImageHandlerJPEGEncoder extends AbstractImageHandlerEncoder {
44     /**
45      * @param imageDir directory where this handler should generate images.
46      * If null, an IllegalArgumentException is thrown.
47      * @param urlRoot root for the urls that point to images created by this
48      * image handler. If null, then the url corresponding to imageDir
49      * is used.
50      */

51     public ImageHandlerJPEGEncoder(String JavaDoc imageDir, String JavaDoc urlRoot)
52         throws SVGGraphics2DIOException {
53         super(imageDir, urlRoot);
54     }
55
56     /**
57      * @return the suffix used by this encoder. E.g., ".jpg" for
58      * ImageHandlerJPEGEncoder
59      */

60     public final String JavaDoc getSuffix() {
61         return ".jpg";
62     }
63
64     /**
65      * @return the prefix used by this encoder. E.g., "jpegImage" for
66      * ImageHandlerJPEGEncoder
67      */

68     public final String JavaDoc getPrefix() {
69         return "jpegImage";
70     }
71
72     /**
73      * Derived classes should implement this method and encode the input
74      * BufferedImage as needed
75      */

76     public void encodeImage(BufferedImage JavaDoc buf, File JavaDoc imageFile)
77         throws SVGGraphics2DIOException {
78         try{
79             OutputStream JavaDoc os = new FileOutputStream JavaDoc(imageFile);
80             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
81             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
82             param.setQuality(1, false);
83             encoder.encode(buf, param);
84             os.flush();
85             os.close();
86         } catch(IOException JavaDoc e) {
87             throw new SVGGraphics2DIOException(ERR_WRITE+imageFile.getName());
88         }
89     }
90
91     /**
92      * This method creates a BufferedImage of the right size and type
93      * for the derived class.
94      */

95     public BufferedImage JavaDoc buildBufferedImage(Dimension JavaDoc size) {
96         return new BufferedImage JavaDoc(size.width, size.height,
97                                  BufferedImage.TYPE_INT_RGB);
98     }
99 }
100
Popular Tags