拍摄JavaFx 2.2中场景或场景一部分的屏幕截图 [英] Taking a screenshot of a scene or a portion of a scene in JavaFx 2.2

查看:130
本文介绍了拍摄JavaFx 2.2中场景或场景一部分的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用




WritableImage snapshot = obj.getScene()。snapshot(null);



现在,我想将此屏幕截图输出为pdf文件。我已经设法使用以下代码使用Apache pdfbox库将文本输出到pdf:



PDDocument doc = null;
PDPage页面= null;

  try {
doc = new PDDocument();
page = new PDPage();

doc.addPage(page);
PDFont字体= PDType1Font.HELVETICA_BOLD;

PDPageContentStream content = new PDPageContentStream(doc,page);
content.beginText();
content.setFont(font,12);
content.moveTextPositionByAmount(100,700);
content.drawString( Hello World);

content.endText();
content.close();
doc.save( PDFWithText.pdf);
doc.close();
} catch(异常e){
System.out.println(e);
}

在使用WritableImage而不是基本的String文本时,该怎么做? / p>

另外,如何为场景中的某些节点拍摄屏幕截图?



谢谢

解决方案


为场景截图


您的问题中已经有此功能的代码。

  WritableImage snapshot = stage.getScene()。快照(空); 







截图的。 。 。 JavaFx 2.2中场景的一部分


获取Node的快照类似于获取场景的快照,您只需使用快照方法(而不是场景)。

  WritableImage snapshot = node.snapshot(null,null);首先,将节点放在场景中,然后对该节点进行快照。 

第一个参数可以传递给 node.snapshot 调用是 SnapshotParameters 的一些配置(您可能不需要,但是您可以对它们进行调查,以了解它们是否对您的案例有用或有用)。







现在,我想将此屏幕截图输出为pdf文件。使用WritableImage而不是使用基本的字符串文本时,该怎么办?


我没有使用问题中引用的pdfbox工具箱。该工具包可能适用于基于awt的映像而不是JavaFX映像,因此您需要使用 SwingFXUtils.fromFXImage



要实际将awt编码的图像转换为pdf文件,请查阅pdfbox工具箱的文档。 Kasas对将BufferedImage添加到PDFBox文档的答案似乎为该操作提供了代码段。看起来相关的代码(并且我还没有尝试过)是:

  PDPageContentStream content = new PDPageContentStream(doc,page) ; 
PDXObjectImage ximage =新的PDJpeg(doc,bufferedImage);
content.drawImage(ximage,x,y);


I've managed to make a WritableImage using

WritableImage snapshot = obj.getScene().snapshot(null);

Now I would like to output this screenshot on a pdf file. I've already managed to output text to a pdf using Apache pdfbox library using the following code:

PDDocument doc = null; PDPage page = null;

   try{
       doc = new PDDocument();
       page = new PDPage();

       doc.addPage(page);
       PDFont font = PDType1Font.HELVETICA_BOLD;

       PDPageContentStream content = new PDPageContentStream(doc, page);
       content.beginText();
       content.setFont( font, 12 );
       content.moveTextPositionByAmount( 100, 700 );
       content.drawString("Hello World");

       content.endText();
       content.close();
      doc.save("PDFWithText.pdf");
      doc.close();
    } catch (Exception e){
    System.out.println(e);
    }

How can I do this when using WritableImage rather that using basic String texts?

Also, how can I take a screenshot of certain nodes within a scene?

Thanks

解决方案

Taking a screenshot of a scene

You already have working code for this in your question.

WritableImage snapshot = stage.getScene().snapshot(null);


Taking a screenshot of a . . . portion of a scene in JavaFx 2.2

Taking a snapshot of Node is similar to taking snapshot of a Scene, you just use the snapshot methods on the Node rather than the scene. First place your Node in a Scene, and then snapshot the Node.

WritableImage snapshot = node.snapshot(null, null);

The first parameter which may be passed to the node.snapshot call is some configuration for SnapshotParameters (which you probably don't need, but you can investigate them to see if they are required or useful for your case).


Now I would like to output this screenshot on a pdf file. How can I do this when using WritableImage rather that using basic String texts?

I have not used the pdfbox toolkit you reference in your question. Likely the toolkit works with awt based images rather than JavaFX images, so you will need to convert your JavaFX snapshot image to an awt buffered image using SwingFXUtils.fromFXImage.

To actually get the awt encoded image into a pdf file, consult the documentation for your pdfbox toolkit. Kasas's answer to Add BufferedImage to PDFBox document would seem to provide a code snippet for this operation. Looks like the relevant code (and I haven't tried this) is:

PDPageContentStream content = new PDPageContentStream(doc, page);
PDXObjectImage ximage = new PDJpeg(doc, bufferedImage);
content.drawImage(ximage, x, y);

这篇关于拍摄JavaFx 2.2中场景或场景一部分的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆