Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to create a TIFF image from a binary raw data

Reply
Thread Tools

How to create a TIFF image from a binary raw data

 
 
huayingyang@gmail.com
Guest
Posts: n/a
 
      10-19-2007
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.

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
huayingyang@gmail.com
Guest
Posts: n/a
 
      10-19-2007
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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-19-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-20-2007

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).getAsBufferedIma ge();
}
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.
>



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      10-20-2007
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

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-20-2007

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



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      10-20-2007
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.asp...neral/200710/1

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-20-2007

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


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      10-20-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
BMP to TIFF or TIFF to BMP- any loss? Jenna Topping Digital Photography 5 01-23-2005 01:18 AM
Tool for converting 12-bit TIFF images to 16-bit TIFF-images? Peter Frank Digital Photography 11 12-13-2004 02:41 AM
jpeg to tiff and back to tiff ARosenblat Digital Photography 6 01-09-2004 08:53 PM
Image::Info::TIFF - multi-page tiff images page count ifiaz Perl Misc 1 11-19-2003 05:25 AM



Advertisments