File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Merging data With image" Watch "Merging data With image" New topic

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() );
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 && (, 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

Joined: Oct 29, 2001
Posts: 9189

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.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Merging data With image
It's not a secret anymore!