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 net.sf.jasperreports.engine.JRException; 35 import net.sf.jasperreports.engine.JRRenderable; 36 37 import com.keypoint.PngEncoderB; 38 import com.sun.image.codec.jpeg.JPEGCodec; 39 import com.sun.image.codec.jpeg.JPEGEncodeParam; 40 import com.sun.image.codec.jpeg.JPEGImageEncoder; 41 42 43 47 public class JRDefaultImageEncoder extends JRAbstractImageEncoder 48 { 49 50 51 54 public byte[] encode(BufferedImage bi, byte imageType) throws JRException 55 { 56 byte[] bytes = null; 57 58 switch (imageType) 59 { 60 case JRRenderable.IMAGE_TYPE_PNG : 61 { 62 bytes = new PngEncoderB(bi).pngEncode(); 63 break; 64 } 65 case JRRenderable.IMAGE_TYPE_JPEG : 66 { 67 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 68 69 try 70 { 71 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos); 72 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); 73 param.setQuality(1f, true); encoder.encode(bi, param); 75 } 76 catch (IOException e) 77 { 78 throw new JRException(e); 79 } 80 81 bytes = baos.toByteArray(); 82 break; 83 } 84 case JRRenderable.IMAGE_TYPE_GIF : 85 case JRRenderable.IMAGE_TYPE_TIFF : 86 case JRRenderable.IMAGE_TYPE_UNKNOWN : 87 default: 88 { 89 throw new JRException("Image type \"" + imageType + "\" not supported by this image encoder."); 90 } 91 } 92 93 return bytes; 94 } 95 96 97 } 98 | Popular Tags |