Re: SVGGraphics2D drawImage() NullPointerException
Thad Humphries <[email protected]> Fri, 6 May 2016 15:06:38 -0400
| Newsgroups | gmane.text.xml.batik.user |
|---|---|
| Message-ID | <CA+bF-zzz1nKh6rXV1E7Ve0KiqHaB+FjS+_T_5dVUhmwq_CcmTQ@mail.gmail.com> |
--001a1133056a84ec7f05323128db Content-Type: text/plain; charset=UTF-8 Ah! Mystery solved. See: http://stackoverflow.com/questions/16138850/batik-svggraphics2d-nullpointerexception-when-drawing-image, specifically the April 14th comment to the lone answer. This comment points out a bug in Batik 1.8 (https://issues.apache.org/jira/browse/BATIK-1146). Using Batik 1.7 fixed the problem. (Let me apologize for double post. A question I had sent the day before to the tomcat-user's list had never posted to mail-archives.apache.org, and I thought my April 30th message here was swallowed as well. As it was, it posted May 1st and I missed it.) On Fri, May 6, 2016 at 12:36 PM, Thad Humphries <[email protected]> wrote: > I am trying to create an SVG from a BufferedImage using the example at > https://xmlgraphics.apache.org/batik/using/svg-generator.html. The image > is a TIFF read from disk using Apache Commons Imaging. I can successfully > export this image to PNG. However cannot create an SVG. The code is > > public static void main(String[] args) { > try { > File inFile = new File(args[0]); > BufferedImage sourceImage = > ImageReadExample.imageReadExample(inFile); > // Imaging.writeImage(sourceImage, new File("sourceImage.png"), > ImageFormats.PNG, null); > > // Get a DOMImplementation. > DOMImplementation domImpl = > GenericDOMImplementation.getDOMImplementation(); > > // Create an instance of org.w3c.dom.Document. > String svgNS = "http://www.w3.org/2000/svg"; > Document document = domImpl.createDocument(svgNS, "svg", null); > > // Create an instance of the SVG Generator. > SVGGraphics2D svgGenerator = new SVGGraphics2D(document); > > // Ask the test to render into the SVG Graphics2D implementation. > svgGenerator.drawImage(sourceImage, 0, 0, null); > ... > > When I end with `Imaging.writeImage(sourceImage, new > File("sourceImage.png"), ImageFormats.PNG, null);` a PNG is created. > However at `svgGenerator.drawImage(sourceImage, 0, 0, null);` I get this > stack trace: > > java.lang.NullPointerException > at org.apache.batik.svggen.ImageHandlerBase64Encoder.encodeImage(Unknown > Source) > at org.apache.batik.svggen.ImageHandlerBase64Encoder.handleHREF(Unknown > Source) > at org.apache.batik.svggen.ImageHandlerBase64Encoder.handleHREF(Unknown > Source) > at org.apache.batik.svggen.DefaultImageHandler.handleImage(Unknown Source) > at org.apache.batik.svggen.SimpleImageHandler.handleImage(Unknown Source) > at org.apache.batik.svggen.SVGGraphics2D.drawImage(Unknown Source) > at com.corp.util.image.ImageReadExample.main(ImageReadExample.java:49) > > I am using Batik 1.8 with Java 1.8.0_91 on MacOS 10.10.5. > > -- > "Hell hath no limits, nor is circumscrib'd In one self-place; but where we > are is hell, And where hell is, there must we ever be" --Christopher > Marlowe, *Doctor Faustus* (v. 121-24) > -- "Hell hath no limits, nor is circumscrib'd In one self-place; but where we are is hell, And where hell is, there must we ever be" --Christopher Marlowe, *Doctor Faustus* (v. 121-24) --001a1133056a84ec7f05323128db Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Ah! Mystery solved. See:=C2=A0<a href=3D"http://stackoverf= low.com/questions/16138850/batik-svggraphics2d-nullpointerexception-when-dr= awing-image">http://stackoverflow.com/questions/16138850/batik-svggraphics2= d-nullpointerexception-when-drawing-image</a>, specifically the April 14th = comment to the lone answer. This comment points out a bug in Batik 1.8 (<a = href=3D"https://issues.apache.org/jira/browse/BATIK-1146">https://issues.ap= ache.org/jira/browse/BATIK-1146</a>). Using Batik 1.7 fixed the problem.<di= v><br></div><div>(Let me apologize for double post. A question I had sent t= he day before to the tomcat-user's list had never posted to <a href=3D"= http://mail-archives.apache.org">mail-archives.apache.org</a>, and I though= t my April 30th message here was swallowed as well. As it was, it posted Ma= y 1st and I missed it.)</div></div><div class=3D"gmail_extra"><br><div clas= s=3D"gmail_quote">On Fri, May 6, 2016 at 12:36 PM, Thad Humphries <span dir= =3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank">= [email protected]</a>></span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left= :1ex"><div dir=3D"ltr"><span style=3D"font-size:12.8px">I am trying to crea= te an SVG from a BufferedImage using the example at=C2=A0</span><a href=3D"= https://xmlgraphics.apache.org/batik/using/svg-generator.html" style=3D"fon= t-size:12.8px" target=3D"_blank">https://xmlgraphics.apache.org/batik/using= /svg-generator.html</a><span style=3D"font-size:12.8px">. The image is a TI= FF read from disk using Apache Commons Imaging. I can successfully export t= his image to PNG. However cannot create an SVG. The code is</span><div styl= e=3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8px"><div>=C2= =A0 public static void main(String[] args) {</div><div>=C2=A0 =C2=A0 try {<= /div><div>=C2=A0 =C2=A0 =C2=A0 File inFile =3D new File(args[0]);</div><div= >=C2=A0 =C2=A0 =C2=A0 BufferedImage sourceImage =3D ImageReadExample.imageR= eadExample(inFile);</div><div>=C2=A0 =C2=A0 =C2=A0 // Imaging.writeImage(so= urceImage, new File("sourceImage.png"), ImageFormats.PNG, null);<= /div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // Get a DOMImplementation.</= div><div>=C2=A0 =C2=A0 =C2=A0 DOMImplementation domImpl =3D</div><div>=C2= =A0 =C2=A0 =C2=A0 =C2=A0 GenericDOMImplementation.getDOMImplementation();</= div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // Create an instance of org.w= 3c.dom.Document.</div><div>=C2=A0 =C2=A0 =C2=A0 String svgNS =3D "<a h= ref=3D"http://www.w3.org/2000/svg" target=3D"_blank">http://www.w3.org/2000= /svg</a>";</div><div>=C2=A0 =C2=A0 =C2=A0 Document document =3D domImp= l.createDocument(svgNS, "svg", null);</div><div><br></div><div>= =C2=A0 =C2=A0 =C2=A0 // Create an instance of the SVG Generator.</div><div>= =C2=A0 =C2=A0 =C2=A0 SVGGraphics2D svgGenerator =3D new SVGGraphics2D(docum= ent);</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // Ask the test to rend= er into the SVG Graphics2D implementation.</div><div>=C2=A0 =C2=A0 =C2=A0 s= vgGenerator.drawImage(sourceImage, 0, 0, null);</div></div><div style=3D"fo= nt-size:12.8px">=C2=A0 =C2=A0 =C2=A0 ...</div><div style=3D"font-size:12.8p= x"><br clear=3D"all"><div>When I end with `Imaging.writeImage(sourceImage, = new File("sourceImage.png"), ImageFormats.PNG, null);` a PNG is c= reated. However at `svgGenerator.drawImage(sourceImage, 0, 0, null);` I get= this stack trace:</div><div><br></div><div><div>java.lang.NullPointerExcep= tion</div><div><span style=3D"white-space:pre-wrap"> </span>at org.apache.b= atik.svggen.ImageHandlerBase64Encoder.encodeImage(Unknown Source)</div><div= ><span style=3D"white-space:pre-wrap"> </span>at org.apache.batik.svggen.Im= ageHandlerBase64Encoder.handleHREF(Unknown Source)</div><div><span style=3D= "white-space:pre-wrap"> </span>at org.apache.batik.svggen.ImageHandlerBase6= 4Encoder.handleHREF(Unknown Source)</div><div><span style=3D"white-space:pr= e-wrap"> </span>at org.apache.batik.svggen.DefaultImageHandler.handleImage(= Unknown Source)</div><div><span style=3D"white-space:pre-wrap"> </span>at o= rg.apache.batik.svggen.SimpleImageHandler.handleImage(Unknown Source)</div>= <div><span style=3D"white-space:pre-wrap"> </span>at org.apache.batik.svgge= n.SVGGraphics2D.drawImage(Unknown Source)</div><div><span style=3D"white-sp= ace:pre-wrap"> </span>at com.corp.util.image.ImageReadExample.main(ImageRea= dExample.java:49)</div></div><div><br></div><div>I am using Batik 1.8 with = Java 1.8.0_91 on MacOS 10.10.5.</div></div><span class=3D"HOEnZb"><font col= or=3D"#888888"><div><br></div>-- <br><div><div dir=3D"ltr">"Hell hath = no limits, nor is circumscrib'd In one self-place; but where we are is = hell, And where hell is, there must we ever be" --Christopher Marlowe,= <i>Doctor Faustus</i> (v. 121-24)</div></div> </font></span></div> </blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class= =3D"gmail_signature"><div dir=3D"ltr">"Hell hath no limits, nor is cir= cumscrib'd In one self-place; but where we are is hell, And where hell = is, there must we ever be" --Christopher Marlowe, <i>Doctor Faustus</i= > (v. 121-24)</div></div> </div> --001a1133056a84ec7f05323128db--