SVGGraphics2D drawImage() NullPointerException
Thad Humphries <[email protected]> Fri, 6 May 2016 12:36:06 -0400
| Newsgroups | gmane.text.xml.batik.user |
|---|---|
| Message-ID | <CA+bF-zye8y5LUK+KUWCYWmGPHr6voQnbDueHatm7iptzuO6Y7g@mail.gmail.com> |
--94eb2c047f0839232c05322f0e48
Content-Type: text/plain; charset=UTF-8
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)
--94eb2c047f0839232c05322f0e48
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><span style=3D"font-size:12.8px">I am trying to create an =
SVG from a BufferedImage using the example at=C2=A0</span><a href=3D"https:=
//xmlgraphics.apache.org/batik/using/svg-generator.html" target=3D"_blank" =
style=3D"font-size:12.8px">https://xmlgraphics.apache.org/batik/using/svg-g=
enerator.html</a><span style=3D"font-size:12.8px">. The image is a TIFF rea=
d from disk using Apache Commons Imaging. I can successfully export this im=
age to PNG. However cannot create an SVG. The code is</span><div style=3D"f=
ont-size:12.8px"><br></div><div style=3D"font-size:12.8px"><div>=C2=A0 publ=
ic static void main(String[] args) {</div><div>=C2=A0 =C2=A0 try {</div><di=
v>=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.imageReadExamp=
le(inFile);</div><div>=C2=A0 =C2=A0 =C2=A0 // Imaging.writeImage(sourceImag=
e, new File("sourceImage.png"), ImageFormats.PNG, null);</div><di=
v><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.w3c.dom.Doc=
ument.</div><div>=C2=A0 =C2=A0 =C2=A0 String svgNS =3D "<a href=3D"htt=
p://www.w3.org/2000/svg" target=3D"_blank">http://www.w3.org/2000/svg</a>&q=
uot;;</div><div>=C2=A0 =C2=A0 =C2=A0 Document document =3D domImpl.createDo=
cument(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(document);</div=
><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // Ask the test to render into th=
e SVG Graphics2D implementation.</div><div>=C2=A0 =C2=A0 =C2=A0 svgGenerato=
r.drawImage(sourceImage, 0, 0, null);</div></div><div style=3D"font-size:12=
.8px">=C2=A0 =C2=A0 =C2=A0 ...</div><div style=3D"font-size:12.8px"><br cle=
ar=3D"all"><div>When I end with `Imaging.writeImage(sourceImage, new File(&=
quot;sourceImage.png"), ImageFormats.PNG, null);` a PNG is created. Ho=
wever at `svgGenerator.drawImage(sourceImage, 0, 0, null);` I get this stac=
k trace:</div><div><br></div><div><div>java.lang.NullPointerException</div>=
<div><span style=3D"white-space:pre-wrap"> </span>at org.apache.batik.svgge=
n.ImageHandlerBase64Encoder.encodeImage(Unknown Source)</div><div><span sty=
le=3D"white-space:pre-wrap"> </span>at org.apache.batik.svggen.ImageHandler=
Base64Encoder.handleHREF(Unknown Source)</div><div><span style=3D"white-spa=
ce:pre-wrap"> </span>at org.apache.batik.svggen.ImageHandlerBase64Encoder.h=
andleHREF(Unknown Source)</div><div><span style=3D"white-space:pre-wrap"> <=
/span>at org.apache.batik.svggen.DefaultImageHandler.handleImage(Unknown So=
urce)</div><div><span style=3D"white-space:pre-wrap"> </span>at org.apache.=
batik.svggen.SimpleImageHandler.handleImage(Unknown Source)</div><div><span=
style=3D"white-space:pre-wrap"> </span>at org.apache.batik.svggen.SVGGraph=
ics2D.drawImage(Unknown Source)</div><div><span style=3D"white-space:pre-wr=
ap"> </span>at com.corp.util.image.ImageReadExample.main(ImageReadExample.j=
ava: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><div><br></div>-- <br><div class=3D"gmail_=
signature"><div dir=3D"ltr">"Hell hath no limits, nor is circumscrib&#=
39;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>
--94eb2c047f0839232c05322f0e48--