从不可见的 AWT 组件创建图像? [英] Create an image from a non-visible AWT Component?

查看:26
本文介绍了从不可见的 AWT 组件创建图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个不可见的 AWT 组件的图像(屏幕截图).我无法使用 Robot 类的屏幕捕获功能,因为该组件在屏幕上不可见.尝试使用以下代码:

I'm trying to create an image (screen-shot) of a non-visible AWT component. I can't use the Robot classes' screen capture functionality because the component is not visible on the screen. Trying to use the following code:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
component.paintAll(g);

有时有效,但如果组件包含诸如文本框或按钮之类的东西,或某种 OpenGL/3D 组件(这些东西被排除在图像之外!),则不起作用.我怎样才能对整个事情进行正确的截图?

Works sometimes, but does not work if the component contains things such as a text box or button, or some sort of OpenGL / 3D component (these things are left out of the image!). How can I take a proper screenshot of the whole thing?

推荐答案

很好的问题,我自己也时不时想过这个问题!

Excellent question, I've thought about this myself from time to time!

正如您已经写过的那样,将 3D 和 AWT 等重量级组件渲染到图像上是一个大问题.这些组件(几乎)直接传输到图形卡,因此它们不能使用普通的 paintComponent 内容重新渲染为图像,您需要操作系统的帮助或对这些组件进行自己的渲染.

As you already have written, that rending heavy weight components such as 3D and AWT onto an image is a big problem. These components are (almost) directly transferred to the graphic card so they cannot be re-rendered to an image using the normal paintComponent stuff, you need help from the operative system or doing your own rendering of these components.

对于每个没有图像渲染方法的组件,您需要创建自己的组件.例如使用 jogl 你可以使用这个 方法 (SO post).

For each component that does not have a to image rendering method you need to create your own. For example using jogl you can take a off-screen screenshot using this method (SO post).

先决条件:

  1. 您能否在无头环境中启动程序/组件?
  2. 您使用的是 Linux 吗?

然后您可以使用 Xvfb 将整个程序渲染到虚拟屏幕上,然后将来自该虚拟屏幕的屏幕截图,如下所示:

Then you can use Xvfb to render the whole program onto a virtual screen and then taking a screenshot from that virtual screen like this:

Xvfb :1 &
DISPLAY=:1 java YourMainClass
xwd -display :1 -root -out image.xwd

也许您需要通过将要渲染的程序的大小传递给它来稍微调整 Xvfb (-screen 0 1024x768x24).

Maybe you need to tweek Xvfb a little bit by passing the size of the program you want to render to it (-screen 0 1024x768x24).

这篇关于从不可见的 AWT 组件创建图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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