wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Merging data With image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Merging data With image" Watch "Merging data With image" New topic
Author

Merging data With image

PJ Crump
Ranch Hand

Joined: Feb 06, 2002
Posts: 51
I have 1 file that has records in it and a directory that contains images (one image per data record).

I read the data record and then read the matching image and then merge them and create a new record that contains both. The issue is that when I write out the merged data and look at the image in a viewer it does not look right (it looks scrambled compared to the original)..

Basically, here is what I'm doing.. Got any hints??


Image image = Toolkit.getDefaultToolkit().getImage(dataRec.getPhotoFileName() );
ImageSizer.waitForImage(image);
int imageWidth = image.getWidth( null );
int imageHeight = image.getHeight( null );
if (imageWidth != maxImageWidth || imageHeight != maxImageHeight) {
System.out.println("Error image not sized correctly");
}


long length;
byte[] bytes = null;
InputStream is = null;

is = new FileInputStream(file);
length = is.available();

if (length > Integer.MAX_VALUE) {
throw new Exception("Error Converting File - File Too Large To Handle ("+file.getName()+"), length of file was "+length+" max allowable length is "+Integer.MAX_VALUE);
}

bytes = new byte[(int)length];

int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}


-- I then take the bytes and append them to the end of the output record..




byte[] bb = FiletoBytes.convert(dataRec.getPhotoFileName());

I then take the bytes and append them on to the end of the output record.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by PJ Crump:

length = is.available();

First and foremost, available() doesn't do what you think it does:

public int available() throws IOException
Returns the number of bytes that can be read from this file input stream without blocking.

Java API Doc for FileInputStream
Second, image files have specific formats. You can't just add data to them and expect them to be correct.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Merging data With image
 
Similar Threads
unable to preserve newline
Convert image to raw binary data
how to insert carriage return
Using streams
Obtaining the contents of a binary file