How to create a TIFF image from a binary raw data

  • Follow


Hello all, I am having difficulty creating a TIFF Image file from abinary raw data.I tried to used ImageIO or ImageJ but with no luck.  My implementationis 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 informme.  Any thoughtful ideas are greatly appreciated.
0
Reply huayingyang (1) 10/19/2007 3:44:12 PM

huayingyang@gmail.com wrote:
>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>

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

Message posted via http://www.javakb.com

0
Reply Andrew 10/19/2007 4:14:22 PM


Hello,Thank you for your response.  I don't know how you compiled the loadmethod.  I used Eclipse and there is no compilation error.  Theproblem is that this method doesn't work as expected, i.e. image isalways equal to null.  I am looking for a way to create a TIFF imagefrom raw binary data...Thanks,Helen
0
Reply huayingyang 10/19/2007 6:07:01 PM

huayingyang@gmail.com wrote:> 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.*;> import ij.ImagePlus;> import ij.io.Opener;> import java.io.*;> import java.awt.Image;> > protected Image load(byte[] data) {Notice: no class declaration.-- Lew
0
Reply Lew 10/19/2007 7:45:15 PM

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.*;
> 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.
> 


0
Reply Jeff 10/20/2007 12:40:40 AM

Jeff Higgins wrote:...>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.BMPbmpjpgJPGwbmpjpegpngPNGJPEGWBMPGIFgif(Though I successfully loaded and displayed two different TIF images, using a slight variant of your code.)-- Andrew Thompsonhttp://www.athompson.info/andrew/Message posted via http://www.javakb.com
0
Reply Andrew 10/20/2007 5:40:49 AM

Andrew Thompson wrote:> Jeff Higgins wrote:> ..>>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.)>Would you mind telling your variations?I swiped the code (with slight variations) from theJAI-Demo project - JAIImageReader.java.The source can be viewed here:<http://preview.tinyurl.com/yubqol>Thanks,JH
0
Reply Jeff 10/20/2007 9:43:03 AM

Jeff Higgins wrote:
...
>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'.  :-)

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1

0
Reply Andrew 10/20/2007 11:25:16 AM

Andrew Thompson wrote:> Jeff Higgins wrote:> ..>>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 
0
Reply Jeff 10/20/2007 12:13:55 PM

Andrew Thompson wrote:...>...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>-- Andrew Thompsonhttp://www.athompson.info/andrew/Message posted via http://www.javakb.com
0
Reply Andrew 10/20/2007 12:25:16 PM

On Oct 19, 8:40 pm, "Jeff Higgins" <oohigg...@yahoo.com> wrote:> 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 abinary 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 stilldoesn't work for my binary raw data array though.  I got this bytearray from a X9 file, which is in ASCII format.  The image data ofthis x9 file consists of 8 bytes of Image header and the rest areRaster data.
0
Reply huayingyang 10/22/2007 3:41:06 PM

huayingyang@gmail.com wrote:
> 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?

-- 
Lew
0
Reply Lew 10/22/2007 4:20:51 PM

On Oct 22, 12:20 pm, Lew <l...@lewscanon.com> wrote:> How could a TIFF image be in ASCII format?>> --> LewX9 file is an ASCII formatted file used by Financial Institutions toprocess physical checks.Check Image data is contained in the x9 file.  To be more precise, theTIFF image data presented in the X9 file is binary stream.  I amtrying to figure out how to create a TIFF image from the byte arrayread from the x9 file.  Hope this will make more sense.
0
Reply huayingyang 10/22/2007 6:23:36 PM

Your code works perfectly when I only used the Raster image data partto create a TIFF file.Thanks to everyone's participation!~helen
0
Reply huayingyang 10/22/2007 7:56:10 PM

13 Replies
443 Views

(page loaded in 0.18 seconds)

Similiar Articles:


















7/24/2012 12:55:52 AM


Reply: