JAI jar Package Conversion TIF file error resolution, multi-page TIF to multiple JPEG methods

Source: Internet
Author: User


1. Defining File Enumeration Classes

/** *  file Type Enumeration classes  * */public enum FileType {/**       * jepg.      */      jpeg ("FFD8FF"),              /**       * png.      */     png ("89504E47"),             /**       * gif.      */      gif ("47494638"),               /**        * TIFF.       */       tiff ("49492a00"),         /**        * tif.  &Nbsp;    */      tif ("49492a00"),            /**       * Windows  Bitmap.       */      bmp ("424D"),               /**        * Rich Text Format.       */      rtf ("7b5c727466"),               /**       * XML.        */      xml ("3c3f786d6c"),               /**       * HTML.        */ &nbSp;    html ("68746d6c3e"),           /**        * Adobe Acrobat.        */      pdf ("255044462d312e"),                  /**       * zip  archive.       */      zip ("504B0304"),               /**        * RAR Archive.       */       rar ("52617221"),               /**       * Wave.        */   &nBsp;  wav ("57415645"),               /**       * avi.       */       avi ("41564920");          private string value =  "";        private  FileType (String value)  {    this.value = value;     }        public string getvalue ()  {     return value;    }        public  void setvalue (String value)  {    this.value = value;     }}

2. Define the file type of the tool class to determine the file type

/**   *  file type Judging class    */  public final class  filetypejudge {              /**        * Constructor       */       private filetypejudge ()  {}               /**       *  Convert a file header to a 16 string        *        *  @param   Native byte       *  @return  16 binary string         */      private static string bytestohexstring (byte[] &NBSP;SRC) {                      &nbsP Stringbuilder stringbuilder = new stringbuilder ();               if  (src == null | |  src.length <= 0)  {                   return null;               }               for  (int i = 0; i < src.length; i++)  {                   int v  = src[i] & 0xFF;                   string hv = integer.tohexstring (v);                   if  (Hv.length ()  < 2)  {                        Stringbuilder.append (0);                   }                   stringbuilder.append (HV);               }               Return stringbuilder.tostring ();          }              /**        *  Get file Header        *        *   @param  filepath  file path        *  @return   file Header         *  @throws  IOException       */       private static string getfilecontent (String filepath)  throws  ioexception {                       byte[] b = new byte[28];                        inputstream inputstream = null;                       try {                inputStream = new  FileInputStream (FilePath);   &nBsp;            inputstream.read (b, 0,  ;           } catch  (IOException e)  {                E.printstacktrace ();                throw e;           } finally {                if  (inputstream  != null)  {                    try {                        inputstream.close ();                    } catch  (ioexception e)  {                         e.printstacktrace ();                        throw e;                    }                }            }           return  Bytestohexstring (b);        }       /**        *  Judging file Types        *         *  @param  filePath  file Paths        *  @return   File Type        */      public static  filetype gettype (String filepath)  throws IOException {                        String filehead = getfilecontent (FilePath);                       if  (filehead ==  null | |  filehead.length ()  == 0)  {                return null;            }                       filehead = filehead.touppercase ();                       filetype[] filetypes  = filetype.values ();                       for  (Filetype type : filetypes)  {               if  ( Filehead.startswith (Type.getvalue ()))  {                    return type;                }            }                       return null;       }   } 

3. When using the Jai jar package call, first determine the type, and then pass the specific parameters according to the specific type

/***     *  convert a single TIF to multiple JPEG files      *  @param   srcfile     *  @param  destFile     *  @return      */    private static List<String>  Convertmtiftomjpeg (String srcfile, string destfile)  {         List<String> jpegFiles = new ArrayList<String> ()  ;          try{              fileseekablestream ss = new fileseekablestream (SrcFile);              tiffdecodeparam param0 =  null;             TIFFEncodeParam  Param = new tiffencodepAram ();             jpegencodeparam  Param1 = new jpegencodeparam ();              filetype filetype = filetypejudge.gettype (srcfile);              ImageDecoder dec =  Imagecodec.createimagedecoder (Filetype.tostring (). toLowerCase (), &NBSP;SS,&NBSP;PARAM0);              int count = dec.getnumpages ();              param.setcompression ( TIFFENCODEPARAM.COMPRESSION_GROUP4);              param.setlittleendian (False);               for  (Int i = 0; i < count; i++)  {                  renderedimage page = dec.decodeasrenderedimage (i);                  file f = new  file (Destfile + i + dot + watermarkdownloadfiletype.jpeg.name (). toLowerCase ());                  parameterblock pb = new parameterblock ();                  pb.addsource (page);                  pb.add (F.toString ());                  pb.add ( WaterMarkDownloadFileType.JPEG.name ()); &NBSP;&NBSp;               pb.add (param1);                   Renderedop r = jai.create ("Filestore", Pb);                  r.dispose ();                  jpegfiles.add (destfile + i +  Dot + watermarkdownloadfiletype.jpeg.name (). toLowerCase ())  ;              }         }  catch  (ioexception e)  {              e.printstacktrace ();         }          return jpegfiles;    } 


JAI jar Package Conversion TIF file error resolution, multi-page TIF to multiple JPEG methods

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.