001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.util;
029:
030: import java.awt.Image;
031: import java.awt.image.BufferedImage;
032: import java.io.File;
033: import java.io.InputStream;
034: import java.net.URL;
035: import java.net.URLStreamHandlerFactory;
036:
037: import net.sf.jasperreports.engine.JRException;
038: import net.sf.jasperreports.engine.JRRenderable;
039:
040: /**
041: * @author Teodor Danciu (teodord@users.sourceforge.net)
042: * @version $Id: JRImageLoader.java 1712 2007-04-30 18:04:51Z teodord $
043: */
044: public class JRImageLoader {
045:
046: /**
047: *
048: */
049: public static final byte NO_IMAGE = 1;
050: public static final byte SUBREPORT_IMAGE = 2;
051: public static final byte CHART_IMAGE = 3;
052: public static final byte CROSSTAB_IMAGE = 4;
053:
054: private static final String str_NO_IMAGE = "net/sf/jasperreports/engine/images/noimage.GIF";
055: private static final String str_SUBREPORT_IMAGE = "net/sf/jasperreports/engine/images/subreport.GIF";
056: private static final String str_CHART_IMAGE = "net/sf/jasperreports/engine/images/chart.GIF";
057: private static final String str_CROSSTAB_IMAGE = "net/sf/jasperreports/engine/images/crosstab.GIF";
058: private static Image img_NO_IMAGE = null;
059: private static Image img_SUBREPORT_IMAGE = null;
060: private static Image img_CHART_IMAGE = null;
061: private static Image img_CROSSTAB_IMAGE = null;
062:
063: /**
064: *
065: */
066: private static JRImageReader imageReader = null;
067: private static JRImageEncoder imageEncoder = null;
068:
069: static {
070: try {
071: JRClassLoader.loadClassForRealName("javax.imageio.ImageIO");
072:
073: Class clazz = JRClassLoader
074: .loadClassForRealName("net.sf.jasperreports.engine.util.JRJdk14ImageReader");
075: imageReader = (JRImageReader) clazz.newInstance();
076: } catch (Exception e) {
077: imageReader = new JRJdk13ImageReader();
078: }
079:
080: try {
081: JRClassLoader.loadClassForRealName("javax.imageio.ImageIO");
082:
083: Class clazz = JRClassLoader
084: .loadClassForRealName("net.sf.jasperreports.engine.util.JRJdk14ImageEncoder");
085: imageEncoder = (JRImageEncoder) clazz.newInstance();
086: } catch (Exception e) {
087: imageEncoder = new JRDefaultImageEncoder();
088: }
089: }
090:
091: /**
092: * @deprecated Replaced by {@link JRLoader#loadBytes(File)}.
093: */
094: public static byte[] loadImageDataFromFile(File file)
095: throws JRException {
096: return JRLoader.loadBytes(file);
097: }
098:
099: /**
100: * @deprecated Replaced by {@link JRLoader#loadBytes(URL)}.
101: */
102: public static byte[] loadImageDataFromURL(URL url)
103: throws JRException {
104: return JRLoader.loadBytes(url);
105: }
106:
107: /**
108: * @deprecated Replaced by {@link JRLoader#loadBytes(InputStream)}.
109: */
110: public static byte[] loadImageDataFromInputStream(InputStream is)
111: throws JRException {
112: return JRLoader.loadBytes(is);
113: }
114:
115: /**
116: * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String)}.
117: */
118: public static byte[] loadImageDataFromLocation(String location)
119: throws JRException {
120: return JRLoader.loadBytesFromLocation(location);
121: }
122:
123: /**
124: * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String, ClassLoader)}.
125: */
126: public static byte[] loadImageDataFromLocation(String location,
127: ClassLoader classLoader) throws JRException {
128: return JRLoader.loadBytesFromLocation(location, classLoader);
129: }
130:
131: /**
132: * @deprecated Replaced by {@link JRLoader#loadBytesFromLocation(String, ClassLoader, URLStreamHandlerFactory)}.
133: */
134: public static byte[] loadImageDataFromLocation(String location,
135: ClassLoader classLoader,
136: URLStreamHandlerFactory urlHandlerFactory)
137: throws JRException {
138: return JRLoader.loadBytesFromLocation(location, classLoader,
139: urlHandlerFactory);
140: }
141:
142: /**
143: * Encoding the image object using an image encoder that supports the supplied image type.
144: *
145: * @param image the java.awt.Image object to encode
146: * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface
147: * @return the encoded image data
148: */
149: public static byte[] loadImageDataFromAWTImage(Image image,
150: byte imageType) throws JRException {
151: return imageEncoder.encode(image, imageType);
152: }
153:
154: /**
155: * Encodes the image object using an image encoder that supports the JRRenderable.IMAGE_TYPE_JPEG image type.
156: *
157: * @deprecated Replaced by {@link JRImageLoader#loadImageDataFromAWTImage(Image, byte)}.
158: */
159: public static byte[] loadImageDataFromAWTImage(BufferedImage bi)
160: throws JRException {
161: return loadImageDataFromAWTImage(bi,
162: JRRenderable.IMAGE_TYPE_JPEG);
163: }
164:
165: /**
166: * Encodes the image object using an image encoder that supports the JRRenderable.IMAGE_TYPE_JPEG image type.
167: *
168: * @deprecated Replaced by {@link JRImageLoader#loadImageDataFromAWTImage(Image, byte)}.
169: */
170: public static byte[] loadImageDataFromAWTImage(Image image)
171: throws JRException {
172: return loadImageDataFromAWTImage(image,
173: JRRenderable.IMAGE_TYPE_JPEG);
174: }
175:
176: /**
177: *
178: */
179: public static Image getImage(byte index) throws JRException {
180: Image image = null;
181:
182: switch (index) {
183: case NO_IMAGE: {
184: if (img_NO_IMAGE == null) {
185: img_NO_IMAGE = loadImage(str_NO_IMAGE);
186: }
187: image = img_NO_IMAGE;
188: break;
189: }
190: case SUBREPORT_IMAGE: {
191: if (img_SUBREPORT_IMAGE == null) {
192: img_SUBREPORT_IMAGE = loadImage(str_SUBREPORT_IMAGE);
193: }
194: image = img_SUBREPORT_IMAGE;
195: break;
196: }
197: case CHART_IMAGE: {
198: if (img_CHART_IMAGE == null) {
199: img_CHART_IMAGE = loadImage(str_CHART_IMAGE);
200: }
201: image = img_CHART_IMAGE;
202: break;
203: }
204: case CROSSTAB_IMAGE: {
205: if (img_CROSSTAB_IMAGE == null) {
206: img_CROSSTAB_IMAGE = loadImage(str_CROSSTAB_IMAGE);
207: }
208: image = img_CROSSTAB_IMAGE;
209: break;
210: }
211: }
212:
213: return image;
214: }
215:
216: /**
217: *
218: */
219: public static Image loadImage(byte[] bytes) throws JRException {
220: return imageReader.readImage(bytes);
221: }
222:
223: /**
224: * Loads an image from an specified resource.
225: *
226: * @param image the resource name
227: * @throws JRException
228: */
229: protected static Image loadImage(String image) throws JRException {
230: ClassLoader classLoader = Thread.currentThread()
231: .getContextClassLoader();
232: URL url = classLoader.getResource(image);
233: if (url == null) {
234: //if (!wasWarning)
235: //{
236: // if (log.isWarnEnabled())
237: // log.warn("Failure using Thread.currentThread().getContextClassLoader() in JRImageLoader class. Using JRImageLoader.class.getClassLoader() instead.");
238: // wasWarning = true;
239: //}
240: classLoader = JRImageLoader.class.getClassLoader();
241: }
242: InputStream is;
243: if (classLoader == null) {
244: is = JRImageLoader.class.getResourceAsStream("/" + image);
245: } else {
246: is = classLoader.getResourceAsStream(image);
247: }
248:
249: return imageReader.readImage(JRLoader.loadBytes(is));
250: }
251:
252: }
|