Browse Source

任务详情导出优化:压缩图片

master
wangguangyuan 5 months ago
parent
commit
efb1963ede
2 changed files with 64 additions and 17 deletions
  1. +14
    -0
      inspect-main/inspect-main-task/pom.xml
  2. +50
    -17
      inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java

+ 14
- 0
inspect-main/inspect-main-task/pom.xml View File

@ -49,5 +49,19 @@
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 图片压缩库 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.19</version>
</dependency>
<!-- POI 流式处理 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
</project>

+ 50
- 17
inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java View File

@ -61,6 +61,7 @@ import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -2453,26 +2454,39 @@ public class PatrolTaskController extends BaseController {
}
}
logger.info("图片总数: {}", uniqueImages.size());
// 并行下载图片
logger.info("数据处理耗时: {} ms", (System.currentTimeMillis() - start));
long startDownImg = System.currentTimeMillis();
// // 并行下载图片
Map<String, byte[]> streamHashMap = new ConcurrentHashMap<>();
// uniqueImages.parallelStream().forEach(imagePath -> {
// try {
// sftpClient.downLoad(imagePath, (inputStream) -> {
// byte[] shm = streamHashMap.get(imagePath);
// byte[] bytes = getStringByInputStream(inputStream);
// if (shm == null) {
// streamHashMap.put(imagePath, bytes);
// }
//
// });
// } catch (Exception e) {
// logger.warn("图片下载失败: {}", imagePath, e);
// }
// });
//
uniqueImages.parallelStream().forEach(imagePath -> {
try {
sftpClient.downLoad(imagePath, (inputStream) -> {
byte[] shm = streamHashMap.get(imagePath);
byte[] bytes = getStringByInputStream(inputStream);
if (shm == null) {
streamHashMap.put(imagePath, bytes);
}
});
} catch (Exception e) {
logger.warn("图片下载失败: {}", imagePath, e);
byte[] shm = streamHashMap.get(imagePath);
byte[] bytes = downloadAndCompressImage(imagePath);
if (shm == null) {
streamHashMap.put(imagePath, bytes);
}
});
logger.info("图片下载成功");
logger.info("图片下载耗时: {} ms", (System.currentTimeMillis() - startDownImg));
// exportExcel(response, newList);
long startExport = System.currentTimeMillis();
exportExcelStream(response, newList, streamHashMap);
logger.info("任务详情导出耗时: {} ms", (System.currentTimeMillis() - start));
logger.info("任务详情导出耗时: {} ms", (System.currentTimeMillis() - startExport));
logger.info("任务详情导出总流程耗时: {} ms", (System.currentTimeMillis() - start));
return ResponseEntity.ok("数据导出成功!");
}
@ -2510,6 +2524,25 @@ public class PatrolTaskController extends BaseController {
}
}
private byte[] downloadAndCompressImage(String imagePath) {
try {
// 直接下载并压缩图片
ByteArrayOutputStream output = new ByteArrayOutputStream();
sftpClient.downLoad(imagePath, inputStream -> {
// 压缩图片
Thumbnails.of(inputStream)
.size(300, 300) // 限制最大尺寸
.outputQuality(0.5) // 50%质量
.outputFormat("JPEG")
.toOutputStream(output);
});
return output.toByteArray();
} catch (Exception e) {
logger.warn("图片下载压缩失败: {}", imagePath, e);
return null;
}
}
private void createHeaderRow(Sheet sheet) {
String[] headers = {
"编号", "设备", "相机名", "系统", "点位名称", "状态",
@ -2556,16 +2589,16 @@ public class PatrolTaskController extends BaseController {
try {
// 获取第一张初筛图片如果有
String firstImage = getFirstImage(item.getImg());
if (firstImage != null && imageCache.containsKey(firstImage)) {
byte[] imageData = imageCache.get(firstImage);
if (firstImage != null) {
byte[] imageData = imageCache.getOrDefault(firstImage, new byte[0]);
// 插入筛选图片第8列
insertImage(workbook, drawing, row, 8, imageData);
}
// 获取第一张分析图片如果有
String firstImgAnalyse = getFirstImage(item.getImgAnalyse());
if (firstImgAnalyse != null && imageCache.containsKey(firstImgAnalyse)) {
byte[] imageData = imageCache.get(firstImgAnalyse);
if (firstImgAnalyse != null) {
byte[] imageData = imageCache.getOrDefault(firstImgAnalyse, new byte[0]);
// 插入分析图片第10列
insertImage(workbook, drawing, row, 10, imageData);
}


Loading…
Cancel
Save