Convert the TIFF image format to gif and jpg in Java.

Source: Internet
Author: User

After searching for images from the Internet for a long time, I didn't find any images that use Java to convert the TIFF format. Suddenly, I can see that the TIFF format is converted to the PSD format. If I want to convert it to the PSD format, I can definitely convert it to the jpg and GIF formats, other jar packages were originally required,

I downloaded it from: http://www.java2s.com/online,

Which has: jai_codec-1.1.3.jar
Jai_core.jar
Jai_imageio-1.1.jar
IText-5.0.2.jar
After the download, add it to your Java project.

Code 

Need to reference

 1   Import  Java. AWT. color;  2   Import  Java. AWT. graphics;  3   4   Import  Java. AWT. image; 5   Import  Java. AWT. image. bufferedimage;  6   Import  Java. Io. file;  7   Import  Java. Io. filenotfoundexception;  8   Import  Java. Io. ioexception;  9   10   11   12  Import  Javax. ImageIO. ImageIO;  13   Import  Javax. ImageIO. imagereader;  14   Import  Javax. ImageIO. Stream. fileimageinputstream;  15   Import  Javax. ImageIO. Stream. imageinputstream;  16   Import  Javax. ImageIO. SPI. iioregistry;  17  Import Javax. ImageIO. SPI. imagereaderspi;

Then there is the method.

Put in main

 1 String tifpath = "C:/1. Tiff" ;  2   Int Pages = 0 ;  3 Bufferedimage [] Bi = Null  ;  4 File Extension file = New  File (tifpath );  5 Imagereader external reader = Null  ;  6 Imageinputstream input = Null  ;  7           Try  {  8 Input = New  Fileimageinputstream (writable file );  9 } Catch  (Filenotfoundexception e ){ 10               //  Todo auto-generated Catch Block  11   E. printstacktrace ();  12 } Catch  (Ioexception e ){  13               //  Todo auto-generated Catch Block  14   E. printstacktrace ();  15   } 16 Iioregistry iioreg = Iioregistry. getdefaultinstance ();  17   Iioreg. registerapplicationclasspathspis ();  18 Imagereaderspi IRS = New  Com. Sun. Media. imageioimpl. plugins. Tiff. Unzip imagereaderspi ();  19           Try  {  20 Tiffreader = IRS. createreaderinstance ();  21 } Catch  (Ioexception e ){  22               //  Todo auto-generated Catch Block  23   E. printstacktrace ();  24   }  25   Tiffreader. setinput (input );  26           27           Try  {  28 Pages = tiffreader. getnumimages ( True  );  29 } Catch  (Ioexception e ){  30               //  Todo auto-generated Catch Block  31   E. printstacktrace ();  32   }  33           34           If (Pages> 0){  35 Bi = New  Bufferedimage [Pages];  36               For ( Int I = 0; I <pages; I ++ ){  37   System. Out. println (I );  38                   Try  {  39 Bi [I] =Tiffreader. Read (I );  40 } Catch  (Ioexception e ){  41                       //  Todo auto-generated Catch Block  42   E. printstacktrace ();  43   }  44 Image image = Bi [I]. getscaledinstance (Bi [I]. getwidth (), Bi [I]. getheight (), image. scale_default ); 45 Bufferedimage tag = New  Bufferedimage (Bi [I]. getwidth (), Bi [I]. getheight (), bufferedimage. type_int_bgr );  46 Graphics G = Tag. getgraphics ();  47 G. drawimage (image, 0, 0, Null  );  48   G. Dispose ();  49                   Try  { 50 ImageIO. Write (TAG, "GIF ", New File ("C:/" + I + "11.gif" ));  51 ImageIO. Write (TAG, "jpg ", New File ("C:/" + I + "11.jpg" ));  52 } Catch  (Ioexception e ){  53                       //  Todo auto-generated Catch Block  54  E. printstacktrace ();  55   }  56   }  57 }

It should be noted that the read image stream cannot be used directly.
If the ImageIO. Write method outputs an image, an error is returned,
In the end, I thought that the image could not be directly generated to zoom in and out !!

I do not know whether the above is correct. I hope someone will point out the error !!!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.