调整 tif文件生成图片 添加后缀 防止同名产生 简化代码

This commit is contained in:
gaoxiongzaq 2023-04-24 15:36:01 +08:00
parent 8649f20841
commit a1abdfb70a
2 changed files with 28 additions and 50 deletions

View File

@ -49,7 +49,7 @@ public class TiffFilePreviewImpl implements FilePreview {
return TIFF_FILE_PREVIEW_PAGE;
} else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
String jpgName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "jpg" ; //生成文件添加类型后缀 防止同名文件
String jpgName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix; //生成文件添加类型后缀 防止同名文件
String strLocalTif = fileDir + fileName;
String outFilePath = fileDir + pdfName;
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
@ -88,10 +88,8 @@ public class TiffFilePreviewImpl implements FilePreview {
}
strLocalTif = response.getContent();
}
// 以JPG模式预览的过程
String strJpgFilePathName = fileDir + jpgName;
// 将tif转换为jpg返回转换后的文件路径文件名的list
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, strJpgFilePathName);
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, jpgName);
// 将返回页面的图片url的list对象
List<String> listImageUrls = new ArrayList<>();
// 循环拼装url的list对象

View File

@ -45,46 +45,26 @@ public class ConvertPicUtil {
return null;
}
strInputFile = strInputFile.replaceAll("\\\\", "/");
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
FileSeekableStream fileSeekStream = null;
try {
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
tiffEncodeParam.setLittleEndian(false);
String jpgname = strInputFile.replace(fileDir.replace("\\","/"), "");
int index = jpgname.lastIndexOf(".");
String strFilePrefix = jpgname.substring(0, index);
fileSeekStream = new FileSeekableStream(strInputFile);
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
int intTifCount = imageDecoder.getNumPages();
logger.info("该tif文件共有【" + intTifCount + "】页");
String strJpgPath;
String strJpgUrl;
if (intTifCount == 1) {
// 如果是单页tif文件则转换的目标文件夹就在指定的位置
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("/"));
} else {
// 如果是多页tif文件则在目标文件夹下按照文件名再创建子目录将转换后的文件放入此新建的子目录中
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("."));
}
String strJpgPath = fileDir+strOutputFile;
// 处理目标文件夹如果不存在则自动创建
File fileJpgPath = new File(strJpgPath);
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
logger.error("{} 创建失败", strJpgPath);
}
// 循环处理每页tif文件转换为jpg
for (int i = 0; i < intTifCount; i++) {
String strJpg;
if (intTifCount == 1) {
strJpg = strJpgPath + "/" + strFilePrefix + ".jpg";
strJpgUrl = strFilePrefix + ".jpg";
} else {
strJpg = strJpgPath + "/" + i + ".jpg";
strJpgUrl = strFilePrefix + "/" + i + ".jpg";
}
String strJpg= strJpgPath + "/" + i + ".jpg";
String strJpgUrl = strOutputFile + "/" + i + ".jpg";
File fileJpg = new File(strJpg);
// 如果文件不存在则生成
if (!fileJpg.exists()) {