1 28 package net.sf.jasperreports.engine.util; 29 30 import java.awt.Image ; 31 import java.awt.image.BufferedImage ; 32 import java.io.File ; 33 import java.io.InputStream ; 34 import java.net.URL ; 35 import java.net.URLStreamHandlerFactory ; 36 37 import net.sf.jasperreports.engine.JRException; 38 import net.sf.jasperreports.engine.JRRenderable; 39 40 41 45 public class JRImageLoader 46 { 47 48 49 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 str_NO_IMAGE = "net/sf/jasperreports/engine/images/noimage.GIF"; 58 private static final String str_SUBREPORT_IMAGE = "net/sf/jasperreports/engine/images/subreport.GIF"; 59 private static final String str_CHART_IMAGE = "net/sf/jasperreports/engine/images/chart.GIF"; 60 private static final String str_CROSSTAB_IMAGE = "net/sf/jasperreports/engine/images/crosstab.GIF"; 61 private static Image img_NO_IMAGE = null; 62 private static Image img_SUBREPORT_IMAGE = null; 63 private static Image img_CHART_IMAGE = null; 64 private static Image img_CROSSTAB_IMAGE = null; 65 66 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 clazz = JRClassLoader.loadClassForName("net.sf.jasperreports.engine.util.JRJdk14ImageReader"); 80 imageReader = (JRImageReader) clazz.newInstance(); 81 } 82 catch (Exception e) 83 { 84 imageReader = new JRJdk13ImageReader(); 85 } 86 87 try 88 { 89 JRClassLoader.loadClassForName("javax.imageio.ImageIO"); 90 91 Class clazz = JRClassLoader.loadClassForName("net.sf.jasperreports.engine.util.JRJdk14ImageEncoder"); 92 imageEncoder = (JRImageEncoder) clazz.newInstance(); 93 } 94 catch (Exception e) 95 { 96 imageEncoder = new JRDefaultImageEncoder(); 97 } 98 } 99 100 101 104 public static byte[] loadImageDataFromFile(File file) throws JRException 105 { 106 return JRLoader.loadBytes(file); 107 } 108 109 110 113 public static byte[] loadImageDataFromURL(URL url) throws JRException 114 { 115 return JRLoader.loadBytes(url); 116 } 117 118 119 122 public static byte[] loadImageDataFromInputStream(InputStream is) throws JRException 123 { 124 return JRLoader.loadBytes(is); 125 } 126 127 128 131 public static byte[] loadImageDataFromLocation(String location) throws JRException 132 { 133 return JRLoader.loadBytesFromLocation(location); 134 } 135 136 139 public static byte[] loadImageDataFromLocation(String location, ClassLoader classLoader) throws JRException 140 { 141 return JRLoader.loadBytesFromLocation(location, classLoader); 142 } 143 144 147 public static byte[] loadImageDataFromLocation( 148 String location, 149 ClassLoader classLoader, 150 URLStreamHandlerFactory urlHandlerFactory 151 ) throws JRException 152 { 153 return JRLoader.loadBytesFromLocation(location, classLoader, urlHandlerFactory); 154 } 155 156 157 164 public static byte[] loadImageDataFromAWTImage(Image image, byte imageType) throws JRException 165 { 166 return imageEncoder.encode(image, imageType); 167 } 168 169 170 175 public static byte[] loadImageDataFromAWTImage(BufferedImage bi) throws JRException 176 { 177 return loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_JPEG); 178 } 179 180 181 186 public static byte[] loadImageDataFromAWTImage(Image image) throws JRException 187 { 188 return loadImageDataFromAWTImage(image, JRRenderable.IMAGE_TYPE_JPEG); 189 } 190 191 192 195 public static Image getImage(byte index) throws JRException 196 { 197 Image 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 246 public static Image loadImage(byte[] bytes) throws JRException 247 { 248 return imageReader.readImage(bytes); 249 } 250 251 252 258 protected static Image loadImage(String image) throws JRException 259 { 260 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 261 URL url = classLoader.getResource(image); 262 if (url == null) 263 { 264 classLoader = JRImageLoader.class.getClassLoader(); 271 } 272 InputStream 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 |