JavaFX如何将WritableImage转换为Image
发布日期:2021-07-01 01:57:29 浏览次数:2 分类:技术文章

本文共 1362 字,大约阅读时间需要 4 分钟。

转载自  

我正在做一些应用程序,制作截图LineChart并将其保存为pdf,所以我不知道将WritableImage(JavaFX 2.2)转换为Image(iText lib)的平滑方式。

临时解决方案是

  • 然后,制作快照
  • 获得WritableImage从该快照
  • 将图像写入png文件
  • 打开图像并制作iText对象 Image

我想做一些更改:我不想将png文件写入光盘,我只想将快照写入pdf

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);

用途:

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() );

从画布中复制绘图,将复制到WritableImage,而不是从WritableImage获得图像

WritableImage wi = new WritableImage((int)gc.getCanvas().getWidth(),                 (int)gc.getCanvas().getHeight());        gc.getCanvas().snapshot(null, wi); //Coping all that now in Canvas        //gc is GraphicContext object from Canvas, it has drawing functions        BufferedImage bi =SwingFXUtils.fromFXImage((Image)wi, null);         SwingFXUtils.toFXImage(bi, (WritableImage)image);

 

转载地址:https://maokun.blog.csdn.net/article/details/84979648 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java自动化邮件中发送图表(一)
下一篇:<table/>设置列宽度无效的问题

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年05月01日 22时15分41秒