<dependency> <groupId>javax.media.jai</groupId> <artifactId>com.springsource.javax.media.jai.core</artifactId> <version>1.1.3</version> </dependency>
import java.awt.image.RenderedImage; import java.awt.image.renderable.ParameterBlock; import java.io.File; import java.io.IOException; import javax.media.jai.JAI; public class Test { public static void tifToPng(String otPath) throws IOException { File tiff = new File(otPath); if (null != tiff && tiff.exists()) { String fileNameIgnore = tiff.getName().replaceAll("[.][^.]+$", ""); TIFFDecodeParam param0 = null; TIFFEncodeParam param = new TIFFEncodeParam(); JPEGEncodeParam param1 = new JPEGEncodeParam(); ImageDecoder dec = ImageCodec.createImageDecoder("tiff", tif, param0); int count = dec.getNumPages(); System.out.println("count:{}"+ count); param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); param.setLittleEndian(false); for (int i = 0; i < count; i++) { RenderedImage page = dec.decodeAsRenderedImage(i); //设置转换后的png图片存储路径 StringBuffer pngPath = new StringBuffer(tiff.getParent()).append(File.separator).append(fileNameIgnore); if (count > 1) { pngPath.append("(").append(++i).append(")"); } pngPath.append(".png"); //png格式 File pngFile = new File(pngPath.toString()); ParameterBlock pb = new ParameterBlock(); pb.addSource(page); pb.add(pngFile.toString()); pb.add("JPEG"); pb.add(param1); JAI.create("filestore", pb); } } } public static void main(String[] args) throws IOException { tifToPng("/Users/xiayangyang/Downloads/tiff/f43a8585-3cf2-4cbb-9150-fb93c10e4f21image1.tiff"); } }
原文:https://www.cnblogs.com/xiayangy/p/14543975.html