• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

need help to generate png image from svg file

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there
It would be much appreciated if any one could help me out.
I have a servlet where I use PNGTranscoder to generate png image and display it. This is my code:
PNGTranscoder trans = new PNGTranscoder();
File svgFile = new File(SVGFILE);
long svgSize = svgFile.length();
TranscoderInput input = new TranscoderInput(svgURI);
ByteArrayOutputStream outStream
= new ByteArrayOutputStream((int)svgSize);
TranscoderOutput output
= new TranscoderOutput(outStream);
trans.transcode(input,output);

HOWEVER, the page above codes rendered is blank. May I ask why the png file is not created corrected ?
Tons of thanks,
Lily
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not 100% sure, but it might be this part of the line with the int in ()
ByteArrayOutputStream((int)svgSize);
Eric
 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That syntax is correct. I think the reason might be that I used ByteArrayOutputStream as PNGTranscoder's output stream and ByteArrayOutputStream will not out put its contents to browser.
However, PNGTranscoder can only accept ByteArrayOutputStream as it output stream. I am wondering if there is any way that I can get around this problem.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to pass back the contents of the ByteArrayOutputStream as part of the HttpResponse you will need to do something like this at the end of your code:
response.setContentType("image/png");
ServletOutputStream servletOutput =
response.getOutputStream();
servletOutput.write(outStream.toByteArray());
servletOutput.flush();
response.setStatus(HTTPServletResponse.SC_OK);

Hope this is what you are after.
[ April 10, 2003: Message edited by: Andy Bowes ]
 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Andy. Yes, this is what I need. I need to set contenttype to be image/png.
Thanks again,
Lily
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!