Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
brightoceanlight@hotmail.com - 17 Nov 2005 16:53 GMT
Can anyone suggest how to use java imageio package to convert a .tif
image file to a .jpg image?
Rhino - 17 Nov 2005 18:14 GMT
> 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
Roedy Green - 17 Nov 2005 21:55 GMT
>Can anyone suggest how to use java imageio package to convert a .tif
>image file to a .jpg image?

IIRC ImageIO supports only jpg gif and png. If you want other formats
you need to use JAI, which is considerably more complicated. See
http://mindprod.com/jgloss/jai.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Knute Johnson - 18 Nov 2005 00:32 GMT
>>Can anyone suggest how to use java imageio package to convert a .tif
>>image file to a .jpg image?
>
> IIRC ImageIO supports only jpg gif and png. If you want other formats
> you need to use JAI, which is considerably more complicated. See
> http://mindprod.com/jgloss/jai.html

There is another package, the JAI Image I/O API 1.0_01.  Go to:

http://java.sun.com/products/java-media/jai/index.jsp

and on the right get the JAI Image I/O API 1.0_01.  This has some
plugins that will work with the ImageIO classes in Java.  TIFF is among
the supported formats.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.