生活随笔
收集整理的這篇文章主要介紹了
将单页/多页 tif转换 jpg 格式方法和所需jar包
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
所需jar包:
鏈接:https://pan.baidu.com/s/1GBBWEiBCHy5y3TbKhkTOfg
提取碼:mfvs
將 tif 轉換 jpg 格式(單頁tif)
public static String
tiffTuanJPG(String filePath
) {String format
= filePath
.substring(filePath
.lastIndexOf(".") + 1);String turnJpgFile
= filePath
.replace("tif", "jpg");if (format
.equals("tif")) {File fileTif
= new File(turnJpgFile
);if (fileTif
.exists()) {System
.out
.println("該tif文件已經轉換為 JPG 文件:" + turnJpgFile
);return turnJpgFile
;}RenderedOp rd
= JAI
.create("fileload", filePath
);OutputStream ops
= null
;try {ops
= new FileOutputStream(turnJpgFile
);JPEGEncodeParam param
= new JPEGEncodeParam();ImageEncoder image
= ImageCodec
.createImageEncoder("JPEG", ops
, param
);image
.encode(rd
);ops
.close();System
.out
.println("tif轉換jpg成功:" + filePath
);} catch (FileNotFoundException e
) {e
.printStackTrace();} catch (IOException e
) {e
.printStackTrace();}}return turnJpgFile
;
}
將 tif 轉換 jpg 格式(多頁tif)
參考:https://www.cnblogs.com/101key/p/3324601.html
public void tif2Jpg(String fileAbsolutePath
) {if (fileAbsolutePath
== null
|| "".equals(fileAbsolutePath
.trim())){return ;}if (!new File(fileAbsolutePath
).exists()){logger
.info("系統找不到指定文件【"+fileAbsolutePath
+"】");return ;}FileSeekableStream fileSeekStream
= null
;try {fileSeekStream
= new FileSeekableStream(fileAbsolutePath
);TIFFEncodeParam tiffEncodeParam
= new TIFFEncodeParam();JPEGEncodeParam jpegEncodeParam
= new JPEGEncodeParam();ImageDecoder dec
= ImageCodec
.createImageDecoder("tiff", fileSeekStream
, null
);int count
= dec
.getNumPages();tiffEncodeParam
.setCompression(TIFFEncodeParam
.COMPRESSION_GROUP4
);tiffEncodeParam
.setLittleEndian(false);logger
.info("該tif文件共有【" + count
+ "】頁");String filePathPrefix
= fileAbsolutePath
.substring(0, fileAbsolutePath
.lastIndexOf("."));for (int i
= 0; i
< count
; i
++) {RenderedImage renderedImage
= dec
.decodeAsRenderedImage(i
);File imgFile
= new File(filePathPrefix
+ "_" + i
+ ".jpg");logger
.info("每頁分別保存至: " + imgFile
.getCanonicalPath());ParameterBlock pb
= new ParameterBlock();pb
.addSource(renderedImage
);pb
.add(imgFile
.toString());pb
.add("JPEG");pb
.add(jpegEncodeParam
);RenderedOp renderedOp
= JAI
.create("filestore",pb
);renderedOp
.dispose();}} catch (IOException e
) {e
.printStackTrace();} finally {if (fileSeekStream
!= null
){try {fileSeekStream
.close();} catch (IOException e
) {}fileSeekStream
= null
;}}}
@Testpublic void test() {String fileName
= "D:\\home\\oldZJYX\\7a400398f2db482baec96e4f4d9e68a9\\揚州中行8269-201904.tif";String s
= tiffTuanJPG(fileName
);System
.out
.println(s
);}
總結
以上是生活随笔為你收集整理的将单页/多页 tif转换 jpg 格式方法和所需jar包的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。