1 18 package org.apache.batik.svggen; 19 20 import java.awt.geom.AffineTransform ; 21 import java.awt.image.BufferedImage ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import org.apache.batik.ext.awt.image.codec.ImageEncoder; 26 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder; 27 import org.apache.batik.util.Base64EncoderStream; 28 import org.w3c.dom.Element ; 29 30 31 39 public class CachedImageHandlerBase64Encoder extends DefaultCachedImageHandler { 40 43 public CachedImageHandlerBase64Encoder() { 44 super(); 45 setImageCacher(new ImageCacher.Embedded()); 46 } 47 48 53 public Element createElement(SVGGeneratorContext generatorContext) { 54 Element imageElement = 57 generatorContext.getDOMFactory().createElementNS( 58 SVG_NAMESPACE_URI, SVG_USE_TAG); 59 60 return imageElement; 61 } 62 63 64 public String getRefPrefix(){ 65 return ""; 66 } 67 68 73 protected AffineTransform handleTransform(Element imageElement, 74 double x, double y, 75 double srcWidth, 76 double srcHeight, 77 double dstWidth, 78 double dstHeight, 79 SVGGeneratorContext generatorContext) { 80 81 84 AffineTransform af = new AffineTransform (); 85 double hRatio = dstWidth / srcWidth; 86 double vRatio = dstHeight / srcHeight; 87 88 af.translate(x,y); 89 90 if(hRatio != 1 || vRatio != 1) { 91 af.scale(hRatio, vRatio); 92 } 93 94 if (!af.isIdentity()){ 95 return af; 96 } else { 97 return null; 98 } 99 } 100 101 104 public void encodeImage(BufferedImage buf, OutputStream os) 105 throws IOException { 106 Base64EncoderStream b64Encoder = new Base64EncoderStream(os); 107 ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null); 108 encoder.encode(buf); 109 b64Encoder.close(); 110 } 111 112 public int getBufferedImageType(){ 113 return BufferedImage.TYPE_INT_ARGB; 114 } 115 } 116 117 | Popular Tags |