Login  Register

java3d - capture screenshot or rendered image?

Posted by LordSmoke on Feb 02, 2015; 11:06pm
URL: https://forum.jogamp.org/java3d-capture-screenshot-or-rendered-image-tp4033984.html

Hi, folks! I feel like such a parasite. It seems I only show up to ask questions this time of year. I really do feel guilty about this.

I am preparing for my usual spring visits to Vienna when I actually have time to do non-trivial programming. However, I did have an afternoon free yesterday. I rec'd data for 3D points in a figure captured at a high frame-rate while walking. My software is setup to show one configurations of points/surfaces or a set (usually bones, or scans or something). So, I just added in a timer to quickly page through the set and get a reasonable animation of the person walking. Lot's of options I can add, but I only put in an afternoon.

One thing that would be really nice and address a problem one of my students has is the ability to save the image sequence to an animated .gif file. The student has a couple of thousand video images. If the save function worked, my little addition could do that for him. I found some code on the i-net that is purported to produce animated gifs from java images, but I have to send it the images. I cannot readily see how to get the screenshots that in j3d.

QUESTION: Would someone provide me with a link or appropriate search terms to find out how to do this?

I have found examples for jogl, java, Robot, etc. But it was not immediately clear how that would apply to a j3d universe.

I did find the code below in one of my programs. And if memory serves, it worked at some point to save a screen capture to a file. Doesn't work now (maybe something changed) and gives error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Canvas3D: The width of the associated Screen3D's size is <= 0
        at javax.media.j3d.Canvas3D.renderOffScreenBuffer(Canvas3D.java:1978)
       
I believe I got the same error from similar code in my new program, but I am not sure if that ever worked.

Just looking for a nudge in the right direction to search for the answer. Searching the forum didn't turn up any obvious examples.

Thanks, LS


/**
 * CODE TAKEN FROM PRINT_CANVAS3D JAVA3D EXAMPLE CODE - LordSmoke
 *
 * JavaDoc and clean 2013FEB01.
 *
 * /*
 * $RCSfile: OffScreenCanvas3D.java,v $
 *
 * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistribution of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended for
 * use in the design, construction, operation or maintenance of any nuclear
 * facility.
 *
 * $Revision: 1.1 $ $Date: 2006/02/01 01:32:53 $ $State: Exp $
 */
package morpheus_vis;

import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;

/**
 * Support 3D printing via saving onscreen graphics to image-format file. CODE
 * TAKEN FROM PRINT_CANVAS3D JAVA3D EXAMPLE CODE
 *
 * @author lordsmoke
 */
public class MVis_OffScreenCanvas3D extends Canvas3D {

    private static final long serialVersionUID = 1L;

    public MVis_OffScreenCanvas3D(GraphicsConfiguration graphicsConfiguration,
            boolean offScreen) {
        super(graphicsConfiguration, offScreen);
    }

    BufferedImage doRender(int width, int height) {

        BufferedImage bImage =
                new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        ImageComponent2D buffer =
                new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage);

        setOffScreenBuffer(buffer);
        renderOffScreenBuffer();
        waitForOffScreenRendering();
        bImage = getOffScreenBuffer().getImage();

        // To release the reference of buffer inside Java 3D.
        setOffScreenBuffer(null);

        return bImage;
    }

    /**
     * No-op since we always wait for off-screen rendering to complete.
     */
    @Override
    public void postSwap() {
    }
}