Areas


Topics

j3d.org

Image capturing

  1. How do I capture still images in JPEG format?
  2. How do I capture the Java3D screen to a movie/animated GIF/etc?
  3. Why can't I use immediate mode rendering to an offscreen buffer?

Return to the main FAQ page for more questions and answers.

 

1. How do I capture still images in JPEG format?
Contributor: Steve Pietrowicz

This can be done by getting inside the rendering cycle and saving out the contents of each frame. Effectively what you do is extend Canvas3D and override the postSwap() method. This method is called after J3D has rendered to the background buffer and then swapped it to screen so you know you have a completed frame.

Don't call postSwap() directly. Set a flag in another method which you can call from within your program, and then call repaint() from within that method.

Once the postSwap method is called, check the flag. If it has been set, you then use the readRaster() method to dump the raw bytes from the frame. You now have the option of directly dumping those bytes to disk and then post processing them or directly saving them as JPEG/MPEG/whatever.

Peter Kunszt provided the code to show how this can be done in your own programs. Click here for the code to CapturingCanvas3D.java. A complete library for dealing with image capture is also available as part of the j3d.org Code Repository

 

2. How do I capture the Java3D screen to a movie/animated GIF/etc?

The basic approach is the same as that to capture a still image - except now you need to capture an image every frame. If you have the basic system for a still image, capturing to a video format is just a case of passing every frame to whatever encoder you are using. To write out these forms of images, you will probably want to look at the Java Media Framework (JMF).

 

3. Why can't I use immediate mode rendering to an offscreen buffer?

Java3D 1.2 does not support immediate-mode rendering into an offscreen buffer. You'll call Canvas3D.renderOffScreenBuffer() to schedule the rendering of a frame to an off-screen buffer.