Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

How to create a TIFF image from a binary raw data

Thread view: 
huayingyang@gmail.com - 19 Oct 2007 16:44 GMT
Hello all, I am having difficulty creating a TIFF Image file from a
binary raw data.
I tried to used ImageIO or ImageJ but with no luck.  My implementation
is as follows:

import javax.imageio.*;
import javax.imageio.stream.*;
import ij.ImagePlus;
import ij.io.Opener;
import java.io.*;
import java.awt.Image;

protected Image load(byte[] data) {
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    Image image = null;
    try {
               // Using ImageIO
        //image = ImageIO.read(is);
        //ImageWaiter.wait(image);

               // Using ImageJ
        ImagePlus imagej = new Opener().openTiff(is, "cmfdata");
        if (imagej != null) {
            image = imagej.getImage();
        }
    }
    catch (Throwable e) {
        System.out.println("Error occurs");
        image = null;
    }
    return image;
}

If you can spot something wrong with above code, please kindly inform
me.  Any thoughtful ideas are greatly appreciated.
Andrew Thompson - 19 Oct 2007 17:14 GMT
>If you can spot something wrong with above code, please kindly inform
>me.  

D:\projects\junk\numbered\484Image\Image.java:8: class, interface, or enum
expected
protected Image load(byte[] data) {
         ^
D:\projects\junk\numbered\484Image\Image.java:10: class, interface, or enum
expected
   Image image = null;
   ^
D:\projects\junk\numbered\484Image\Image.java:11: class, interface, or enum
expected
   try {
   ^
D:\projects\junk\numbered\484Image\Image.java:18: class, interface, or enum
expected
       if (imagej != null) {
       ^
D:\projects\junk\numbered\484Image\Image.java:20: class, interface, or enum
expected
       }
       ^
D:\projects\junk\numbered\484Image\Image.java:24: class, interface, or enum
expected
       image = null;
       ^
D:\projects\junk\numbered\484Image\Image.java:25: class, interface, or enum
expected
   }
   ^
D:\projects\junk\numbered\484Image\Image.java:27: class, interface, or enum
expected
}
^
8 errors

> Any thoughtful ideas are greatly appreciated.

I recommend posting SSCCE code.
<http://www.physci.org/codes/sscce.html>

<dws>Was that 'thoughtful' enough?</dws>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

huayingyang@gmail.com - 19 Oct 2007 19:07 GMT
Hello,

Thank you for your response.  I don't know how you compiled the load
method.  I used Eclipse and there is no compilation error.  The
problem is that this method doesn't work as expected, i.e. image is
always equal to null.  I am looking for a way to create a TIFF image
from raw binary data...

Thanks,
Helen
Lew - 19 Oct 2007 20:45 GMT
> Thank you for your response.  I don't know how you compiled the load
> method.  I used Eclipse and there is no compilation error.  

Impossible.  You cannot compile just one method, as you posted, it has to be
inside a class definition.

Where is the class declaration?

The code you posted will not compile in Eclipse or anywhere else.
> import javax.imageio.*;
> import javax.imageio.stream.*;
[quoted text clipped - 4 lines]
>
> protected Image load(byte[] data) {

Notice: no class declaration.

Signature

Lew

Jeff Higgins - 20 Oct 2007 01:40 GMT
huayingyang wrote:
> Hello all, I am having difficulty creating a TIFF Image file from a
> binary raw data.
> I tried to used ImageIO or ImageJ but with no luck.  My implementation
> is as follows:

Your question is not clear to me.
Several interpretations that I can come up with:

1. You wish to construct a java.awt.Image from a TIFF file, or
from an array of bytes which represent the contents of a valid
TIFF file.

Please run the following small program, if it does not output
something like tif, tiff, TIF, or TIFF, you probably cannot
use java.imageio.ImageIO.read.

I am not familiar with ImageJ.

You might be able to use Java Advanced Imaging.
A (naive) example follows at the end of this post.

public class PrintReaders
{
 public static void main(String[] args)
 {
   for (String s : ImageIO.getReaderFormatNames())
     System.out.println(s);
 }
}

2. You have an array of bytes which represent some image data,
and you wish to construct a TIFF file from this (raw) data.

First you will need to determine exactly what kind of data you
have, and whether the TIF format is able to accomodate this
type of data. See the TIFF specification at:
<http://partners.adobe.com/public/developer/tiff/index.html>

You will then need to write the appropriate TIFF metadata and data
to a file.  You can do this manually or perhaps there is some
Java language API to do this.

3. You have some (raw) data from a device, such as a camera or
scanner you wish to convert to a TIFF file.

There is probably some software associated with the device that
will convert the raw data to a TIFF file. Else, see:
<http://en.wikipedia.org/wiki/RAW_image_format>
for some introductory discussion on raw image data, if you
are not already familiar with it.

import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.awt.Image;
import java.awt.image.RenderedImage;

public class ImageLoader
{
 public static void main(String[] args)
 {
   for (String s : ImageIO.getReaderFormatNames())
     System.out.println(s);
   try
   {
     FileInputStream in =
       new FileInputStream("c:\\temp\\ccitt_8.tif");
     FileChannel channel = in.getChannel();
     ByteBuffer buffer =
       ByteBuffer.allocate((int)channel.size());
     channel.read(buffer);
     load(buffer.array());
   }
   catch (FileNotFoundException e)
   {
     e.printStackTrace();
   }
   catch (IOException e)
   {
     e.printStackTrace();
   }
 }

 static Image load(byte[] data)
 {
   Image image = null;
   try
   {
     SeekableStream stream =
       new ByteArraySeekableStream(data);
     String[] names =
       ImageCodec.getDecoderNames(stream);
     ImageDecoder dec =
       ImageCodec.createImageDecoder(names[0], stream, null);
     RenderedImage im =
       dec.decodeAsRenderedImage();
     image =
       PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
   return image;
 }
}

> import javax.imageio.*;
> import javax.imageio.stream.*;
[quoted text clipped - 26 lines]
> If you can spot something wrong with above code, please kindly inform
> me.  Any thoughtful ideas are greatly appreciated.
Andrew Thompson - 20 Oct 2007 06:40 GMT
...
>Please run the following small program, if it does not output
>something like tif, tiff, TIF, or TIFF, you probably cannot
>use java.imageio.ImageIO.read.

No - here is the list reported as recognised by
ImageIO for this Win based Java 1.6 unit.

BMP
bmp
jpg
JPG
wbmp
jpeg
png
PNG
JPEG
WBMP
GIF
gif

(Though I successfully loaded and displayed two different
TIF images, using a slight variant of your code.)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Jeff Higgins - 20 Oct 2007 10:43 GMT
> ..
>>Please run the following small program, if it does not output
[quoted text clipped - 19 lines]
> (Though I successfully loaded and displayed two different
> TIF images, using a slight variant of your code.)

Would you mind telling your variations?
I swiped the code (with slight variations) from the
JAI-Demo project - JAIImageReader.java.
The source can be viewed here:
<http://preview.tinyurl.com/yubqol>
Thanks,
JH
Andrew Thompson - 20 Oct 2007 12:25 GMT
...
>Would you mind telling your variations?

Not at all.  In fact, I'm glad you asked.  I was tempted to
post it in my reply, but the changes were so trivial I thought
best not at the time.

<sscce>
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.awt.Image;
import java.awt.image.RenderedImage;

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class ImageLoader
{
 public static void main(String[] args)
 {
   for (String s : ImageIO.getReaderFormatNames())
   System.out.println(s);
   try
   {
     String path;
     if (args.length==0)
     {
       path = JOptionPane
         .showInputDialog(
           null,
           "Image Path",
           "D:/PP/GAMMA.tif");
     }
     else
     {
       path = args[0];
     }
     FileInputStream in =
       new FileInputStream(path);
     FileChannel channel = in.getChannel();
     ByteBuffer buffer =
       ByteBuffer.allocate((int)channel.size());
     channel.read(buffer);
     Image image = load(buffer.array());

     System.out.println("image: " + path + "\n" + image);
     JOptionPane.showMessageDialog(null,
       new JLabel(
       new ImageIcon( image )) );
   }
   catch (FileNotFoundException e)
   {
     e.printStackTrace();
   }
   catch (IOException e)
   {
     e.printStackTrace();
   }
 }

 static Image load(byte[] data)
 {
   Image image = null;
   try
   {
     SeekableStream stream =
       new ByteArraySeekableStream(data);
     String[] names =
       ImageCodec.getDecoderNames(stream);
     ImageDecoder dec =
       ImageCodec.createImageDecoder(
         names[0],
         stream,
         null);
     RenderedImage im =
       dec.decodeAsRenderedImage();
     image =
       PlanarImage.
         wrapRenderedImage(im).
         getAsBufferedImage();
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
   return image;
 }
}
</sscce>

<beseechingly>
You'll have to forgive my failure to wrap those calls
to Swing methods in a Runnable, (shrugs) or perhaps
not.  In any case, I am confident you are capable of
doing that yourself, and I wanted to post the code
*exactly* as I'd seen it work.

Oh, and if I was going to take it beyond 'absolutely trivial'
changes, I would probably swap that first input dialog for
a JFileChooser.
</beseechingly>

>I swiped the code (with slight variations) from the
>JAI-Demo project - JAIImageReader.java.
>The source can be viewed here:
><http://preview.tinyurl.com/yubqol>

Cool.  Thanks to 'aastha' for the original code, and you
for the alterations and link.  That was actually the first
time I'd played with JAI, your post 'inspired me'.  :-)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Jeff Higgins - 20 Oct 2007 13:13 GMT
> ..
>>Would you mind telling your variations?
>
> Not at all.  In fact, I'm glad you asked.  I was tempted to
> post it in my reply, but the changes were so trivial I thought
> best not at the time.

Nice, thanks.
JH
Andrew Thompson - 20 Oct 2007 13:25 GMT
...
>...if I was going to take it beyond 'absolutely trivial'
>changes, I would probably swap that first input dialog for
>a JFileChooser.

Or perhaps to an URL pulling an image directly off the World
Wild Web.  There seem to be a few 'floating about'.
<http://images.google.com/images?output=images&as_filetype=tif>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

huayingyang@gmail.com - 22 Oct 2007 16:41 GMT
> Your question is not clear to me.
> Several interpretations that I can come up with:
>
> 1. You wish to construct a java.awt.Image from a TIFF file, or
> from an array of bytes which represent the contents of a valid
> TIFF file.

Thanks a lot for your response.  I need to create a TIFF image from a
binary array known consisting of a valid TIFF file.

> Please run the following small program, if it does not output
> something like tif, tiff, TIF, or TIFF, you probably cannot
> use java.imageio.ImageIO.read.

I ran your program and it worked well with TIFF image file!  It still
doesn't work for my binary raw data array though.  I got this byte
array from a X9 file, which is in ASCII format.  The image data of
this x9 file consists of 8 bytes of Image header and the rest are
Raster data.
Lew - 22 Oct 2007 17:20 GMT
> I ran your program and it worked well with TIFF image file!  It still
> doesn't work for my binary raw data array though.  I got this byte
> array from a X9 file, which is in ASCII format.  The image data of
> this x9 file consists of 8 bytes of Image header and the rest are
> Raster data.

How could a TIFF image be in ASCII format?

Signature

Lew

huayingyang@gmail.com - 22 Oct 2007 19:23 GMT
> How could a TIFF image be in ASCII format?
>
> --
> Lew

X9 file is an ASCII formatted file used by Financial Institutions to
process physical checks.
Check Image data is contained in the x9 file.  To be more precise, the
TIFF image data presented in the X9 file is binary stream.  I am
trying to figure out how to create a TIFF image from the byte array
read from the x9 file.  Hope this will make more sense.
huayingyang@gmail.com - 22 Oct 2007 20:56 GMT
Your code works perfectly when I only used the Raster image data part
to create a TIFF file.
Thanks to everyone's participation!

~helen


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.