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.i
pener;
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.