1 28 package net.sf.jasperreports.engine.xml; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 33 import net.sf.jasperreports.engine.JRException; 34 import net.sf.jasperreports.engine.JRImage; 35 import net.sf.jasperreports.engine.JRImageRenderer; 36 import net.sf.jasperreports.engine.JRPrintImage; 37 38 import org.w3c.tools.codec.Base64Decoder; 39 40 41 45 public class JRPrintImageSourceObject 46 { 47 48 49 52 private JRPrintImage printImage = null; 53 54 57 private boolean isEmbedded = false; 58 59 60 63 public void setPrintImage(JRPrintImage printImage) 64 { 65 this.printImage = printImage; 66 } 67 68 69 72 public void setEmbedded(boolean isEmbedded) 73 { 74 this.isEmbedded = isEmbedded; 75 } 76 77 78 81 public void setImageSource(String imageSource) throws JRException 82 { 83 if (isEmbedded) 84 { 85 try 86 { 87 ByteArrayInputStream bais = new ByteArrayInputStream (imageSource.getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream (); 89 90 Base64Decoder decoder = new Base64Decoder(bais, baos); 91 decoder.process(); 92 93 printImage.setRenderer(JRImageRenderer.getInstance(baos.toByteArray(), JRImage.ON_ERROR_TYPE_ERROR)); 94 } 95 catch (Exception e) 96 { 97 throw new JRException("Error decoding embedded image.", e); 98 } 99 } 100 else 101 { 102 printImage.setRenderer( 103 JRImageRenderer.getInstance( 104 imageSource, 105 printImage.getOnErrorType() 106 ) 107 ); 108 } 109 } 110 111 112 } 113 | Popular Tags |