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.geom.AffineTransform ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.RenderedImage ; 26 import java.awt.image.renderable.RenderableImage ; 27 import java.io.File ; 28 import java.lang.reflect.Method ; 29 import java.net.MalformedURLException ; 30 31 import org.w3c.dom.Element ; 32 33 47 public abstract class AbstractImageHandlerEncoder extends DefaultImageHandler { 48 private static final AffineTransform IDENTITY = new AffineTransform (); 49 50 53 private String imageDir = ""; 54 55 58 private String urlRoot = ""; 59 60 private static Method createGraphics = null; 62 private static boolean initDone = false; 63 private final static Class [] paramc = new Class [] {BufferedImage .class}; 64 private static Object [] paramo = null; 65 66 72 private static Graphics2D createGraphics(BufferedImage buf) { 73 if (!initDone) { 74 try { 75 Class clazz = Class.forName("org.apache.batik.ext.awt.image.GraphicsUtil"); 76 createGraphics = clazz.getMethod("createGraphics", paramc); 77 paramo = new Object [1]; 78 } catch (ThreadDeath td) { 79 throw td; 80 } catch (Throwable t) { 81 } finally { 83 initDone = true; 84 } 85 } 86 if (createGraphics == null) 87 return buf.createGraphics(); 88 else { 89 paramo[0] = buf; 90 Graphics2D g2d = null; 91 try { 92 g2d = (Graphics2D )createGraphics.invoke(null, paramo); 93 } catch (Exception e) { 94 } 96 return g2d; 97 } 98 } 99 100 107 public AbstractImageHandlerEncoder(String imageDir, String urlRoot) 108 throws SVGGraphics2DIOException { 109 if (imageDir == null) 110 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_DIR_NULL); 111 112 File imageDirFile = new File (imageDir); 113 if (!imageDirFile.exists()) 114 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_DIR_DOES_NOT_EXIST); 115 116 this.imageDir = imageDir; 117 if (urlRoot != null) 118 this.urlRoot = urlRoot; 119 else { 120 try{ 121 this.urlRoot = imageDirFile.toURL().toString(); 122 } catch (MalformedURLException e) { 123 throw new SVGGraphics2DIOException(ERR_CANNOT_USE_IMAGE_DIR+ 124 e.getMessage(), 125 e); 126 } 127 } 128 } 129 130 134 protected void handleHREF(Image image, Element imageElement, 135 SVGGeneratorContext generatorContext) 136 throws SVGGraphics2DIOException { 137 Dimension size = new Dimension (image.getWidth(null), 139 image.getHeight(null)); 140 BufferedImage buf = buildBufferedImage(size); 141 142 Graphics2D g = createGraphics(buf); 143 144 g.drawImage(image, 0, 0, null); 145 g.dispose(); 146 147 saveBufferedImageToFile(imageElement, buf, generatorContext); 149 } 150 151 155 protected void handleHREF(RenderedImage image, Element imageElement, 156 SVGGeneratorContext generatorContext) 157 throws SVGGraphics2DIOException { 158 Dimension size = new Dimension (image.getWidth(), image.getHeight()); 160 BufferedImage buf = buildBufferedImage(size); 161 162 Graphics2D g = createGraphics(buf); 163 164 g.drawRenderedImage(image, IDENTITY); 165 g.dispose(); 166 167 saveBufferedImageToFile(imageElement, buf, generatorContext); 169 } 170 171 175 protected void handleHREF(RenderableImage image, Element imageElement, 176 SVGGeneratorContext generatorContext) 177 throws SVGGraphics2DIOException { 178 Dimension size = new Dimension ((int)Math.ceil(image.getWidth()), 180 (int)Math.ceil(image.getHeight())); 181 BufferedImage buf = buildBufferedImage(size); 182 183 Graphics2D g = createGraphics(buf); 184 185 g.drawRenderableImage(image, IDENTITY); 186 g.dispose(); 187 188 saveBufferedImageToFile(imageElement, buf, generatorContext); 190 } 191 192 private void saveBufferedImageToFile(Element imageElement, 193 BufferedImage buf, 194 SVGGeneratorContext generatorContext) 195 throws SVGGraphics2DIOException { 196 if (generatorContext == null) 197 throw new SVGGraphics2DRuntimeException(ERR_CONTEXT_NULL); 198 199 File imageFile = null; 201 202 while (imageFile == null) { 205 String fileId = generatorContext.idGenerator.generateID(getPrefix()); 206 imageFile = new File (imageDir, fileId + getSuffix()); 207 if (imageFile.exists()) 208 imageFile = null; 209 } 210 211 encodeImage(buf, imageFile); 213 214 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 216 ATTR_XLINK_HREF, urlRoot + "/" + 217 imageFile.getName()); 218 } 219 220 224 public abstract String getSuffix(); 225 226 230 public abstract String getPrefix(); 231 232 236 public abstract void encodeImage(BufferedImage buf, File imageFile) 237 throws SVGGraphics2DIOException; 238 239 243 public abstract BufferedImage buildBufferedImage(Dimension size); 244 } 245 | Popular Tags |