How to create a TIFF image from a binary raw data

Discussion in 'Java' started by huayingyang@gmail.com, Oct 19, 2007.

  1. Guest

    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.
    , Oct 19, 2007
    #1
    1. Advertising

  2. 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
    Andrew Thompson, Oct 19, 2007
    #2
    1. Advertising

  3. Guest

    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
    , Oct 19, 2007
    #3
  4. Lew Guest

    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
    Lew, Oct 19, 2007
    #4
  5. Jeff Higgins Guest

    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.
    >
    Jeff Higgins, Oct 20, 2007
    #5
  6. 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.)

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

    Message posted via http://www.javakb.com
    Andrew Thompson, Oct 20, 2007
    #6
  7. Jeff Higgins Guest

    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 the
    JAI-Demo project - JAIImageReader.java.
    The source can be viewed here:
    <http://preview.tinyurl.com/yubqol>
    Thanks,
    JH
    Jeff Higgins, Oct 20, 2007
    #7
  8. 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
    Andrew Thompson, Oct 20, 2007
    #8
  9. Jeff Higgins Guest

    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
    Jeff Higgins, Oct 20, 2007
    #9
  10. 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 Thompson
    http://www.athompson.info/andrew/

    Message posted via http://www.javakb.com
    Andrew Thompson, Oct 20, 2007
    #10
  11. Guest

    On Oct 19, 8:40 pm, "Jeff Higgins" <> 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 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.
    , Oct 22, 2007
    #11
  12. Lew Guest

    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
    Lew, Oct 22, 2007
    #12
  13. Guest

    On Oct 22, 12:20 pm, Lew <> wrote:

    > 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.
    , Oct 22, 2007
    #13
  14. Guest

    Your code works perfectly when I only used the Raster image data part
    to create a TIFF file.
    Thanks to everyone's participation!

    ~helen
    , Oct 22, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Douglas at AI

    Need Raw Tile Data of Tiled Tiff

    Douglas at AI, Sep 30, 2005, in forum: Java
    Replies:
    1
    Views:
    612
    Andrey Kuznetsov
    Sep 30, 2005
  2. Achim Domma
    Replies:
    3
    Views:
    606
    Sjoerd Mullender
    Feb 17, 2004
  3. CD
    Replies:
    3
    Views:
    254
  4. ifiaz
    Replies:
    1
    Views:
    890
    Martien Verbruggen
    Nov 19, 2003
  5. Jack
    Replies:
    2
    Views:
    178
    Dave Weaver
    Jan 7, 2008
Loading...

Share This Page