E
Evgrafov
Hi there.
Recently i've tried to create BufferedImage from SVG content. I used a
documented method which is described on Batik's site:
public static BufferedImage getImageFromSvg(SVGDocument document,
Dimension dimension)
throws IOException
{
// Load SVG resource into a document
// Build the tree and get the document dimensions
UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
BridgeContext bridgeContext = new
BridgeContext(userAgentAdapter);
GVTBuilder builder = new GVTBuilder();
GraphicsNode graphicsNode =
builder.build(bridgeContext,document);
// Paint svg into image buffer
BufferedImage bufferedImage = new
BufferedImage(dimension.width,dimension.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
graphicsNode.paint(g2d);
// Cleanup and return image
g2d.dispose();
return bufferedImage;
}
...........................................
startTime = System.currentTimeMillis();
BufferedImage bim = getImageFromSvg(document,new
Dimension(Integer.parseInt(args[1]),Integer.parseInt(args[2])));
endTime = System.currentTimeMillis();
System.out.println("BufferedImage from SVG:
"+(endTime-startTime));
Then i used PNGTranscoder to create the png content based on the same
SVG document. I used the following method:
baos = new ByteArrayOutputStream();
PNGTranscoder pngtr = new PNGTranscoder();
pngtr.addTranscodingHint(PNGTranscoder.KEY_AOI,new
Rectangle(0,0,new Integer(args[1]).intValue(),new
Integer(args[2]).intValue()));
//pngtr.addTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT,new
Float(50.0));
//pngtr.addTranscodingHint(PNGTranscoder.KEY_MAX_WIDTH,new
Float(50.0));
pngtr.addTranscodingHint(PNGTranscoder.KEY_HEIGHT,new
Float(args[1]));
pngtr.addTranscodingHint(PNGTranscoder.KEY_WIDTH,new
Float(args[2]));
try {
startTime = System.currentTimeMillis();
pngtr.transcode(new TranscoderInput(document),new
TranscoderOutput(baos));
endTime = System.currentTimeMillis();
} catch (TranscoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("PNG from SVG: "+(endTime-startTime));
The program outputs:
BufferedImage from SVG: 2016
PNG from SVG: 547
I wonder that SVG->BufferedImage is almost four times slowly then
SVG->PNG. Does anyone there can explain why? It is very critical for my
application. I would prefere BufferedImage rather then PNG. But that
performance is realy annoys me.
Thanks.
Evgrafov.
Recently i've tried to create BufferedImage from SVG content. I used a
documented method which is described on Batik's site:
public static BufferedImage getImageFromSvg(SVGDocument document,
Dimension dimension)
throws IOException
{
// Load SVG resource into a document
// Build the tree and get the document dimensions
UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
BridgeContext bridgeContext = new
BridgeContext(userAgentAdapter);
GVTBuilder builder = new GVTBuilder();
GraphicsNode graphicsNode =
builder.build(bridgeContext,document);
// Paint svg into image buffer
BufferedImage bufferedImage = new
BufferedImage(dimension.width,dimension.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
graphicsNode.paint(g2d);
// Cleanup and return image
g2d.dispose();
return bufferedImage;
}
...........................................
startTime = System.currentTimeMillis();
BufferedImage bim = getImageFromSvg(document,new
Dimension(Integer.parseInt(args[1]),Integer.parseInt(args[2])));
endTime = System.currentTimeMillis();
System.out.println("BufferedImage from SVG:
"+(endTime-startTime));
Then i used PNGTranscoder to create the png content based on the same
SVG document. I used the following method:
baos = new ByteArrayOutputStream();
PNGTranscoder pngtr = new PNGTranscoder();
pngtr.addTranscodingHint(PNGTranscoder.KEY_AOI,new
Rectangle(0,0,new Integer(args[1]).intValue(),new
Integer(args[2]).intValue()));
//pngtr.addTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT,new
Float(50.0));
//pngtr.addTranscodingHint(PNGTranscoder.KEY_MAX_WIDTH,new
Float(50.0));
pngtr.addTranscodingHint(PNGTranscoder.KEY_HEIGHT,new
Float(args[1]));
pngtr.addTranscodingHint(PNGTranscoder.KEY_WIDTH,new
Float(args[2]));
try {
startTime = System.currentTimeMillis();
pngtr.transcode(new TranscoderInput(document),new
TranscoderOutput(baos));
endTime = System.currentTimeMillis();
} catch (TranscoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("PNG from SVG: "+(endTime-startTime));
The program outputs:
BufferedImage from SVG: 2016
PNG from SVG: 547
I wonder that SVG->BufferedImage is almost four times slowly then
SVG->PNG. Does anyone there can explain why? It is very critical for my
application. I would prefere BufferedImage rather then PNG. But that
performance is realy annoys me.
Thanks.
Evgrafov.