Read From Binary File on Android - java

I have some data that I have saved into a file using Matlab. I have saved this data in Matlab as follows:
fwrite(fid,numImg2,'integer*4');
fwrite(fid,y,'integer*4');
fwrite(fid,imgName,'char*1');
fwrite(fid,a,'integer*4');
fwrite(fid,img.imageData,'double');
I read this data back into Matlab using the following code
fread(fid,1,'integer*4');// Returns numImg2
fread(fid,1,'integer*4');// Returns y which is the number of cha rectors in the image name, i use in the following line to read the image name, say for example if the image name is 1.jpg, then using the following will return the image name
fread(fid,5,'char*1');
fread(fid,1);
etc...
I want to be able to read this data on an android phone. This is the code I have at the moment.
DataInputStream ds = new DataInputStream(new FileInputStream(imageFile));
//String line;
// Read the first byte to find out how many images are stored in the file.
int x = 0;
byte numberOfImages;
int numImages = 0;
while(x<1)
{
numberOfImages = ds.readByte();
numImages = (int)numberOfImages;
x++;
}
int lengthName = 0;
String imgName = "";
for(int y=1; y<=numImages; y++)
{
lengthName = ds.readInt();
byte[] nameBuffer = new byte[lengthName];
char[] name = new char[lengthName];
for(int z = 1; z<=5;z++)
{
nameBuffer[z-1] = ds.readByte();
//name[z-1] = ds.readChar();
}
imgName = new String(nameBuffer);
//imgName = name.toString();
}
text.append(imgName);
I cannot seem to retrieve the image name as a string from the binary file data. Any help is much appreciated.

I'm not sure it will work but anyway:
byte[] nameBuffer = new byte[lengthName];
if(ds.read(nameBuffer) != lengthName) {
// error handling here
return;
}
imgName = new String(nameBuffer, "ISO-8859-1");

Related

part of the video from gridfs mongodb in java?

I have uploaded a video file .mp4(18MB) into gridfs . and trying to read it from java code .here are some points i am unable to move further
1) i can able to retrieve the whole video into byte array and able to play
2) for first Nbytes means starting from first chunk to n no of chunks also i can able to play using directly querying from fs.chunks ... as below and giving to servletOutputstream ..
DBCollection a= db.getCollection("fs.chunks");DBCursor cur1=a.find().limit(10);
System.out.println(cur1);
byte[] destination2 =new byte[2621440];
int length2 = 0;
while(cur1.hasNext()) {
byte[] b2 = (byte[]) cur1.next().get("data");
System.arraycopy(b2, 0, destination2, length2, b2.length);
length2 += b2.length;
System.out.println("##########");
System.out.println(destination2.length);
}
3) I was stuck here, while reading from middle of the chunks , means after skip(n) chunks in the find() operation , unable to play the video by windows media player.saying unable to codec and etc error.. am i trying in a right way ?
DBCollection a= db.getCollection("fs.chunks");
DBCursor cur1=a.find(new BasicDBObject("n",new BasicDBObject("$gt",9))).limit(10);
System.out.println(cur1);
byte[] destination2 =new byte[2621440];
int length2 = 0;
while(cur1.hasNext()) {
byte[] b2 = (byte[]) cur1.next().get("data");
System.arraycopy(b2, 0, destination2, length2, b2.length);
length2 += b2.length;
System.out.println("##########");
System.out.println(destination2.length);
}
...........
public void showVideos(Model model,HttpServletResponse response) throws IOException {............response.setHeader("Content-Type", "video/quicktime");
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");//byte[] bytearray =destination2
//response.s
ServletOutputStream out = response.getOutputStream();
System.out.println("hello");
int n=0;
//while(is.read(bytes, 0, 4096) != -1)
{
System.out.println(n++);
out.write(bytearray);
}
please suggest me for retrieving the part of the video file and play it from grid fs?
I'd use the GridFS classes for this purpose. Pseudo code below. myFS points to the bucket and findOne looks for the id of the file.
GridFS myFS = null;
if (bucket.isPresent()) {
myFS = new GridFS(m.getDb(), bucket.get());
} else {
myFS = new GridFS(m.getDb());
}
return Optional.fromNullable(myFS.findOne(id));

How to set image instead of boolean value?

I want to show image as a boolean value from a servlet to android app. Servlet will be uploaded to google app engine.
This is my servlet. "arrBool" value displays like some random value.
//
resp.setContentType("text/plain");
if (req.getParameterMap().containsKey("message"))
message = req.getParameter("message");
resp.getWriter().println("Server Said" + message);
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(arrBool);
}
}
and this is my android application file:
//
RestClient client = new RestClient("http://machougul01.appspot.com/listenforrestclient");
client.AddParam("message", "Hello two World");
// client.AddParam("arrBool", "textView1");
try
{
client.Execute(RequestMethod.GET);
}
catch (Exception e)
{
textView.setText(e.getMessage());
}
String response = client.getResponse();
textView.setText(response);
}
Output shows "Server said: Hello two world" and arrBool value: "m9a9990a m9a9990"
I want to set arrBool value as image instead of m9a9990a. So whenever randomly values are selected if it is true then number of cars will be shown from 1 - 6 out of 6.
Please help me with this.
Try changing
resp.getWriter().print(arrBool);
To
resp.getWriter().print(String.valueOf(arrBool));
It will write a string in your response.
or you can change this
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(arrBool);
}
}
To
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(1);
}
}
So when you read at response at client side you will get response line as
"Server said: Hello two world"
111
You can then parse string and no 1's or true's out and show or
You have a few things you need to do.
First, there's a few import statements you'll need. Maybe you can use apache commons for the base64, this example uses xerces.
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
Next you need to create an image out of your booleans:
BufferedImage image = new BufferedImage(wid,hgt,BufferedImage.TYPE_3BYTE_RGB);
final int BLACK = java.awt.Color.BLACK.getRGB();
final int WHITE = java.awt.Color.WHITE.getRGB();
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hgt; y++) {
boolean val = array[x*wid + y];
image.setRGB(x,y, val ? BLACK : WHITE);
}
}
I probably could have used a binary image there. This is what I knew offhand :) I'm sure you can tinker with it to change the formatting.
Then you need to convert it to Base64
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = out.toByteArray();
String imageText = Base64.encode(baos.toByteArray());
Then, you need to spit out some HTML that references that base 64:
String html = "<img src=\"data:image/bmp;" + imageText + "\" alt=\"Random booleans\">";
And spit that html out on your page and you should be all set!

String array throws ArrayIndexOutOfBoundsException after splitting a String

I am trying to develop a Web-Client based Online game using GWT.
The game mechanics work quite fine by now and I'd really like to go on with my next step in my development plan, but now I am stuck with a simple encoding/decoding function I created to store my data for the client into a string.
The steps I took are not that complicated. During runtime, my server creates "ImageStates" of the game Objects to be drawn on a Canvas on my client. Each ImageState contains a number of Layers, which contain Details of what has to be drawn where at what specific time and so on.
I tried ObjectInput/OutputStream, combined with GZip-Compression and ByteInputStreams, but I could not find any GWT implementation of the first two so I had to think about another solution and came across gwt-lzma.
My goal is to encode those details into a single string, compress it on the server, send the compressed Data to the client, decompress and decode it there so it can be "read" by the client. To do so, I created an encoding and a decoding function:
The encoder:
public static String encodeImageStateContainer(HashMap<String,HashMap<Long,ImageState>> imageStateContainer){
StringBuilder mainStringBuilder = new StringBuilder();
Iterator<Entry<String,HashMap<Long,ImageState>>> imageStateContainerIterator = imageStateContainer.entrySet().iterator();
while(imageStateContainerIterator.hasNext()){
Entry<String,HashMap<Long,ImageState>> tempImageStateManagerMetadata = imageStateContainerIterator.next();
HashMap<Long,ImageState> tempImageStateManager = tempImageStateManagerMetadata.getValue();
if(tempImageStateManager.size() > 0){
if(mainStringBuilder.length() > 0){
mainStringBuilder.append('}'); //Divisor between Image State Managers
}
mainStringBuilder.append(tempImageStateManagerMetadata.getKey());
mainStringBuilder.append('|'); //Divisor between Name and following Data
StringBuilder subOneStringBuilder = new StringBuilder();
Iterator<Entry<Long,ImageState>> tempImageStateManagerIterator = tempImageStateManager.entrySet().iterator();
while(tempImageStateManagerIterator.hasNext()){
Entry<Long,ImageState> tempImageStateMetaData = tempImageStateManagerIterator.next();
if(subOneStringBuilder.length() > 0){
subOneStringBuilder.append(')'); //Divisor between Image Objects
}
subOneStringBuilder.append(tempImageStateMetaData.getKey());
subOneStringBuilder.append('-'); //Divisor between Object Id and Image State Data
StringBuilder subTwoStringBuilder = new StringBuilder();
ImageState tempImageState = tempImageStateMetaData.getValue();
subOneStringBuilder.append(tempImageState.getFirstFrameId()); //1. => First Frame Id
subOneStringBuilder.append(';'); //Divisor between Image State data types
subOneStringBuilder.append(tempImageState.getFramesPerLayer()); //2. => Total Frame Count
subOneStringBuilder.append(';');
subOneStringBuilder.append(tempImageState.getMinumimScaleFactor()); //3. => Minimum Scale Factor
subOneStringBuilder.append(';');
ImageStateLayer[] tempImageStateLayers = tempImageState.getImageStateLayers();
for(int layerId = 0; layerId < tempImageStateLayers.length; ++layerId){
if(subTwoStringBuilder.length() > 0){
subTwoStringBuilder.append('/'); //Divisor between Image State Layers
}
//Frame Arrays
String[] imageNativePath = tempImageStateLayers[layerId].getImageNativePath();
short[] offsetX = tempImageStateLayers[layerId].getOffsetX();
short[] offsetY = tempImageStateLayers[layerId].getOffsetY();
short[] orientation = tempImageStateLayers[layerId].getOrientation();
//Finalization Arrays
byte[] imagePathChange = tempImageStateLayers[layerId].getImagePathChange();
byte[] offsetXChange = tempImageStateLayers[layerId].getOffsetXChange();
byte[] offsetYChange = tempImageStateLayers[layerId].getOffsetYChange();
byte[] orientationChange = tempImageStateLayers[layerId].getOrientationChange();
//Image Path Data
StringBuilder subThreeStrignBuilder = new StringBuilder();
for(int imagePathId = 0; imagePathId < imageNativePath.length; ++imagePathId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(imageNativePath[imagePathId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(imagePathChange[imagePathId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//OffsetX Data
subThreeStrignBuilder = new StringBuilder();
for(int offsetXId = 0; offsetXId < offsetX.length; ++offsetXId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(offsetX[offsetXId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(offsetXChange[offsetXId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//OffsetY Data
subThreeStrignBuilder = new StringBuilder();
for(int offsetYId = 0; offsetYId < offsetY.length; ++offsetYId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(offsetY[offsetYId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(offsetYChange[offsetYId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//Orientation Data
subThreeStrignBuilder = new StringBuilder();
for(int orientationId = 0; orientationId < orientation.length; ++orientationId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(orientation[orientationId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(orientationChange[orientationId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
}
subOneStringBuilder.append(subTwoStringBuilder.toString());
}
mainStringBuilder.append(subOneStringBuilder.toString());
}
}
return mainStringBuilder.toString();
}
The decoder:
public static HashMap<String,HashMap<Long,ImageState>> decodeImageStateContainer(String data){
String[] imageStateManagerArray = data.split("\\}");
HashMap<String,HashMap<Long,ImageState>> imageStateManagerContainer = new HashMap<String,HashMap<Long,ImageState>>(imageStateManagerArray.length);
for(int managerId = 0; managerId < imageStateManagerArray.length; ++managerId){
String[] tempImageStateData = imageStateManagerArray[managerId].split("\\|");
HashMap<Long,ImageState> tempImageStateManager = new HashMap<Long,ImageState>();
imageStateManagerContainer.put(tempImageStateData[0], tempImageStateManager);
String[] tempImageStateManagerObjects = tempImageStateData[1].split("\\)");
for(int objectId = 0; objectId < tempImageStateManagerObjects.length; ++objectId){
String[] tempImageObjectData = tempImageStateManagerObjects[objectId].split("\\-");
long imageObjectId = Long.parseLong(tempImageObjectData[0]);
String[] imageStateMetaData = tempImageObjectData[1].split("\\;");
ImageState tempImageState = new ImageState(Integer.parseInt(imageStateMetaData[1]), Integer.parseInt(imageStateMetaData[0]), Integer.parseInt(imageStateMetaData[2]));
tempImageStateManager.put(imageObjectId, tempImageState);
String[] tempImageStateLayerMetaData = imageStateMetaData[3].split("\\/");
ImageStateLayer[] tempImageStateLayers = new ImageStateLayer[tempImageStateLayerMetaData.length];
for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");
String[] imageNativePathDetails = layerElements[0].split("\\#");
String[] offsetXDetails = layerElements[1].split("\\#");
String[] offsetYDetails = layerElements[2].split("\\#");
**String[] orientationDetails = layerElements[3].split("\\#");**
//Image Path Data
String[] imageNativePaths = new String[imageNativePathDetails.length];
byte[] imagePathChange = new byte[imageNativePathDetails.length];
for(int id = 0; id < imageNativePathDetails.length; ++id){
String[] imagePathDetailElements = imageNativePathDetails[id].split("\\,");
imageNativePaths[id] = imagePathDetailElements[0];
imagePathChange[id] = Byte.parseByte(imagePathDetailElements[1]);
}
//OffsetX Data
short[] offsetX = new short[offsetXDetails.length];
byte[] offsetXChange = new byte[offsetXDetails.length];
for(int id = 0; id < offsetXDetails.length; ++id){
String[] offsetXDetailElements = offsetXDetails[id].split("\\,");
offsetX[id] = Short.parseShort(offsetXDetailElements[0]);
offsetXChange[id] = Byte.parseByte(offsetXDetailElements[1]);
}
//OffsetY Data
short[] offsetY = new short[offsetYDetails.length];
byte[] offsetYChange = new byte[offsetYDetails.length];
for(int id = 0; id < offsetYDetails.length; ++id){
String[] offsetYDetailElements = offsetYDetails[id].split("\\,");
offsetY[id] = Short.parseShort(offsetYDetailElements[0]);
offsetYChange[id] = Byte.parseByte(offsetYDetailElements[1]);
}
//Orientation Data
short[] orientation = new short[orientationDetails.length];
byte[] orientationChange = new byte[orientationDetails.length];
for(int id = 0; id < orientationDetails.length; ++id){
String[] orientationDetailElements = orientationDetails[id].split("\\,");
orientation[id] = Short.parseShort(orientationDetailElements[0]);
orientationChange[id] = Byte.parseByte(orientationDetailElements[1]);
}
//Create the Layer and Add it to the Array
tempImageStateLayers[layerId] = new ImageStateLayer(imageNativePaths,new short[][]{offsetX,offsetY,orientation}, new byte[][]{imagePathChange,offsetXChange,offsetYChange,orientationChange});
}
//Connect the Reference to the layers with the Image State
tempImageState.setImageStateLayers(tempImageStateLayers);
}
}
return imageStateManagerContainer;
}
Now my problem: If I encode it on the server and decode it directly afterwards It takes about three cycles of Encoding until I get an IndexOutofBoundsException at this specific position in the decoder, the last line is the line where the error seems to be according to stacktrace:
for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");
String[] imageNativePathDetails = layerElements[0].split("\\#");
String[] offsetXDetails = layerElements[1].split("\\#");
String[] offsetYDetails = layerElements[2].split("\\#");
String[] orientationDetails = layerElements[3].split("\\#");
I would not say I'm an expert, but after testing the encoder I can definitely say that the string generated by it is always valid, the string at which I get this error is complete as well. There must be something wrong with my decoding function, but I have no idea what it could be.
As I said, the first three cycles are encoded and decoded properly and the data to encode did not change dramatically. There is no way that the encoded string could lead to having a String array smaller than 4 elements. Basically, this error can't exist as far as I understand my code.
I think it could be some kind of memory allocation problem that prevents the string from being split properly, but thats just an idea of a clueless programmer.
Any help is deeply appreciated!
I found the solution to my problem:
While encoding, I was printing Numeric types to my string. While selecting my Seperator symbols I didn't take negative values, resulting in a '-' before the digit into consideration, which led to an uncontrolled splitting of my String when I tryed to split by '-'. I replaced the minus with another character and everything works fine now!

In Java, retrieve a JPEG from a URL and convert it to binary or hexadecimal form suitable for embedding in an RTF document

I'm trying to write a simple RTF document pretty much from scratch in Java, and I'm trying to embed JPEGs in the document. Here's an example of a JPEG (a 2x2-pixel JPEG consisting of three white pixels and a black pixel in the upper left, if you're curious) embedded in an RTF document (generated by WordPad, which converted the JPEG to WMF):
{\pict\wmetafile8\picw53\pich53\picwgoal30\pichgoal30
0100090000036e00000000004500000000000400000003010800050000000b0200000000050000
000c0202000200030000001e000400000007010400040000000701040045000000410b2000cc00
020002000000000002000200000000002800000002000000020000000100040000000000000000
000000000000000000000000000000000000000000ffffff00fefefe0000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000
0000001202af0801010000040000002701ffff030000000000
}
I've been reading the RTF specification, and it looks like you can specify that the image is a JPEG, but since WordPad always converts images to WMF, I can't see an example of an embedded JPEG. So I may also end up needing to transcode from JPEG to WMF or something....
But basically, I'm looking for how to generate the binary or hexadecimal (Spec, p.148: "These pictures can be in hexadecimal (the default) or binary format.") form of a JPEG given a file URL.
Thanks!
EDIT: I have the stream stuff working all right, I think, but still don't understand exactly how to encode it, because whatever I'm doing, it's not RTF-readable. E.g., the above picture instead comes out as:
ffd8ffe00104a464946011106006000ffdb0430211211222222223533333644357677767789b988a877adaabcccc79efdcebcccffdb04312223336336c878ccccccccccccccccccccccccccccccccccccccccccccccccccffc0011802023122021113111ffc401f001511111100000000123456789abffc40b5100213324355440017d123041151221314161351617227114328191a182342b1c11552d1f024336272829a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc401f103111111111000000123456789abffc40b51102124434754401277012311452131612415176171132232818144291a1b1c19233352f0156272d1a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda0c31021131103f0fdecf09f84f4af178574cd0b42d334fd1744d16d22bd3f4fb0b74b6b5bb78902450c512091c688aaaa8a0500014514507ffd9
This PHP library would do the trick, so I'm trying to port the relevant portion to Java. Here is is:
$imageData = file_get_contents($this->_file);
$size = filesize($this->_file);
$hexString = '';
for ($i = 0; $i < $size; $i++) {
$hex = dechex(ord($imageData{$i}));
if (strlen($hex) == 1) {
$hex = '0' . $hex;
}
$hexString .= $hex;
}
return $hexString;
But I don't know what the Java analogue to dechex(ord($imageData{$i})) is. :( I got only as far as the Integer.toHexString() function, which takes care of the dechex part....
Thanks all. :)
Given a file URL for any file you can get the corresponding bytes by doing (exception handling omitted for brevity)...
int BUF_SIZE = 512;
URL fileURL = new URL("http://www.somewhere.com/someurl.jpg");
InputStream inputStream = fileURL.openStream();
byte [] smallBuffer = new byte[BUF_SIZE];
ByteArrayOutputStream largeBuffer = new ByteArrayOutputStream();
int numRead = BUF_SIZE;
while(numRead == BUF_SIZE) {
numRead = inputStream.read(smallBuffer,0,BUF_SIZE);
if(numRead > 0) {
largeBuffer.write(smallBuffer,0,BUF_SIZE);
}
}
byte [] bytes = largeBuffer.toByteArray();
I'm looking at your PHP snippet now and realizing that RTF is a bizarre specification! It looks like each byte of the image is encoded as 2 hex digits (which doubles the size of the image for no apparent reason). The the entire thing is stored in raw ASCII encoding. So, you'll want to do...
StringBuilder hexStringBuilder = new StringBuilder(bytes.length * 2);
for(byte imageByte : bytes) {
String hexByteString = Integer.toHexString(0x000000FF & (int)imageByte);
if(hexByteString .size() == 1) {
hexByteString = "0" + hexByteString ;
}
hexStringBuilder.append(hexByteString);
}
String hexString = hexStringBuilder.toString();
byte [] hexBytes = hexString.getBytes("UTF-8"); //Could also use US-ASCII
EDIT: Updated code sample to pad 0's on the hex bytes
EDIT: negative bytes were getting logically right shifted when converted to ints >_<
https://joseluisbz.wordpress.com/2013/07/26/exploring-a-wmf-file-0x000900/
Maybe help you this:
String HexRTFBytes = "Representations text of bytes from Image RTF File";
String Destiny = "The path of the output File";
FileOutputStream wmf;
try {
wmf = new FileOutputStream(Destiny);
HexRTFBytes = HexRTFBytes.replaceAll("\n", ""); //Erase New Lines
HexRTFBytes = HexRTFBytes.replaceAll(" ", ""); //Erase Blank spaces
int NumBytesWrite = HexRTFBytes.length();
int WMFBytes = NumBytesWrite/2;//One byte is represented by 2 characters
byte[] ByteWrite = new byte[WMFBytes];
for (int i = 0; i < WMFBytes; i++){
se = HexRTFBytes.substring(i*2,i*2+2);
int Entero = Integer.parseInt(se,16);
ByteWrite[i] = (byte)Entero;
}
wmf.write(ByteWrite);
wmf.close();
}
catch (FileNotFoundException fnfe)
{System.out.println(fnfe.toString());}
catch (NumberFormatException fnfe)
{System.out.println(fnfe.toString());}
catch (EOFException eofe)
{System.out.println(eofe.toString());}
catch (IOException ioe)
{System.out.println(ioe.toString());}
This code take the representation in one string, and result is stored in a file.
https://joseluisbz.wordpress.com/2011/06/22/script-de-clases-rtf-para-jsp-y-php/
Now if you want to obtain the representation of the image file, you can use this:
private void ByteStreamImageString(byte[] ByteStream) {
this.Format = 0;
this.High = 0;
this.Wide = 0;
this.HexImageString = "Error";
if (ByteStream[0]== (byte)137 && ByteStream[1]== (byte)80 && ByteStream[2]== (byte)78){
this.Format = PNG; //PNG
this.High = this.Byte2PosInt(ByteStream[22],ByteStream[23]);
this.Wide = this.Byte2PosInt(ByteStream[18],ByteStream[19]);
}
if (ByteStream[0]== (byte)255 && ByteStream[1]== (byte)216
&& ByteStream[2]== (byte)255 && ByteStream[3]== (byte)224){
this.Format = JPG; //JPG
int PosJPG = 2;
while (PosJPG < ByteStream.length){
String M = String.format("%02X%02X", ByteStream[PosJPG+0],ByteStream[PosJPG+1]);
if (M.equals("FFC0") || M.equals("FFC1") || M.equals("FFC2") || M.equals("FFC3")){
this.High = this.Byte2PosInt(ByteStream[PosJPG+5],ByteStream[PosJPG+6]);
this.Wide = this.Byte2PosInt(ByteStream[PosJPG+7],ByteStream[PosJPG+8]);
}
if (M.equals("FFDA")) {
break;
}
PosJPG = PosJPG+2+this.Byte2PosInt(ByteStream[PosJPG+2],ByteStream[PosJPG+3]);
}
}
if (this.Format > 0) {
this.HexImageString = "";
int Salto = 0;
for (int i=0;i < ByteStream.length; i++){
Salto++;
this.HexImageString += String.format("%02x", ByteStream[i]);
if (Salto==64){
this.HexImageString += "\n"; //To make readable
Salto = 0;
}
}
}
}

Open Microsoft Word in Java

I'm trying to open MS Word 2003 document in java, search for a specified String and replace it with a new String. I use APACHE POI to do that. My code is like the following one:
public void searchAndReplace(String inputFilename, String outputFilename,
HashMap<String, String> replacements) {
File outputFile = null;
File inputFile = null;
FileInputStream fileIStream = null;
FileOutputStream fileOStream = null;
BufferedInputStream bufIStream = null;
BufferedOutputStream bufOStream = null;
POIFSFileSystem fileSystem = null;
HWPFDocument document = null;
Range docRange = null;
Paragraph paragraph = null;
CharacterRun charRun = null;
Set<String> keySet = null;
Iterator<String> keySetIterator = null;
int numParagraphs = 0;
int numCharRuns = 0;
String text = null;
String key = null;
String value = null;
try {
// Create an instance of the POIFSFileSystem class and
// attach it to the Word document using an InputStream.
inputFile = new File(inputFilename);
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
fileSystem = new POIFSFileSystem(bufIStream);
document = new HWPFDocument(fileSystem);
docRange = document.getRange();
numParagraphs = docRange.numParagraphs();
keySet = replacements.keySet();
for (int i = 0; i < numParagraphs; i++) {
paragraph = docRange.getParagraph(i);
text = paragraph.text();
numCharRuns = paragraph.numCharacterRuns();
for (int j = 0; j < numCharRuns; j++) {
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
System.out.println("Character Run text: " + text);
keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
key = keySetIterator.next();
if (text.contains(key)) {
value = replacements.get(key);
charRun.replaceText(key, value);
docRange = document.getRange();
paragraph = docRange.getParagraph(i);
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
}
}
}
}
bufIStream.close();
bufIStream = null;
outputFile = new File(outputFilename);
fileOStream = new FileOutputStream(outputFile);
bufOStream = new BufferedOutputStream(fileOStream);
document.write(bufOStream);
} catch (Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows.............");
ex.printStackTrace(System.out);
}
}
I call this function with following arguments:
HashMap<String, String> replacements = new HashMap<String, String>();
replacements.put("AAA", "BBB");
searchAndReplace("C:/Test.doc", "C:/Test1.doc", replacements);
When the Test.doc file contains a simple line like this : "AAA EEE", it works successfully, but when i use a complicated file it will read the content successfully and generate the Test1.doc file but when I try to open it, it will give me the following error:
Word unable to read this document. It may be corrupt.
Try one or more of the following:
* Open and repair the file.
* Open the file with Text Recovery converter.
(C:\Test1.doc)
Please tell me what to do, because I'm a beginner in POI and I have not found a good tutorial for it.
First of all you should be closing your document.
Besides that, what I suggest doing is resaving your original Word document as a Word XML document, then changing the extension manually from .XML to .doc . Then look at the XML of the actual document you're working with and trace the content to make sure you're not accidentally editing hexadecimal values (AAA and EEE could be hex values in other fields).
Without seeing the actual Word document it's hard to say what's going on.
There is not much documentation about POI at all, especially for Word document unfortunately.
I don't know : is its OK to answer myself, but Just to share the knowledge, I'll answer myself.
After navigating the web, the final solution i found is :
The Library called docx4j is very good for dealing with MS docx file, although its documentation is not enough till now and its forum is still in a beginning steps, but overall it help me to do what i need..
Thanks 4 all who help me..
You could try OpenOffice API, but there arent many resources out there to tell you how to use it.
You can also try this one: http://www.dancrintea.ro/doc-to-pdf/
Looks like this could be the issue.

Categories