aspose file tools
The moose likes Java in General and the fly likes How Parse Multiple images contained in a single file stream Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How Parse Multiple images contained in a single file stream" Watch "How Parse Multiple images contained in a single file stream" New topic
Author

How Parse Multiple images contained in a single file stream

James Frankman
Greenhorn

Joined: Jun 01, 2006
Posts: 12
I have a project where a large number of tiff image files are concatenated together into one file (i.e. imagefile.img) and sent to us from another company. Accompanying this file is an index that give the "Image Starting Location" and the "Image length" in bytes is listed sequentially in a text file (i.e. ImageIndex.txt). For instance, two entries in the index file would looks something like this:

image1.tif 000000000000000000 000013220
image2.tif 000000000000013220 000012762

I would need to start at the beginning of the the image file read the first 13220 bytes and save those bytes as a .tif image. I would then need start at postion 13220 and read the next 12762 bytes and so on until all images are removed from the image file.

I assume I would need to use the Java Standard I/O libraries, but am not really sure since I have never done anything like this. What are my options for parsing through a large binary file and saving the tiff images contained therein as separate files?

Thanks.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Well it should already be encoded as a TIF to begin with so the file type is somewhat irrelevent. I think all you need to do is get a FileInputStream and pass the first x bytes to a FileOutputStream that is writing to the appropriate File. Then pass the next y bytes to a different FileOutputStream, etc.
James Frankman
Greenhorn

Joined: Jun 01, 2006
Posts: 12
Thanks, I think I understand, but I have not used the java.io library very much. I am a bit green and have mostly focused on Web and O/R stuff. I think I could figure this out on my own, but it would be helpful if you could post a snippet of code. (Perhaps I should have posted this in the beginners forum, if so, my apologies)

Thanks.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's common fot .tiff files to contain multiple images. I suggest you check out the various javax.imageio packages. Reading such an image file is exactly what they were designed for! For example, javax.imageio.ImageReader has methods like:

public int getNumImages(boolean allowSearch) throws IOException
public BufferedImage read(int imageIndex) throws IOException

Too easy!


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Jeff Albertson:
It's common fot .tiff files to contain multiple images. I suggest you check out the various javax.imageio packages. Reading such an image file is exactly what they were designed for! For example, javax.imageio.ImageReader has methods like:

public int getNumImages(boolean allowSearch) throws IOException
public BufferedImage read(int imageIndex) throws IOException

Too easy!


He doesn't want to decode the image file and the standard javax.imageio package does not support TIF anyway, he'd have to go to JAI Image I/O for that. However, all of this is unnecessary for simply writing each x bytes to a different file.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See the JavaDoc for FileInputStream and FileOutputStream. You can read from a file into a byte array and write from the array to a file. If you have enough memory to make an array of exactly the number of bytes specified in the index you can do each file with a single read & write.

For more detail, see the stream tutorial: http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

Google for "anytopic java tutorial" almost always gets a winner from Sun or some university class handouts. Let us know how it goes


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How Parse Multiple images contained in a single file stream
 
Similar Threads
JAI - creating multi page tiff images
Multi display Applet
Reading tif files on x64 system
Read Multiple Tiff images
Multiple Files in an InputStream