How to create a TIFF image from a binary raw data

H

huayingyang

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.
 
A

Andrew Thompson

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>
 
H

huayingyang

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
 
L

Lew

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.
 
J

Jeff Higgins

huayingyang said:
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.
 
A

Andrew Thompson

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.)
 
J

Jeff Higgins

Andrew said:
Jeff Higgins wrote:
..

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
 
A

Andrew Thompson

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.
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
 
J

Jeff Higgins

Andrew said:
Jeff Higgins wrote:
..

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
 
H

huayingyang

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.
 
L

Lew

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?
 
H

huayingyang

How could a TIFF image be in ASCII format?

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.
 
H

huayingyang

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

~helen
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top