Thanks for pointing me in the right direction Vincent! I've attached
the working code for converting an SVG file into a java.awt.Image below
for posterity.
public static Image transcodeSVGDocument( URL url, int x, int y ){
// Create a PNG transcoder.
Transcoder t = new PNGTranscoder();
// Set the transcoding hints.
t.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float(x) );
t.addTranscodingHint( PNGTranscoder.KEY_HEIGHT, new Float(y) );
// Create the transcoder input.
TranscoderInput input = new TranscoderInput( url.toString() );
ByteArrayOutputStream ostream = null;
try {
// Create the transcoder output.
ostream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput( ostream );
// Save the image.
t.transcode( input, output );
// Flush and close the stream.
ostream.flush();
ostream.close();
} catch( Exception ex ){
ex.printStackTrace();
}
// Convert the byte stream into an image.
byte[] imgData = ostream.toByteArray();
Image img = Toolkit.getDefaultToolkit().createImage( imgData );
// Wait until the entire image is loaded.
MediaTracker tracker = new MediaTracker( new JPanel() );
tracker.addImage( img, 0 );
try {
tracker.waitForID( 0 );
} catch( InterruptedException ex ){
ex.printStackTrace();
}
// Return the newly rendered image.
return img;
}
Warren W. Thompson
Beer is proof that God loves us and wants us to be happy.
--Benjamin Franklin
Vincent Hardy wrote:
>
> Warren,
>
> The org.apache.batik.transcoder.image.ImageTranscoder class should
> be exactly what you want. If you are trying to convert images to
> PNG or JPEG, you may even be able to use the PNGTranscoder or the
> JPEGTranscoder directly. If you are trying to do something else,
> implementing your own ImageTranscoder and using
> PNGTranscode and JPEGTranscoder as sources of inspiration.
>
> May be Thierry, who wrote the transcoder framework, will have more
> comments.
>
> Cheers,
> Vincent.
>
> "Warren W. Thompson" wrote:
> >
> > I am attempting to render an SVG image file as a java.awt.Image, or one
> > of its subclasses like java.awt.image.BufferedImage. I have attempted to
> > directly render the SVG file and I have also attempted to transcode it.
> > I am currently using Batik version 1.1.
> >
> > I have achieved the most success with the following code snippet; however,
> > the BufferedImage that is returned is the correct shape without any colors
> > or details being drawn. I feel that I am missing some step in the rendering
> > process, and I have looked in detail at the Batik documentation and mailing
> > list archives to no avail.
> >
> > public static Image renderSVGDocument( URL url, int x, int y ){
> > // Create a rendereing context.
> > UserAgent userAgent = new UserAgentAdapter();
> > DocumentLoader loader = new DocumentLoader( userAgent );
> > Document doc = null;
> > try {
> > doc = loader.loadDocument( url.toString() );
> > } catch( Exception ex ){
> > ex.printStackTrace();
> > }
> > BridgeContext ctx = new BridgeContext(userAgent, loader);
> >
> > // Create a buffered image.
> > BufferedImage bufImg =
> > new BufferedImage( x, y, BufferedImage.TYPE_INT_ARGB );
> > Graphics2D graphics = GraphicsUtil.createGraphics( bufImg );
> >
> > // Create a builder and root node.
> > GVTBuilder builder = new GVTBuilder();
> > GraphicsNode root = null;
> >
> > // Render the SVG image.
> > try {
> > root = builder.build(ctx, doc);
> > root.paint(graphics);
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> >
> > return bufImg;
> > }
> >
> > Thanks in advance for your assistance!
> >
> > Warren W. Thompson
> >
> > Beer is proof that God loves us and wants us to be happy.
> > --Benjamin Franklin
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]