1 28 package net.sf.jasperreports.engine.util; 29 30 import java.awt.image.BufferedImage ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 34 import javax.imageio.ImageIO ; 35 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.JRRenderable; 38 39 40 44 public class JRJdk14ImageEncoder extends JRAbstractImageEncoder 45 { 46 47 48 51 public byte[] encode(BufferedImage bi, byte imageType) throws JRException 52 { 53 String formatName = null; 54 55 switch (imageType) 56 { 57 case JRRenderable.IMAGE_TYPE_GIF : 58 { 59 formatName = "gif"; 60 break; 61 } 62 case JRRenderable.IMAGE_TYPE_PNG : 63 { 64 formatName = "png"; 65 break; 66 } 67 case JRRenderable.IMAGE_TYPE_TIFF : 68 { 69 formatName = "tiff"; 70 break; 71 } 72 case JRRenderable.IMAGE_TYPE_JPEG : 73 case JRRenderable.IMAGE_TYPE_UNKNOWN : 74 default: 75 { 76 formatName = "jpeg"; 77 break; 78 } 79 } 80 81 boolean success = false; 82 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 83 84 try 85 { 86 success = ImageIO.write(bi, formatName, baos); 87 } 88 catch (IOException e) 89 { 90 throw new JRException(e); 91 } 92 93 if (!success) 94 { 95 throw new JRException("No appropriate image writer found for the \"" + formatName + "\" format."); 96 } 97 98 return baos.toByteArray(); 99 } 100 101 102 } 103 | Popular Tags |