1 18 package org.apache.batik.transcoder.image; 19 20 import java.awt.Color ; 21 import java.awt.Graphics2D ; 22 import java.awt.image.BufferedImage ; 23 24 import java.io.ByteArrayOutputStream ; 25 26 import org.apache.batik.transcoder.TranscoderInput; 27 import org.apache.batik.transcoder.TranscoderOutput; 28 import org.apache.batik.dom.svg.SVGDOMImplementation; 29 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.DOMImplementation ; 33 34 40 public class DOMTest extends AbstractImageTranscoderTest { 41 42 45 public DOMTest() { 46 } 47 48 51 protected TranscoderInput createTranscoderInput() { 52 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 53 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 54 Document doc = impl.createDocument(svgNS, "svg", null); 55 56 Element root = doc.getDocumentElement(); 57 58 root.setAttributeNS(null, "width", "400"); 59 root.setAttributeNS(null, "height", "400"); 60 61 Element r = doc.createElementNS(svgNS, "rect"); 62 r.setAttributeNS(null, "x", "0"); 63 r.setAttributeNS(null, "y", "0"); 64 r.setAttributeNS(null, "width", "400"); 65 r.setAttributeNS(null, "height", "400"); 66 r.setAttributeNS(null, "style", "fill:black"); 67 root.appendChild(r); 68 69 r = doc.createElementNS(svgNS, "rect"); 70 r.setAttributeNS(null, "x", "100"); 71 r.setAttributeNS(null, "y", "50"); 72 r.setAttributeNS(null, "width", "100"); 73 r.setAttributeNS(null, "height", "50"); 74 r.setAttributeNS(null, "style", "stroke:red; fill:none"); 75 root.appendChild(r); 76 77 return new TranscoderInput(doc); 78 } 79 80 83 protected byte [] getReferenceImageData() { 84 try { 85 BufferedImage img = new BufferedImage 86 (400, 400, BufferedImage.TYPE_INT_ARGB); 87 Graphics2D g2d = img.createGraphics(); 88 g2d.setColor(Color.black); 89 g2d.fillRect(0, 0, 400, 400); 90 g2d.setColor(Color.red); 91 g2d.drawRect(100, 50, 100, 50); 92 ByteArrayOutputStream ostream = new ByteArrayOutputStream (); 93 PNGTranscoder t = new PNGTranscoder(); 94 TranscoderOutput output = new TranscoderOutput(ostream); 95 t.writeImage(img, output); 96 return ostream.toByteArray(); 97 } catch (Exception ex) { 98 throw new RuntimeException ("DOMTest error"); 99 } 100 } 101 } 102 | Popular Tags |