1 18 package org.apache.batik.svggen; 19 20 import java.awt.Dimension ; 21 import java.awt.Graphics2D ; 22 import java.awt.Image ; 23 import java.awt.image.BufferedImage ; 24 import java.awt.image.RenderedImage ; 25 import java.awt.image.renderable.RenderableImage ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 import org.apache.batik.ext.awt.image.codec.ImageEncoder; 31 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder; 32 import org.apache.batik.util.Base64EncoderStream; 33 import org.w3c.dom.Element ; 34 35 46 public class ImageHandlerBase64Encoder extends DefaultImageHandler { 47 50 public ImageHandlerBase64Encoder() { 51 super(); 52 } 53 54 58 public void handleHREF(Image image, Element imageElement, 59 SVGGeneratorContext generatorContext) 60 throws SVGGraphics2DIOException { 61 if (image == null) 62 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_NULL); 63 64 int width = image.getWidth(null); 65 int height = image.getHeight(null); 66 67 if (width==0 || height==0) { 68 handleEmptyImage(imageElement); 69 } else { 70 if (image instanceof RenderedImage ) { 71 handleHREF((RenderedImage )image, imageElement, 72 generatorContext); 73 } else { 74 BufferedImage buf = 75 new BufferedImage (width, height, 76 BufferedImage.TYPE_INT_ARGB); 77 78 Graphics2D g = buf.createGraphics(); 79 g.drawImage(image, 0, 0, null); 80 g.dispose(); 81 handleHREF((RenderedImage )buf, imageElement, 82 generatorContext); 83 } 84 } 85 } 86 87 91 public void handleHREF(RenderableImage image, Element imageElement, 92 SVGGeneratorContext generatorContext) 93 throws SVGGraphics2DIOException { 94 if (image == null){ 95 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_NULL); 96 } 97 98 RenderedImage r = image.createDefaultRendering(); 99 if (r == null) { 100 handleEmptyImage(imageElement); 101 } else { 102 handleHREF(r, imageElement, generatorContext); 103 } 104 } 105 106 protected void handleEmptyImage(Element imageElement) { 107 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 108 ATTR_XLINK_HREF, DATA_PROTOCOL_PNG_PREFIX); 109 imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "0"); 110 imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "0"); 111 } 112 113 119 public void handleHREF(RenderedImage image, Element imageElement, 120 SVGGeneratorContext generatorContext) 121 throws SVGGraphics2DIOException { 122 123 ByteArrayOutputStream os = new ByteArrayOutputStream (); 127 Base64EncoderStream b64Encoder = new Base64EncoderStream(os); 128 try { 129 encodeImage(image, b64Encoder); 133 134 b64Encoder.close(); 136 } catch (IOException e) { 137 throw new SVGGraphics2DIOException(ERR_UNEXPECTED, e); 139 } 140 141 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 145 ATTR_XLINK_HREF, 146 DATA_PROTOCOL_PNG_PREFIX + 147 os.toString()); 148 149 } 150 151 public void encodeImage(RenderedImage buf, OutputStream os) 152 throws SVGGraphics2DIOException { 153 try{ 154 ImageEncoder encoder = new PNGImageEncoder(os, null); 155 encoder.encode(buf); 156 } catch(IOException e) { 157 throw new SVGGraphics2DIOException(ERR_UNEXPECTED); 159 } 160 } 161 162 166 public BufferedImage buildBufferedImage(Dimension size) { 167 return new BufferedImage (size.width, size.height, 168 BufferedImage.TYPE_INT_ARGB); 169 } 170 } 171 | Popular Tags |