KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRImageLoader


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.awt.Image JavaDoc;
31 import java.awt.image.BufferedImage JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.net.URLStreamHandlerFactory JavaDoc;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRRenderable;
39
40
41 /**
42  * @author Teodor Danciu (teodord@users.sourceforge.net)
43  * @version $Id: JRImageLoader.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
44  */

45 public class JRImageLoader
46 {
47
48
49     /**
50      *
51      */

52     public static final byte NO_IMAGE = 1;
53     public static final byte SUBREPORT_IMAGE = 2;
54     public static final byte CHART_IMAGE = 3;
55     public static final byte CROSSTAB_IMAGE = 4;
56
57     private static final String JavaDoc str_NO_IMAGE = "net/sf/jasperreports/engine/images/noimage.GIF";
58     private static final String JavaDoc str_SUBREPORT_IMAGE = "net/sf/jasperreports/engine/images/subreport.GIF";
59     private static final String JavaDoc str_CHART_IMAGE = "net/sf/jasperreports/engine/images/chart.GIF";
60     private static final String JavaDoc str_CROSSTAB_IMAGE = "net/sf/jasperreports/engine/images/crosstab.GIF";
61     private static Image JavaDoc img_NO_IMAGE = null;
62     private static Image JavaDoc img_SUBREPORT_IMAGE = null;
63     private static Image JavaDoc img_CHART_IMAGE = null;
64     private static Image JavaDoc img_CROSSTAB_IMAGE = null;
65
66     /**
67      *
68      */

69     private static JRImageReader imageReader = null;
70     private static JRImageEncoder imageEncoder = null;
71     
72
73     static
74     {
75         try
76         {
77             JRClassLoader.loadClassForName("javax.imageio.ImageIO");
78
79             Class JavaDoc clazz = JRClassLoader.loadClassForName("net.sf.jasperreports.engine.util.JRJdk14ImageReader");
80             imageReader = (JRImageReader) clazz.newInstance();
81         }
82         catch (Exception JavaDoc e)
83         {
84             imageReader = new JRJdk13ImageReader();
85         }
86
87         try
88         {
89             JRClassLoader.loadClassForName("javax.imageio.ImageIO");
90
91             Class JavaDoc clazz = JRClassLoader.loadClassForName("net.sf.jasperreports.engine.util.JRJdk14ImageEncoder");
92             imageEncoder = (JRImageEncoder) clazz.newInstance();
93         }
94         catch (Exception JavaDoc e)
95         {
96             imageEncoder = new JRDefaultImageEncoder();
97         }
98     }
99
100
101     /**
102      * @deprecated Replaced by {@link JRLoader#loadBytes(File)}.
103      */

104     public static byte[] loadImageDataFromFile(File JavaDoc file) throws JRException
105     {
106         return JRLoader.loadBytes(file);
107     }
108
109
110     /**
111      * @deprecated Replaced by {@link JRLoader#loadBytes(URL)}.
112      */

113     public static byte[] loadImageDataFromURL(URL JavaDoc url) throws JRException
114     {
115         return JRLoader.loadBytes(url);
116     }
117
118
119     /**
120      * @deprecated Replaced by {@link JRLoader#loadBytes(InputStream)}.
121      */

122     public static byte[] loadImageDataFromInputStream(InputStream JavaDoc is) throws JRException
123     {
124         return JRLoader.loadBytes(is);
125     }
126
127
128     /**
129      * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String)}.
130      */

131     public static byte[] loadImageDataFromLocation(String JavaDoc location) throws JRException
132     {
133         return JRLoader.loadBytesFromLocation(location);
134     }
135
136     /**
137      * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String, ClassLoader)}.
138      */

139     public static byte[] loadImageDataFromLocation(String JavaDoc location, ClassLoader JavaDoc classLoader) throws JRException
140     {
141         return JRLoader.loadBytesFromLocation(location, classLoader);
142     }
143     
144     /**
145      * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String, ClassLoader, URLStreamHandlerFactory)}.
146      */

147     public static byte[] loadImageDataFromLocation(
148         String JavaDoc location,
149         ClassLoader JavaDoc classLoader,
150         URLStreamHandlerFactory JavaDoc urlHandlerFactory
151         ) throws JRException
152     {
153         return JRLoader.loadBytesFromLocation(location, classLoader, urlHandlerFactory);
154     }
155
156
157     /**
158      * Encoding the image object using an image encoder that supports the supplied image type.
159      *
160      * @param image the java.awt.Image object to encode
161      * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface
162      * @return the encoded image data
163      */

164     public static byte[] loadImageDataFromAWTImage(Image JavaDoc image, byte imageType) throws JRException
165     {
166         return imageEncoder.encode(image, imageType);
167     }
168
169
170     /**
171      * Encodes the image object using an image encoder that supports the JRRenderable.IMAGE_TYPE_JPEG image type.
172      *
173      * @deprecated Replaced by {@link JRImageLoader#loadImageDataFromAWTImage(Image, byte)}.
174      */

175     public static byte[] loadImageDataFromAWTImage(BufferedImage JavaDoc bi) throws JRException
176     {
177         return loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_JPEG);
178     }
179
180
181     /**
182      * Encodes the image object using an image encoder that supports the JRRenderable.IMAGE_TYPE_JPEG image type.
183      *
184      * @deprecated Replaced by {@link JRImageLoader#loadImageDataFromAWTImage(Image, byte)}.
185      */

186     public static byte[] loadImageDataFromAWTImage(Image JavaDoc image) throws JRException
187     {
188         return loadImageDataFromAWTImage(image, JRRenderable.IMAGE_TYPE_JPEG);
189     }
190
191
192     /**
193      *
194      */

195     public static Image JavaDoc getImage(byte index) throws JRException
196     {
197         Image JavaDoc image = null;
198
199         switch(index)
200         {
201             case NO_IMAGE:
202             {
203                 if (img_NO_IMAGE == null)
204                 {
205                     img_NO_IMAGE = loadImage(str_NO_IMAGE);
206                 }
207                 image = img_NO_IMAGE;
208                 break;
209             }
210             case SUBREPORT_IMAGE:
211             {
212                 if (img_SUBREPORT_IMAGE == null)
213                 {
214                     img_SUBREPORT_IMAGE = loadImage(str_SUBREPORT_IMAGE);
215                 }
216                 image = img_SUBREPORT_IMAGE;
217                 break;
218             }
219             case CHART_IMAGE:
220             {
221                 if (img_CHART_IMAGE == null)
222                 {
223                     img_CHART_IMAGE = loadImage(str_CHART_IMAGE);
224                 }
225                 image = img_CHART_IMAGE;
226                 break;
227             }
228             case CROSSTAB_IMAGE:
229             {
230                 if (img_CROSSTAB_IMAGE == null)
231                 {
232                     img_CROSSTAB_IMAGE = loadImage(str_CROSSTAB_IMAGE);
233                 }
234                 image = img_CROSSTAB_IMAGE;
235                 break;
236             }
237         }
238         
239         return image;
240     }
241
242
243     /**
244      *
245      */

246     public static Image JavaDoc loadImage(byte[] bytes) throws JRException
247     {
248         return imageReader.readImage(bytes);
249     }
250
251
252     /**
253      * Loads an image from an specified resource.
254      *
255      * @param image the resource name
256      * @throws JRException
257      */

258     protected static Image JavaDoc loadImage(String JavaDoc image) throws JRException
259     {
260         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
261         URL JavaDoc url = classLoader.getResource(image);
262         if (url == null)
263         {
264             //if (!wasWarning)
265
//{
266
// if (log.isWarnEnabled())
267
// log.warn("Failure using Thread.currentThread().getContextClassLoader() in JRImageLoader class. Using JRImageLoader.class.getClassLoader() instead.");
268
// wasWarning = true;
269
//}
270
classLoader = JRImageLoader.class.getClassLoader();
271         }
272         InputStream JavaDoc is;
273         if (classLoader == null)
274         {
275             is = JRImageLoader.class.getResourceAsStream("/" + image);
276         }
277         else
278         {
279             is = classLoader.getResourceAsStream(image);
280         }
281         
282         return imageReader.readImage(JRLoader.loadBytes(is));
283     }
284
285 }
286
Popular Tags