On Thursday 20 December 2001 15:44, Warren W. Thompson wrote: > 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; > }
The following code is not really optimal as you encode the image in png and then decode it. Another way to implement that would be to subclass the ImageTranscoder such as (live code so might not work :): import org.apache.batik.transcoder.*; import org.apache.batik.transcoder.image.ImageTranscoder; public class Foo { public static Image transcodeSVGDocument( URL url, int x, int y ){ // Create a custom transcoder. Transcoder t = new MyImageTranscoder(); // 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() ); t.transcode(input, null); return t.getBufferedImage(); } static class MyImageTranscoder extends ImageTranscoder { protected BufferedImage img; public void writeImage(BufferedImage img, TranscoderOutput output) throws TranscoderException { this.img = img; // just store the image } public BufferedImage getBufferedImage() { return img; } } } Thierry. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]