1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
* Main.java
*
* Created on September 26, 2006, 2:02 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.web3d.parser.x3d.X3DReader;
import org.web3d.vrml.export.Exporter;
import org.web3d.vrml.export.X3DBinaryRetainedDirectExporter;
import org.web3d.vrml.export.X3DBinarySerializer;
import org.web3d.vrml.sav.InputSource;
import org.web3d.vrml.sav.VRMLReader;
/*
* Need to include libraries in the project
*
*
*/
/* @author Franck Kolb */
public class vtkX3DBinaryConverter {
// Default Largest acceptable error for float quantization
private static float PARAM_FLOAT_LOSSY = 0.001f;
/** The VRMLReader */
private VRMLReader reader;
private static Exporter writer;
/** The compression method to use for binary */
private int compressionMethod;
/** The float lossy param */
private float quantizeParam;
private ByteArrayOutputStream writeStream;
private BufferedOutputStream bos;
/** Creates a new instance of vtkX3DBinaryConverter */
public vtkX3DBinaryConverter(String outputFileName) throws Exception {
this ( outputFileName, X3DBinarySerializer.METHOD_SMALLEST_NONLOSSY, PARAM_FLOAT_LOSSY);
}
/** Creates a new instance of vtkX3DBinaryConverter */
private vtkX3DBinaryConverter(String outputFileName, int method,
float quantizeParam) throws Exception {
// Get the input stream for the XML document
this.writeStream = new ByteArrayOutputStream();
compressionMethod = method;
this.quantizeParam = quantizeParam;
reader = new X3DReader();
// Set up output stream for fast infoset document
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFileName);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
bos = new BufferedOutputStream(fos);
}
public void Write(byte[] b) throws IOException
{
this.writeStream.write(b, 0, b.length);
}
public void Close() throws Exception
{
InputStream inputStream = new ByteArrayInputStream(
this.writeStream.toByteArray());
InputSource is = new InputSource(null, inputStream);
System.out.println("Write X3D binary file");
writer = new X3DBinaryRetainedDirectExporter(bos, 3, 1, null,
compressionMethod, quantizeParam);
reader.setContentHandler(writer);
reader.setRouteHandler(writer);
reader.setScriptHandler(writer);
reader.setProtoHandler(writer);
try {
reader.parse(is);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Create an instance of this class and run it.
*/
public static void main(String[] args) {
/*
* Set the compression method to use for binary compression. 4 mehods :
* Fasting parsing method Smallest parsing method Lossy parsing method
* Strings method
*/
// method = X3DBinarySerializer.METHOD_FASTEST_PARSING;
// method = X3DBinarySerializer.METHOD_SMALLEST_NONLOSSY;
// method = X3DBinarySerializer.METHOD_SMALLEST_LOSSY;
// method = X3DBinarySerializer.METHOD_STRINGS;
try{
System.out.println("Create converter");
vtkX3DBinaryConverter x = new vtkX3DBinaryConverter("out.x3db");
InputStream is = new BufferedInputStream(new FileInputStream("export.x3d"));
System.out.println("Read file: " + is.available());
byte b[] = new byte[is.available()];
is.read(b);
x.Write(b);
System.out.println("Done...");
x.Close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
|