1 28 package net.sf.jasperreports.engine; 29 30 import java.awt.Dimension ; 31 import java.awt.Graphics2D ; 32 import java.awt.Image ; 33 import java.awt.geom.Dimension2D ; 34 import java.awt.geom.Rectangle2D ; 35 import java.io.File ; 36 import java.io.InputStream ; 37 import java.lang.ref.SoftReference ; 38 import java.net.URL ; 39 import java.net.URLStreamHandlerFactory ; 40 41 import net.sf.jasperreports.engine.util.JRImageLoader; 42 import net.sf.jasperreports.engine.util.JRLoader; 43 import net.sf.jasperreports.engine.util.JRResourcesUtil; 44 import net.sf.jasperreports.engine.util.JRTypeSniffer; 45 46 47 51 public class JRImageRenderer extends JRAbstractRenderer 52 { 53 54 57 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 58 59 62 private byte[] imageData = null; 63 private String imageLocation = null; 64 private byte onErrorType = JRImage.ON_ERROR_TYPE_ERROR; 65 private byte imageType = IMAGE_TYPE_UNKNOWN; 66 67 70 private transient SoftReference awtImageRef = null; 71 72 73 76 private JRImageRenderer(byte[] imageData, byte onErrorType) 77 { 78 this.imageData = imageData; 79 this.onErrorType = onErrorType; 80 81 if(imageData != null) 82 { 83 imageType = JRTypeSniffer.getImageType(imageData); 84 } 85 86 } 87 88 89 92 private JRImageRenderer(String imageLocation, byte onErrorType) 93 { 94 this.imageLocation = imageLocation; 95 this.onErrorType = onErrorType; 96 } 97 98 99 103 public static ClassLoader getClassLoader() 104 { 105 return JRResourcesUtil.getThreadClassLoader(); 106 } 107 108 109 113 public static void setClassLoader(ClassLoader classLoader) 114 { 115 JRResourcesUtil.setThreadClassLoader(classLoader); 116 } 117 118 119 122 public static JRImageRenderer getInstance(byte[] imageData) 123 { 124 return getInstance(imageData, JRImage.ON_ERROR_TYPE_ERROR); 125 } 126 127 128 131 public static JRImageRenderer getInstance(byte[] imageData, byte onErrorType) 132 { 133 return new JRImageRenderer(imageData, onErrorType); 138 } 139 140 141 144 public static JRRenderable getInstance(String imageLocation) throws JRException 145 { 146 return getInstance(imageLocation, JRImage.ON_ERROR_TYPE_ERROR, true); 147 } 148 149 150 153 public static JRRenderable getInstance(String imageLocation, byte onErrorType) throws JRException 154 { 155 return getInstance(imageLocation, onErrorType, true); 156 } 157 158 159 162 public static JRRenderable getInstance(String imageLocation, byte onErrorType, boolean isLazy) throws JRException 163 { 164 return getInstance(imageLocation, onErrorType, isLazy, null, null); 165 } 166 167 168 public static JRRenderable getInstance( 169 String imageLocation, 170 byte onErrorType, 171 boolean isLazy, 172 ClassLoader classLoader 173 ) throws JRException 174 { 175 return getInstance(imageLocation, onErrorType, isLazy, classLoader); 176 } 177 178 181 public static JRRenderable getInstance( 182 String imageLocation, 183 byte onErrorType, 184 boolean isLazy, 185 ClassLoader classLoader, 186 URLStreamHandlerFactory urlHandlerFactory 187 ) throws JRException 188 { 189 if (imageLocation == null) 190 { 191 return null; 192 } 193 194 if (isLazy) 195 { 196 return new JRImageRenderer(imageLocation, onErrorType); 197 } 198 199 try 200 { 201 byte[] data = JRLoader.loadBytesFromLocation(imageLocation, classLoader, urlHandlerFactory); 202 return new JRImageRenderer(data, onErrorType); 203 } 204 catch (JRException e) 205 { 206 return getOnErrorRenderer(onErrorType, e); 207 } 208 } 209 210 211 214 public static JRRenderable getInstance(Image img, byte onErrorType) throws JRException 215 { 216 return getInstance(img, JRRenderable.IMAGE_TYPE_JPEG, onErrorType); 217 } 218 219 220 229 public static JRRenderable getInstance(Image image, byte imageType, byte onErrorType) throws JRException 230 { 231 try 232 { 233 return new JRImageRenderer(JRImageLoader.loadImageDataFromAWTImage(image, imageType), onErrorType); 234 } 235 catch (JRException e) 236 { 237 return getOnErrorRenderer(onErrorType, e); 238 } 239 } 240 241 242 245 public static JRRenderable getInstance(InputStream is, byte onErrorType) throws JRException 246 { 247 try 248 { 249 return new JRImageRenderer(JRLoader.loadBytes(is), onErrorType); 250 } 251 catch (JRException e) 252 { 253 return getOnErrorRenderer(onErrorType, e); 254 } 255 } 256 257 258 261 public static JRRenderable getInstance(URL url, byte onErrorType) throws JRException 262 { 263 try 264 { 265 return new JRImageRenderer(JRLoader.loadBytes(url), onErrorType); 266 } 267 catch (JRException e) 268 { 269 return getOnErrorRenderer(onErrorType, e); 270 } 271 } 272 273 274 277 public static JRRenderable getInstance(File file, byte onErrorType) throws JRException 278 { 279 try 280 { 281 return new JRImageRenderer(JRLoader.loadBytes(file), onErrorType); 282 } 283 catch (JRException e) 284 { 285 return getOnErrorRenderer(onErrorType, e); 286 } 287 } 288 289 290 293 private static JRImageRenderer getOnErrorRenderer(byte onErrorType, JRException e) throws JRException 294 { 295 JRImageRenderer renderer = null; 296 297 switch (onErrorType) 298 { 299 case JRImage.ON_ERROR_TYPE_ICON : 300 { 301 renderer = new JRImageRenderer("net/sf/jasperreports/engine/images/noimage.GIF", JRImage.ON_ERROR_TYPE_ERROR); 302 break; 304 } 305 case JRImage.ON_ERROR_TYPE_BLANK : 306 { 307 renderer = new JRImageRenderer("net/sf/jasperreports/engine/images/pixel.GIF", JRImage.ON_ERROR_TYPE_ERROR); 308 break; 309 } 310 case JRImage.ON_ERROR_TYPE_ERROR : 311 default : 312 { 313 throw e; 314 } 315 } 316 317 return renderer; 318 } 319 320 321 324 public Image getImage() throws JRException 325 { 326 Image awtImage = null; 327 if (awtImageRef == null || awtImageRef.get() == null) { 328 try 329 { 330 awtImage = JRImageLoader.loadImage(getImageData()); 331 awtImageRef = new SoftReference (awtImage); 332 } 333 catch (JRException e) 334 { 335 return getOnErrorRenderer(onErrorType, e).getImage(); 336 } 337 } 338 return (Image ) awtImageRef.get(); 339 } 340 341 342 345 public String getImageLocation() 346 { 347 return imageLocation; 348 } 349 350 351 354 public byte getType() 355 { 356 return TYPE_IMAGE; 357 } 358 359 360 public byte getImageType() { 361 return imageType; 362 } 363 364 365 366 369 public Dimension2D getDimension() throws JRException 370 { 371 Image img = getImage(); 372 return new Dimension (img.getWidth(null), img.getHeight(null)); 373 } 374 375 376 379 public byte[] getImageData() throws JRException 380 { 381 if (imageData == null) 382 { 383 try 384 { 385 imageData = JRLoader.loadBytesFromLocation(imageLocation); 386 } 387 catch (JRException e) 388 { 389 imageData = getOnErrorRenderer(onErrorType, e).getImageData(); 390 } 391 } 392 393 return imageData; 394 } 395 396 397 400 public void render(Graphics2D grx, Rectangle2D rectanle) throws JRException 401 { 402 Image img = getImage(); 403 404 grx.drawImage( 405 img, 406 (int)rectanle.getX(), 407 (int)rectanle.getY(), 408 (int)rectanle.getWidth(), 409 (int)rectanle.getHeight(), 410 null 411 ); 412 } 413 414 415 } 416 | Popular Tags |