Skip to main content

How to save an image of my 3d-view to disk?

1 reply [Last post]
fredand44
Offline
Joined: 2006-05-08

Hello!
I would like to save an image of my 3d-view in a Java3D app.
I have searched the net for a solution but I just get a black image if I
use paint and a white image if I use printAll.
Below is my code for a small app with 2 cubes that you can move around. if
you push VK_D you print the image to a file.
So if any one know the solution how to save a snapshot of the canvas3d
please let me know.
Best regards
Fredrik
The apps contians of 2 classes:

package java3d.test;
import java3d.*;
import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import Acme.JPM.Encoders.*;
//http://archives.java.sun.com/cgi-bin/wa?A2=ind9909&L=java3d-
interest&D=0&P=22136
public class Test extends Applet
{
BranchGroup branchGroup;
ColorCube colorCube1 = new ColorCube(0.4); //The cube that you can
navigate
ColorCube colorCube2 = new ColorCube(0.4);
TransformGroup transformGroup1;
Canvas3D canvas3D;
public void init()
{
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
//canvas3D = new Canvas3D(config, true);
canvas3D = new Canvas3D(config);
add("Center", canvas3D);
SimpleUniverse simpleUniverse = new SimpleUniverse
(canvas3D);
branchGroup = new BranchGroup();
//Cube1
transformGroup1 = new TransformGroup();
TestListener testListener = new TestListener
(transformGroup1, this);
Transform3D transform3D1 = new Transform3D();
transform3D1.set(new Vector3f(0.0f, 0.0f, -20.0f));
transformGroup1.setTransform(transform3D1);
transformGroup1.setCapability
(TransformGroup.ALLOW_TRANSFORM_WRITE);
TestCollisionBehavior collisionBehavior = new
TestCollisionBehavior(colorCube1, testListener);
BoundingSphere boundingSphere = new BoundingSphere(new
Point3d(0,0,0), 0.5);
collisionBehavior.setSchedulingBounds(boundingSphere);
transformGroup1.addChild(collisionBehavior);
transformGroup1.addChild(colorCube1);
branchGroup.addChild(transformGroup1);
canvas3D.addKeyListener( testListener );
//Cube2
TransformGroup transformGroup2 = new TransformGroup();
Transform3D transform3D2 = new Transform3D();
transform3D2.set(new Vector3f(0.0f, 2.0f, -20.0f));
transformGroup2.setTransform(transform3D2);
transformGroup2.addChild(colorCube2);
branchGroup.addChild(transformGroup2);
branchGroup.compile();
simpleUniverse.addBranchGraph(branchGroup);
}
public void saveImage()
{
try
{
System.out.println("saveImage");
System.out.println(canvas3D.getDoubleBufferAvailable
());
System.out.println(canvas3D.getDoubleBufferEnable
());

//ImageComponent2D imageComponent2D =
canvas3D.getOffScreenBuffer();
//BufferedImage bufferedImage =
imageComponent2D.getImage();
//BufferedImage bufferedImage = getCanvasImage();
//Graphics2D backBufferGraphics2D =
bufferedImage.createGraphics();
//backBufferGraphics2D.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//backBufferGraphics2D.drawImage(resultImage, 0, 0,
resultImage.getWidth(this), resultImage.getHeight(this), null);
//BufferedOutputStream bufferedOutputStream = new
BufferedOutputStream( new FileOutputStream( new File
("D:/Fredrik/test.jpg") ) );
//JPEGImageEncoder jPEGImageEncoder =
JPEGCodec.createJPEGEncoder(bufferedOutputStream);
//jPEGImageEncoder.encode( bufferedImage );
//writeImage("D:/Fredrik/test.jpg", bufferedImage);
saveComponentAsJPEG
(canvas3D, "D:/Fredrik/test2.jpg");
doCopy("D:/Fredrik/test2.jpg");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public BufferedImage getCanvasImage ()
{
GraphicsContext3D ctx = canvas3D.getGraphicsContext3D();
javax.media.j3d.Raster ras = new javax.media.j3d.Raster();
ras.setType (javax.media.j3d.Raster.RASTER_COLOR);
ras.setCapability (javax.media.j3d.Raster.ALLOW_IMAGE_READ);
Dimension d = getSize();
ras.setSize (d);
ras.setImage (new ImageComponent2D
(ImageComponent2D.FORMAT_RGB, d.width, d.height));
ctx.readRaster(ras);
// Now strip out the image info
ImageComponent2D img_src = ras.getImage();
// DepthComponent depth = ras.getDepthComponent();
return img_src.getImage();
}
public void writeImage (String fileName, BufferedImage bi)
{
try
{
File file = new File(fileName);
FileOutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam
(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
}
catch (Exception ex)
{
System.out.println ("Error writing image to file " +
fileName + ": " + ex.toString());
ex.printStackTrace();
}
}
public void saveComponentAsJPEG(Component myComponent, String
filename) {
Dimension size = myComponent.getSize();
BufferedImage myImage =
new BufferedImage(size.width, size.height,
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
//myComponent.printAll(g2);
try {
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
public void doCopy(String originalImage)
{
File f = new File(originalImage);
try
{
BufferedImage bufi = javax.imageio.ImageIO.read(f);
//
BufferedImage bi = new BufferedImage(bufi.getWidth
(this), bufi.getHeight(this), BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(bufi, 0, 0, this);
String newName = originalImage.substring(0,
originalImage.indexOf(".")) + "Copy.jpg";
javax.imageio.ImageIO.write(bi, "jpg", new File
(newName));
//
BufferedImage bi1 = new BufferedImage(bufi.getWidth
(this), bufi.getHeight(this), BufferedImage.SCALE_DEFAULT);
Graphics2D big1 = bi.createGraphics();
big1.drawImage(bufi, 0, 0, this);
String newName1 = originalImage.substring(0,
originalImage.indexOf(".")) + "CopySCALE_DEFAULT.jpg";
javax.imageio.ImageIO.write(bi1, "jpg", new File
(newName1));
String newName3 = originalImage.substring(0,
originalImage.indexOf(".")) + "CopyScreateJPEGEncoder.jpg";
OutputStream out = new FileOutputStream(newName3);
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufi);
out.close(); //
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
Frame frame = new MainFrame(new Test(), 600, 400);
}
}

package java3d.test;
import java3d.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.geometry.*;
public class TestListener implements KeyListener
{
final static float DISTANCE = 0.1f;
final static double TURNANGLE = 0.1;
final static float SHOOTING_DISTANCE = 20f;
float x = 0.0f;
float y = 0.0f;
float z = -20.0f;
private double angle = 0.0;
TransformGroup transformGroup;
Transform3D positionTransform3D = new Transform3D();
Transform3D shootingEndTransform3D = new Transform3D();
Transform3D angleTransform3D = new Transform3D();
Test test;
private boolean isMovePossible = true;
public TestListener(TransformGroup tfg, Test t)
{
transformGroup = tfg;
test = t;
positionTransform3D.set(new Vector3f(x, y, z));
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if( e.getKeyCode() == KeyEvent.VK_UP )
{
if(isMovePossible)
{
Transform3D temp = new Transform3D();
temp.set(new Vector3f(0, DISTANCE, 0));
positionTransform3D.mul(temp);
transformGroup.setTransform( positionTransform3D );
}
else
{
Transform3D temp = new Transform3D();
temp.set(new Vector3f(0, -DISTANCE, 0));
positionTransform3D.mul(temp);
transformGroup.setTransform( positionTransform3D );
setIsMovePossible(true);
}
}
else if( e.getKeyCode() == KeyEvent.VK_DOWN )
{
if(isMovePossible)
{
Transform3D temp = new Transform3D();
temp.set(new Vector3f(0, -DISTANCE, 0));
positionTransform3D.mul(temp);
transformGroup.setTransform( positionTransform3D );
}
else
{
Transform3D temp = new Transform3D();
temp.set(new Vector3f(0, DISTANCE, 0));
positionTransform3D.mul(temp);
transformGroup.setTransform( positionTransform3D );
setIsMovePossible(true);
}
}
else if( e.getKeyCode() == KeyEvent.VK_LEFT )
{
angle = angle + TURNANGLE;
angleTransform3D.rotZ(TURNANGLE);
positionTransform3D.mul(angleTransform3D);
transformGroup.setTransform( positionTransform3D );
}
else if( e.getKeyCode() == KeyEvent.VK_RIGHT )
{
angle = angle - TURNANGLE;
angleTransform3D.rotZ(-TURNANGLE);
positionTransform3D.mul(angleTransform3D);
transformGroup.setTransform( positionTransform3D );
}
else if( e.getKeyCode() == KeyEvent.VK_SPACE )
{
double[] cords = new double[16];
positionTransform3D.get(cords);
double x = cords[3];
double y = cords[7];
double z = cords[11];
Point3d start = new Point3d(x, y, z);
Transform3D startTransform3D = new Transform3D();
startTransform3D.set(cords);
startTransform3D.mul(angleTransform3D);
Transform3D endTransform3D = new Transform3D();
endTransform3D.set(new Vector3f(0, SHOOTING_DISTANCE, 0));
startTransform3D.mul(endTransform3D);
startTransform3D.get(cords);
x = cords[3];
y = cords[7];
z = cords[11];
Point3d end = new Point3d(x, y, z);
pick(start, end);
}
else if( e.getKeyCode() == KeyEvent.VK_D )
{
test.saveImage();
}
}
public void keyReleased(KeyEvent e)
{
}
public synchronized void setIsMovePossible(boolean b)
{
isMovePossible = b;
}
public void pick(Point3d start, Point3d end)
{
PickSegment pickSegment = new PickSegment(start, end);
SceneGraphPath[] sceneGraphPaths =
test.branchGroup.pickAllSorted(pickSegment);
if(sceneGraphPaths != null && sceneGraphPaths.length > 1)
{
if(sceneGraphPaths[1] != null)
{
Node node = sceneGraphPaths[1].getObject();
if(node != null)
{
Class c = node.getClass();
if(node instanceof ColorCube)
{
System.out.println("Hit " +
sceneGraphPaths.length);
}
}
}
}
}
public static void main(String[] args)
{
Frame frame = new MainFrame(new Test(), 600, 350);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
fredand44
Offline
Joined: 2006-05-08

Hello!

I found the soulution in the maillist archive. Unfortunately I can't find it right now but I got it at my computer at home. Do not hesitate to ask me for the solution and I will get back to you.

But for now we are closing this topic.

Best regards
Fredrik