KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.batik.ext.awt.image.codec.ImageEncoder;
28 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
29
30 /**
31  * This implementation of the abstract AbstractImageHandlerEncoder
32  * class creates PNG images in the image directory and sets the
33  * url pointing to that file in the xlink:href attributes of the
34  * image elements it handles.
35  *
36  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
37  * @version $Id: ImageHandlerPNGEncoder.java,v 1.13 2004/08/18 07:14:59 vhardy Exp $
38  * @see org.apache.batik.svggen.SVGGraphics2D
39  * @see org.apache.batik.svggen.ImageHandlerJPEGEncoder
40  * @see org.apache.batik.svggen.ImageHandlerPNGEncoder
41  */

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

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

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

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

75     public void encodeImage(BufferedImage JavaDoc buf, File JavaDoc imageFile)
76         throws SVGGraphics2DIOException {
77         try {
78             OutputStream JavaDoc os = new FileOutputStream JavaDoc(imageFile);
79             ImageEncoder encoder = new PNGImageEncoder(os, null);
80             encoder.encode(buf);
81             os.close();
82         } catch (IOException JavaDoc e) {
83             throw new SVGGraphics2DIOException(ERR_WRITE+imageFile.getName());
84         }
85     }
86
87     /**
88      * This method creates a BufferedImage with an alpha channel, as this is
89      * supported by PNG.
90      */

91     public BufferedImage JavaDoc buildBufferedImage(Dimension JavaDoc size){
92         return new BufferedImage JavaDoc(size.width, size.height,
93                                  BufferedImage.TYPE_INT_ARGB);
94     }
95 }
96
Popular Tags