KickJava   Java API By Example, From Geeks To Geeks.

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


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.image.BufferedImage JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import com.sun.image.codec.jpeg.JPEGCodec;
25 import com.sun.image.codec.jpeg.JPEGEncodeParam;
26 import com.sun.image.codec.jpeg.JPEGImageEncoder;
27
28 /**
29  * GenericImageHandler which caches JPEG images.
30  *
31  * @author <a HREF="mailto:paul_evenblij@compuware.com">Paul Evenblij</a>
32  * @version $Id: CachedImageHandlerJPEGEncoder.java,v 1.5 2004/08/18 07:14:59 vhardy Exp $
33  */

34 public class CachedImageHandlerJPEGEncoder extends DefaultCachedImageHandler {
35     public static final String JavaDoc CACHED_JPEG_PREFIX = "jpegImage";
36     public static final String JavaDoc CACHED_JPEG_SUFFIX = ".jpg";
37
38     protected String JavaDoc refPrefix = "";
39      
40     /**
41      * @param imageDir directory where this handler should generate images.
42      * If null, an IllegalArgumentException is thrown.
43      * @param urlRoot root for the urls that point to images created by this
44      * image handler. If null, then the url corresponding to imageDir
45      * is used.
46      */

47     public CachedImageHandlerJPEGEncoder(String JavaDoc imageDir, String JavaDoc urlRoot)
48         throws SVGGraphics2DIOException {
49         refPrefix = urlRoot + "/";
50         setImageCacher(new ImageCacher.External(imageDir,
51                                                 CACHED_JPEG_PREFIX,
52                                                 CACHED_JPEG_SUFFIX));
53     }
54    
55     /**
56      * Uses JPEG encoding.
57      */

58     public void encodeImage(BufferedImage JavaDoc buf, OutputStream JavaDoc os)
59         throws IOException JavaDoc {
60         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
61         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
62         param.setQuality(1, false);
63         encoder.encode(buf, param);
64     }
65
66     public int getBufferedImageType(){
67         return BufferedImage.TYPE_INT_RGB;
68     }
69
70     public String JavaDoc getRefPrefix(){
71         return refPrefix;
72     }
73 }
74
Popular Tags