依赖
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
代码实现
import org.junit.Test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.UUID;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class TifDemo {
public static File tif2Png(File tifFile) {
File outFiles = new File("test-file/" + UUID.randomUUID().toString());
boolean mkdirs = outFiles.mkdirs();
try (ImageInputStream input = ImageIO.createImageInputStream(tifFile)) {
ImageReader reader = ImageIO.getImageReaders(input).next();
reader.setInput(input);
int frameCount = reader.getNumImages(true);
for (int i = 0; i < frameCount; i++) {
BufferedImage image = reader.read(i, null);
File tempPng = new File(outFiles, i + ".png");
ImageIO.write(image, "PNG", tempPng);
}
reader.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return outFiles;
}
}