Use imageio to convert a .tif image to a .jpg image

B

brightoceanlight

Can anyone suggest how to use java imageio package to convert a .tif
image file to a .jpg image?
 
R

Rhino

Can anyone suggest how to use java imageio package to convert a .tif
image file to a .jpg image?
I've never tried to convert a .tif to a .jpg but I looked (briefly) at
trying to convert a .gif to .jpg via the ImageIO classes a few weeks ago. As
I recall, you had to use a transcoder to convert from one format to another.
A transcoder is a special plugin that reads one format, then writes an
output in the second format, so you'd pass it the .tif as the input and it
would write the .jpg equivalent for you.

I don't know if a .tif to .jpg transcoder is included with ImageIO. There
was no .gif to .jpg transcoder included with ImageIO and that would probably
be the most common conversion that people would want. One of the methods in
the ImageIO classes will tell you which transcoders (if any) are available
to you.

Hmm, I just ran this little bit of code that I had and found that .tif isn't
one of the formats supported by ImageIO!

----
public Images01() {
listReaderFormats();
listWriterFormats();
listTranscoders();
}

public void listReaderFormats() {

String[] readerFormats = ImageIO.getReaderFormatNames();
System.out.println("\nImageIO Reader Formats: ");
for (int ix = 0; ix < readerFormats.length; ix++) {
System.out.println(readerFormats[ix]);
}

}

public void listWriterFormats() {

String[] writerFormats = ImageIO.getWriterFormatNames();
System.out.println("\nImageIO Writer Formats: ");
for (int ix = 0; ix < writerFormats.length; ix++) {
System.out.println(writerFormats[ix]);
}
}

public void listTranscoders() {

/* Initialize the following two variables to the desired input and
output formats. */
String inputFormat = "tif";
String outputFormat = "jpg";

Iterator inputReaders =
ImageIO.getImageReadersByFormatName(inputFormat);
ImageReader reader = (ImageReader) inputReaders.next();

Iterator outputWriters =
ImageIO.getImageWritersByFormatName(outputFormat);
ImageWriter writer = (ImageWriter) outputWriters.next();

Iterator transcoders = ImageIO.getImageTranscoders(reader, writer);
System.out.println("\n" + inputFormat + " to " + outputFormat + "
transcoders:");
while (transcoders.hasNext()) {
System.out.println(" " + transcoders.next());
}

}
----

The output of this code is:

----
ImageIO Reader Formats:

BMP

jpeg

bmp

wbmp

gif

JPG

png

jpg

WBMP

JPEG

ImageIO Writer Formats:

BMP

jpeg

bmp

wbmp

JPG

png

jpg

PNG

WBMP

JPEG

----

I get a NoSuchElementException when I try to execute the listTranscoders()
method; not surprising because ImageIO doesn't read .tif files.

In that case, you probably need _two_ plugins: one to read .tif files and
one to convert .tif to .jpg. Neither of those is shipped with my version of
the ImageIO classes but I'm working with a fairly early version of J2SE 1.5;
maybe later versions have additional plugins. Just try my code against your
version of Java and see if you have a .tif reader and/or a .tif to .jpg
transcoder.

If you don't have the relevant plugin(s) in your copy of Java, you need to
do one of the following things:
1. Find the necessary plugins from a third party provider. They may not be
free...
-OR-
2. Write your own plugin(s). As I recall, the ImageIO architecture is
designed to make it extensible by developers like us. The Java Image I/O API
Guide, which is part of the documentation in the Java API, discusses how to
write your own plugins.

Rhino
 
K

Knute Johnson

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top