Create an image from a non-visible AWT Component?

后端 未结 5 596
迷失自我
迷失自我 2020-12-16 11:15

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

5条回答
  •  轮回少年
    2020-12-16 11:58

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


    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.


    1. Making your own to image renderer

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


    2. Rendering onto a virtual screen

    Prerequisites:

    1. Can you start the program/component in a headless environment?
    2. Are you using Linux?

    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
    

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

提交回复
热议问题