Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX convert WritableImage to Image

I am doing some application which makes a screenshot of LineChart and save it to pdf file. So I don't know a smooth way to convert WritableImage (JavaFX 2.2) to Image (iText lib).

My temporary solution is:

  1. to make a snapshot, then
  2. get the WritableImage from the snapshot
  3. write the image to a png file
  4. open the image and make iText object Image

I would like to make some changes: I don't want to write the png file to disc, I just want the snapshot to be written to a pdf file.

My temporary solution is:

 WritableImage wim = new WritableImage((int) lineChart.getWidth(),(int) lineChart.getHeight());
 Scene scena = primaryStage.getScene();
 scena.snapshot(wim);
 
 File fileA = new File("C://Graphs/chart.png");
 try {
      ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", fileA);
 }
 catch (Exception s) {
 }
 pdfDocument.add(preface3);
 com.itextpdf.text.Image graph =com.itextpdf.text.Image.getInstance("C://Graphs/chart.png");
 pdfDocument.add((com.itextpdf.text.Element) graph);        
like image 564
IndexOutOfDevelopersException Avatar asked Dec 19 '13 08:12

IndexOutOfDevelopersException


1 Answers

Use:

ByteArrayOutputStream  byteOutput = new ByteArrayOutputStream();

ImageIO.write( SwingFXUtils.fromFXImage( wim, null ), "png", byteOutput );

com.itextpdf.text.Image  graph;
graph = com.itextpdf.text.Image.getInstance( byteOutput.toByteArray() );
like image 157
Jurgen Avatar answered Sep 29 '22 11:09

Jurgen