This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

How to save 3D into a image file?

843799
843799 Member Posts: 49,999
edited Aug 16, 2001 10:25AM in Java 3D
Hi guys:
I want to save a 3D image shown on the screen into a gif file, how could I do this?
regards.

Comments

  • 843799
    843799 Member Posts: 49,999
    I had the same problem. As far as i know, the paint method of the component which contains your Canvas3D object will only invoke the rendering process. Using this method to draw to an image will result in a black image.

    A workaround i have found is to use the Robot API:
    The following code writes the content of your Canvas3D in a JPEG file using the screenshot function.
    I know it's a hack but it works on my machine...

    'path' is the pathname of the destination jpeg file,
    'canvas' is your Canvas3D object.

    To use JPEGEncoder you have to import:
    com.sun.image.codec.jpeg.*;


    try
    {
    Robot robot = new Robot();
    Rectangle theRectOnScreen = new Rectangle(canvas.getLocationOnScreen().x, canvas.getLocationOnScreen().y, canvas.getWidth(), canvas.getHeight());
    BufferedImage bi = robot.createScreenCapture(theRectOnScreen);
    File f = new File(path);
    if (f.exists())
    {
    f.delete();
    }
    try
    {
    if (f.createNewFile())
    {
    FileOutputStream fos = new FileOutputStream(f);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
    encoder.encode(bi);
    fos.flush();
    fos.close();
    }
    }
    catch (IOException ioex)
    {
    System.out.println("Error writing JPEG");
    }
    }
    catch (AWTException aex)
    {
    System.out.println("Error getting Screen content");
    }
  • 843799
    843799 Member Posts: 49,999
    What is Robot? How to use the "Robot"?Thanks!
  • 843799
    843799 Member Posts: 49,999
    The java.awt.Robot can be used to generate native system input events (e.g. keyboard key pressed, mouse moved/pressed). You can use it for automatic testing or for demonstration purposes.
    But as I've already said, this might be not the best solution. It simulates the printscreen key.

    I recently found a link for this topic, see http://www.j3d.org/faq/capturing.html

    I didn't try it out, perhaps this is the correct solution for our problem.

  • 843799
    843799 Member Posts: 49,999
    I went to the link and downloaded the CaptureCanvas3D.java file.

    Ya so basically the postSwap method just gets called over and over by some thread running in the background. So when you want to draw the image to a file, you set the flag temporarily.

    Seemed to work like a charm. Thanks for the link p_b!
  • 843799
    843799 Member Posts: 49,999
    Thanks everybody here.
    In fact, I want to get a image file by a backgroup thread without shown the picture on screeen, and I have tried CapturingCanvas3D, but the file will be created only after the cube was displayed, OH, my god, what can I do now?
  • 843799
    843799 Member Posts: 49,999
    Thanks everybody here.
    In fact, I want to get a image file by a backgroup thread without shown the picture on screeen, and I have tried CapturingCanvas3D, but the file will be created only after the cube was displayed, OH, my god, what can I do now?
  • 843799
    843799 Member Posts: 49,999
    Try using a Canvas3D object in offscreen rendering mode.
    (Second parameter -> true in Canvas3D constructor).
    A Canvas3D in this mode can't be added to a container,
    so there will nothing appear on the screen.
  • 843799
    843799 Member Posts: 49,999
    I had to try that. And it worked very well. Check out my site:
    http://www.geocities.com/virtualexp
    with the JPG-images I made.
    Thanks for the info!
This discussion has been closed.