1.在build.gradle中添加依赖:
implementation group: 'javax.media', name: 'jai_codec', version: '1.1.3'
implementation group: 'com.sun', name: 'jai_core', version: '1.1.3'
implementation group: 'javax.media', name: 'jai_imageio', version: '1.1'
implementation 'xx.com.sun:jimi:1.0'
最后一个依赖jimi:1.0很难找,如果没有文章上方有jar包可以下载手动导入
手动导入的方式为:
1.和src平级创建lib目录,将所需jar包拷贝到此目录
2.在build.gradle中通过下面语句进行导入
implementation fileTree(dir: 'lib', include: ['*.jar'])
2.创建工具类,进行格式转换
import com.sun.media.jai.codec.*;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Component
public class TifConverterRefreshFilter {
/**
* tif to jpg.
*/
public List<String> tifToJpg(String oldFilePath, String fileName, String newFilePath) throws Exception {
if (oldFilePath == null || fileName.isEmpty()) {
return null;
}
List<String> jpgList = new ArrayList<>();
SeekableStream ss = new FileSeekableStream(oldFilePath);
TIFFDecodeParam param0 = null;
TIFFEncodeParam param = new TIFFEncodeParam();
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, param0);
int count = dec.getNumPages();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(false); // Intel
for (int i = 0; i < count; i++) {
File file = new File(oldFilePath);
ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next();
FileImageInputStream inputStream = new FileImageInputStream(file);
reader.setInput(inputStream);
reader.read(i);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
String pictureUrl = fileName + "_" + i;
FileImageOutputStream fs = new FileImageOutputStream(new File(newFilePath + pictureUrl + ".jpg"));
writer.setOutput(fs);
writer.write(reader.read(i));
fs.close();
reader.dispose();
writer.dispose();
jpgList.add(pictureUrl);
}
ss.close();
return jpgList;
}
}
所需jar包:无法下载,积分不够的私聊我,我单独发给你们