How to convert TIFF to JPEG/PNG in java

前端 未结 5 1448
时光说笑
时光说笑 2020-12-03 06:08

recently i\'m facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safar

5条回答
  •  日久生厌
    2020-12-03 06:30

    Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.

    SeekableStream s = new FileSeekableStream(inFile);
    TIFFDecodeParam param = null;
    ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
    RenderedImage op = dec.decodeAsRenderedImage(0);
    
    FileOutputStream fos = new FileOutputStream(otPath);
    JPEGEncodeParam jpgparam = new JPEGEncodeParam();
    jpgparam.setQuality(67);
    ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
    en.encode(op);
    fos.flush();
    fos.close();
    

    otPath is the path that you would like to store your JPEG image. For example: "C:/image/abc.JPG";
    inFile is the input file which is the TIFF file

    At least this method is workable to me. If there is any other better method, kindly share along with us.

提交回复
热议问题