You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Brecht Yperman <br...@UGent.be> on 2004/03/08 13:11:23 UTC
ImageTranscoder problem
Hi,
i'm new to Batik. I've searched the archive to find an answer to my problem,
but I didn't find anything.
We're developing a tool to visualise networks. We'd like to be able to
export them to different graphic formats.
Exporting to svg is no problem.
For export to JPG, TIF and PNG, i first create a temporary svg file and then
transcode that file to one of those formats.
What seems to be the problem:
the svg file doesn't have width or height attributes. Displaying a 1000*1000
svg file is no problem, but when I transcode it, it clips the image to the
upper left 400*400 pixels.
Is there a way to solve this?
Thanks in advance,
Brecht
Code:
/**
* Export a visualisation to the SVG format
*
* @param vis the visualisation to be exported
* @param fileName the filename under which it should be saved
* @return the filename
*/
public static File exportToSVG(Visualisation vis, String fileName){
logger.debug("");
File file = new File(fileName);
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
ctx.setComment("Generated by Trs-visualisation");
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
// render visualisation height 1000 and width 1000
vis.render(svgGenerator, 1000, 1000, null);
boolean useCSS = false;
try {
Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
svgGenerator.stream(out, useCSS);
} catch (IOException e) {
logger.error(e);
}
return file;
}
/**
* Export a visualisation to the JPG format
*
* @param vis the visualisation to be exported
* @param fileName the filename under which it should be saved
* @return the filename
*/
public static File exportToJPG(Visualisation vis, String filename) {
logger.debug("");
File file = new File(filename);
JPEGTranscoder t = new JPEGTranscoder();
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1));
t.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(1000));
//t.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(1000));
transcode(vis, file, t);
return file;
}
/**
* Create an image from a visualisation
*
* @param vis the visualisation
* @param file the filename
* @param t the transcoder used to create the image
*/
private static void transcode(Visualisation vis, File file, ImageTranscoder
t){
logger.debug("");
String svgURI = null;
try {
svgURI = exportToSVG(vis, "temp.svg").toURL().toString();
} catch (MalformedURLException e) {
logger.error(e);
}
TranscoderInput input = new TranscoderInput(svgURI);
OutputStream ostream = null;
try {
ostream = new FileOutputStream(file);
} catch (FileNotFoundException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
TranscoderOutput output = new TranscoderOutput(ostream);
try {
t.transcode(input, output);
} catch (TranscoderException e1) {
logger.error(e1);
}
try {
ostream.flush();
ostream.close();
} catch (IOException e2) {
logger.error(e2);
}
new File("temp.svg").delete();
}
--
Light, sound, drums, guitar, let there be rock!
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: ImageTranscoder problem
Posted by Thomas DeWeese <Th...@Kodak.com>.
Brecht Yperman wrote:
> Exporting to svg is no problem.
> For export to JPG, TIF and PNG, i first create a temporary svg file and then
> transcode that file to one of those formats.
> What seems to be the problem:
> the svg file doesn't have width or height attributes. Displaying a 1000*1000
> svg file is no problem, but when I transcode it, it clips the image to the
> upper left 400*400 pixels.
>
> Is there a way to solve this?
Hi Brecht,
Yes, simply call 'SVGGraphics2D.setSVGCanvasSize(Dimension sz)'
with the desired size of the SVG (in your example 1000,1000):
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
svgGenerator.setSVGCanvasSize(new Dimension(1000, 1000));
Also you don't have to write the temporary file. You can
hand the transcoders a document in memory (It's one of the accepted
types for InputSource) - this saves the time of writing the
SVG and then reparsing it.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: ImageTranscoder problem
Posted by Chris Cruzdal <cc...@yahoo.com>.
Hi,
I think I know a solution to your problem.
In short, I suspect your SVG files do not have a
proper viewBox and/or width and height.
In long, check
http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01121.html
and
http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01123.html
as well as the related threads.
Hope that helps,
Chris
--
Chris Cruzdal
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org