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

Discussion in 'Java' started by brightoceanlight@hotmail.com, Nov 17, 2005.

  1. Guest

    Can anyone suggest how to use java imageio package to convert a .tif
    image file to a .jpg image?
     
    , Nov 17, 2005
    #1
    1. Advertising

  2. Rhino Guest

    <> wrote in message
    news:...
    > 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
     
    Rhino, Nov 17, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On 17 Nov 2005 08:53:57 -0800, wrote,
    quoted or indirectly quoted someone who said :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 17, 2005
    #3
  4. Roedy Green wrote:
    > On 17 Nov 2005 08:53:57 -0800, wrote,
    > quoted or indirectly quoted someone who said :
    >
    >
    >>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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 18, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F C
    Replies:
    2
    Views:
    5,203
  2. Davidski
    Replies:
    0
    Views:
    3,937
    Davidski
    Nov 5, 2004
  3. Rune
    Replies:
    4
    Views:
    1,886
  4. Trung Chinh Nguyen
    Replies:
    2
    Views:
    1,734
    Trung Chinh Nguyen
    Apr 26, 2006
  5. Replies:
    2
    Views:
    1,633
    Andrew Thompson
    May 19, 2007
Loading...

Share This Page