From b66e6d1b0ba21642a380988f1485604e04592ae3 Mon Sep 17 00:00:00 2001 From: linrf Date: Mon, 4 Sep 2023 14:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=9B=BE=E7=89=87=E6=A0=B9=E6=8D=AE=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=BF=9B=E8=A1=8C=E5=8E=8B=E7=BC=A9=E5=92=8C?= =?UTF-8?q?=E5=89=AA=E5=88=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/service/IFileUploadService.java | 2 + .../service/impl/FileUploadServiceImpl.java | 58 +++++++++++++++++-- .../ocrfileinfo/AddOcrFileInfoDTO.java | 6 ++ .../AddOcrFileInfoTestDTO.java | 6 ++ .../entity/segmentation/OcrFileInfo.java | 8 +++ .../entity/segmentation/OcrFileInfoTest.java | 9 +++ .../schcollectorversionfile/UploadFileVO.java | 8 +++ .../server/controller/FileController.java | 29 +++++++++- .../server/service/impl/CommonService.java | 5 ++ .../src/main/resources/bootstrap.yml | 1 + .../resources/mapper/OcrFileInfoMapper.xml | 4 +- .../mapper/OcrFileInfoTestMapper.xml | 4 +- .../docus/server/ImageProcessingExample.java | 25 ++++---- 13 files changed, 142 insertions(+), 23 deletions(-) diff --git a/docus-api-common/src/main/java/com/docus/server/common/service/IFileUploadService.java b/docus-api-common/src/main/java/com/docus/server/common/service/IFileUploadService.java index 6876306..8a96e7d 100644 --- a/docus-api-common/src/main/java/com/docus/server/common/service/IFileUploadService.java +++ b/docus-api-common/src/main/java/com/docus/server/common/service/IFileUploadService.java @@ -14,4 +14,6 @@ public interface IFileUploadService { void downloadFile(String filePath, HttpServletResponse response); void getImage(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception; + + List uploadFile(MultipartFile[] files, String segmentation, Double height, Double widthStart, Double widthEnd) throws Exception; } diff --git a/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java b/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java index 874f077..ce32330 100644 --- a/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java +++ b/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java @@ -4,6 +4,8 @@ import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.docus.core.util.Func; +import com.docus.core.util.ParamsUtils; +import com.docus.core.util.StopWatch; import com.docus.infrastructure.web.exception.ApiException; import com.docus.infrastructure.web.exception.ExceptionCode; import com.docus.server.common.service.IFileUploadService; @@ -43,6 +45,9 @@ public class FileUploadServiceImpl implements IFileUploadService { @Value("${file.uploadFolder:D://docus/source/}") private String saveFilePath; + @Value("${file.uploadCompressFolder:#{null}}") + private String saveCompressFilePath; + @Value("${file.uploadCutFolder:#{null}}") private String saveCutFilePath; @@ -63,13 +68,36 @@ public class FileUploadServiceImpl implements IFileUploadService { valid(multipartFile); - uploadFileVOList.add(actionUploadFile(multipartFile, pathKey)); + uploadFileVOList.add(actionUploadFile(multipartFile, pathKey, null, null, null)); + } return uploadFileVOList; } - private UploadFileVO actionUploadFile(MultipartFile multipartFile, String pathKey) throws Exception { + + @Override + public List uploadFile(MultipartFile[] multipartFiles, String pathKey, Double height, Double widthStart, Double widthEnd) throws Exception { + if (Func.isBlank(pathKey)) { + pathKey = UPLOAD_FOLDER; + } + + List uploadFileVOList = new ArrayList<>(); + + for (MultipartFile multipartFile : multipartFiles) { + + valid(multipartFile); + + StopWatch watch = new StopWatch(); + + uploadFileVOList.add(actionUploadFile(multipartFile, pathKey, height, widthStart, widthEnd)); + + System.out.println(multipartFile.getOriginalFilename() + "====图片压缩和剪切总耗时=======>" + watch.elapsedTime() + "(ms)" + "<==========="); + } + return uploadFileVOList; + } + + private UploadFileVO actionUploadFile(MultipartFile multipartFile, String pathKey, Double compressWidth, Double widthStart, Double heightStart) throws Exception { String todayFormat = ymdDtf.format(LocalDateTime.now()); @@ -91,6 +119,23 @@ public class FileUploadServiceImpl implements IFileUploadService { uploadFileVO.setFileType(multipartFile.getContentType()); uploadFileVO.setFilePath(fileName); uploadFileVO.setSaveFilePath(saveFilePath); + uploadFileVO.setParams(ParamsUtils.addParam("compressWidth", compressWidth).addParam("widthStart", widthStart).addParam("heightStart", heightStart).param()); + + if (Func.isNotBlank(saveCompressFilePath)) { + File compressFileDir = new File(saveCompressFilePath + path); + if (!compressFileDir.exists()) { + compressFileDir.mkdirs(); + } + File compressDest = new File(saveCompressFilePath + fileName); + + BufferedImage image = ImageIO.read(multipartFile.getInputStream()); + int width = image.getWidth(); + + //图片压缩 + ImgUtil.scale(image, FileUtil.file(compressDest), (float) (compressWidth / width)); + + uploadFileVO.setSaveCompressFilePath(saveCompressFilePath); + } if (Func.isNotBlank(saveCutFilePath)) { File cutFileDir = new File(saveCutFilePath + path); @@ -99,13 +144,14 @@ public class FileUploadServiceImpl implements IFileUploadService { } File cutDest = new File(saveCutFilePath + fileName); - BufferedImage image = ImageIO.read(multipartFile.getInputStream()); + BufferedImage image = ImageIO.read(FileUtil.file(saveCompressFilePath + fileName)); int height = image.getHeight(); int width = image.getWidth(); int minX = image.getMinX(); int minY = image.getMinY(); - ImgUtil.cut(image, FileUtil.file(cutDest), new Rectangle(50, 50, width, (height / 2) / 2)); + //剪切 + ImgUtil.cut(image, FileUtil.file(cutDest), new Rectangle(0, 0, widthStart.intValue(), heightStart.intValue())); uploadFileVO.setWidth(width); uploadFileVO.setHeight(height); @@ -199,11 +245,11 @@ public class FileUploadServiceImpl implements IFileUploadService { } } + /** * 根据图片的存储类型读取图片 * - * @param fileStorageType 文件存放类型 3 共享文件夹 1本地 2Ftp - * @param fileSrc 文件地址 + * @param fileSrc 文件地址 * @return 返回图片对象 */ private BufferedImage readImageByStorageType(String fileSrc) throws IOException { diff --git a/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfo/AddOcrFileInfoDTO.java b/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfo/AddOcrFileInfoDTO.java index b2dd32f..662de63 100644 --- a/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfo/AddOcrFileInfoDTO.java +++ b/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfo/AddOcrFileInfoDTO.java @@ -48,6 +48,9 @@ public class AddOcrFileInfoDTO implements Serializable { @ApiModelProperty(value = "图片地址") private String picUrl; + @ApiModelProperty(value = "图片分割后地址") + private String picCompressUrl; + @ApiModelProperty(value = "图片分割后地址") private String picCutUrl; @@ -104,6 +107,9 @@ public class AddOcrFileInfoDTO implements Serializable { @ApiModelProperty(value = "纵坐标") private int y; + @ApiModelProperty(value = "额外参数") + private String jsonStr; + @ApiModelProperty(value = "原图片路径保存前缀") private String saveFilePath; diff --git a/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfotest/AddOcrFileInfoTestDTO.java b/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfotest/AddOcrFileInfoTestDTO.java index 03e1ae3..9ed436a 100644 --- a/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfotest/AddOcrFileInfoTestDTO.java +++ b/docus-client-interface/src/main/java/com/docus/server/dto/segmentation/ocrfileinfotest/AddOcrFileInfoTestDTO.java @@ -48,6 +48,9 @@ public class AddOcrFileInfoTestDTO implements Serializable { @ApiModelProperty(value = "图片地址") private String picUrl; + @ApiModelProperty(value = "图片分割后地址") + private String picCompressUrl; + @ApiModelProperty(value = "图片分割后地址") private String picCutUrl; @@ -110,4 +113,7 @@ public class AddOcrFileInfoTestDTO implements Serializable { @ApiModelProperty(value = "剪切图片路径保存前缀") private String saveCutFilePath; + @ApiModelProperty(value = "额外参数") + private String jsonStr; + } diff --git a/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfo.java b/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfo.java index 7cfe9c0..b05077f 100644 --- a/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfo.java +++ b/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfo.java @@ -63,6 +63,10 @@ public class OcrFileInfo implements Serializable { @TableField("pic_url") private String picUrl; + @ApiModelProperty(value = "图片压缩后地址") + @TableField("pic_compress_url") + private String picCompressUrl; + @ApiModelProperty(value = "图片分割后地址") @TableField("pic_cut_url") private String picCutUrl; @@ -123,6 +127,10 @@ public class OcrFileInfo implements Serializable { @TableField("y") private int y; + @ApiModelProperty(value = "额外参数") + @TableField("json_str") + private String jsonStr; + @ApiModelProperty(value = "创建时间") @TableField("create_time") private Date createTime; diff --git a/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfoTest.java b/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfoTest.java index f5f081d..84b980e 100644 --- a/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfoTest.java +++ b/docus-client-interface/src/main/java/com/docus/server/entity/segmentation/OcrFileInfoTest.java @@ -64,6 +64,10 @@ public class OcrFileInfoTest implements Serializable { @TableField("pic_url") private String picUrl; + @ApiModelProperty(value = "图片压缩后地址") + @TableField("pic_compress_url") + private String picCompressUrl; + @ApiModelProperty(value = "图片分割后地址") @TableField("pic_cut_url") private String picCutUrl; @@ -124,6 +128,11 @@ public class OcrFileInfoTest implements Serializable { @TableField("y") private int y; + @ApiModelProperty(value = "额外参数") + @TableField("json_str") + private String jsonStr; + + @ApiModelProperty(value = "创建时间") @TableField("create_time") private Date createTime; diff --git a/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java b/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java index 6b8e706..1ce90dd 100644 --- a/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java +++ b/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java @@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.util.Map; + @Data @ApiModel(value = "UploadFileVO对象", description = "UploadFileVO") public class UploadFileVO { @@ -38,6 +40,12 @@ public class UploadFileVO { @ApiModelProperty(value = "原图片路径保存前缀") private String saveFilePath; + @ApiModelProperty(value = "压缩图片路径保存前缀") + private String saveCompressFilePath; + @ApiModelProperty(value = "剪切图片路径保存前缀") private String saveCutFilePath; + + @ApiModelProperty(value = "扩展参数") + private Map params; } diff --git a/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java b/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java index 8607f9f..af7f8d0 100644 --- a/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java +++ b/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java @@ -3,10 +3,14 @@ package com.docus.server.controller; import com.docus.server.common.event.FlowEvent; import com.docus.server.common.service.IFileUploadService; import com.docus.server.dto.segmentation.UploadBatchFileRequest; +import com.docus.server.service.IOcrCutConfigService; +import com.docus.server.service.IOcrCutConfigTestService; import com.docus.server.service.IPublishEventService; import com.docus.server.service.impl.CommonService; import com.docus.server.service.impl.PlatformServiceImpl; import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO; +import com.docus.server.vo.segmentation.ocrcutconfig.OcrCutConfigVO; +import com.docus.server.vo.segmentation.ocrcutconfigtest.OcrCutConfigTestVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -34,6 +38,10 @@ import java.util.List; @RestController @RequestMapping("/ocr/file") public class FileController { + @Resource + private IOcrCutConfigService iOcrCutConfigService; + @Resource + private IOcrCutConfigTestService iOcrCutConfigTestService; @Resource private IFileUploadService iFileUploadService; @Resource @@ -61,7 +69,26 @@ public class FileController { @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) }) public Object uploadFile(@RequestPart("files") MultipartFile[] files, @Validated UploadBatchFileRequest request) throws Exception { - List segmentation = iFileUploadService.uploadFile(files, "segmentation"); + + int testData = request.getTestData(); + Double height; + Double widthStart; + Double widthEnd; + + if (testData == 1) { + OcrCutConfigTestVO ocrCutConfigTestVO = iOcrCutConfigTestService.findAll().get(0); + height = ocrCutConfigTestVO.getHeight(); + widthStart = ocrCutConfigTestVO.getWidthStart(); + widthEnd = ocrCutConfigTestVO.getWidthEnd(); + } else { + OcrCutConfigVO ocrCutConfigVO = iOcrCutConfigService.findAll().get(0); + height = ocrCutConfigVO.getHeight(); + widthStart = ocrCutConfigVO.getWidthStart(); + widthEnd = ocrCutConfigVO.getWidthEnd(); + } + + + List segmentation = iFileUploadService.uploadFile(files, "segmentation", height, widthStart, widthEnd); //将基础信息存到库表里面,后面自动分段后,需要上传到归档系统 Object result = commonService.add(segmentation, request); diff --git a/docus-segmentation/src/main/java/com/docus/server/service/impl/CommonService.java b/docus-segmentation/src/main/java/com/docus/server/service/impl/CommonService.java index ca41cbd..97dde3b 100644 --- a/docus-segmentation/src/main/java/com/docus/server/service/impl/CommonService.java +++ b/docus-segmentation/src/main/java/com/docus/server/service/impl/CommonService.java @@ -1,5 +1,6 @@ package com.docus.server.service.impl; +import com.docus.core.util.json.JSON; import com.docus.server.dto.segmentation.UploadBatchFileRequest; import com.docus.server.dto.segmentation.ocrbasic.AddOcrBasicDTO; import com.docus.server.dto.segmentation.ocrbasictest.AddOcrBasicTestDTO; @@ -55,12 +56,14 @@ public class CommonService { addOcrFileInfoDTO.setFileType(2); addOcrFileInfoDTO.setSerialNumber(UUID.randomUUID().toString().replace("-", "")); addOcrFileInfoDTO.setPicUrl(uploadFileVO.getSaveFilePath() + uploadFileVO.getFilePath()); + addOcrFileInfoDTO.setPicCompressUrl(uploadFileVO.getSaveCompressFilePath() + uploadFileVO.getFilePath()); addOcrFileInfoDTO.setPicCutUrl(uploadFileVO.getSaveCutFilePath() + uploadFileVO.getFilePath()); addOcrFileInfoDTO.setFileSize(uploadFileVO.getFileSize()); addOcrFileInfoDTO.setHeight(uploadFileVO.getHeight()); addOcrFileInfoDTO.setWidth(uploadFileVO.getWidth()); addOcrFileInfoDTO.setX(uploadFileVO.getX()); addOcrFileInfoDTO.setY(uploadFileVO.getY()); + addOcrFileInfoDTO.setJsonStr(JSON.toJSON(uploadFileVO.getParams())); addOcrFileInfoDTO.setOcrStatus(OcrStatusEnum.NO_START); files.add(addOcrFileInfoDTO); } @@ -88,12 +91,14 @@ public class CommonService { addOcrFileInfoDTO.setFileType(2); addOcrFileInfoDTO.setSerialNumber(UUID.randomUUID().toString().replace("-", "")); addOcrFileInfoDTO.setPicUrl(uploadFileVO.getSaveFilePath() + uploadFileVO.getFilePath()); + addOcrFileInfoDTO.setPicCompressUrl(uploadFileVO.getSaveCompressFilePath() + uploadFileVO.getFilePath()); addOcrFileInfoDTO.setPicCutUrl(uploadFileVO.getSaveCutFilePath() + uploadFileVO.getFilePath()); addOcrFileInfoDTO.setFileSize(uploadFileVO.getFileSize()); addOcrFileInfoDTO.setHeight(uploadFileVO.getHeight()); addOcrFileInfoDTO.setWidth(uploadFileVO.getWidth()); addOcrFileInfoDTO.setX(uploadFileVO.getX()); addOcrFileInfoDTO.setY(uploadFileVO.getY()); + addOcrFileInfoDTO.setJsonStr(JSON.toJSON(uploadFileVO.getParams())); addOcrFileInfoDTO.setOcrStatus(OcrStatusEnum.NO_START); files.add(addOcrFileInfoDTO); } diff --git a/docus-segmentation/src/main/resources/bootstrap.yml b/docus-segmentation/src/main/resources/bootstrap.yml index d0af12e..d33fab2 100644 --- a/docus-segmentation/src/main/resources/bootstrap.yml +++ b/docus-segmentation/src/main/resources/bootstrap.yml @@ -58,6 +58,7 @@ mybatis-plus: file: uploadFolder: D://docus/source/ + uploadCompressFolder: D://docus/compress/ uploadCutFolder: D://docus/cut/ ocr: diff --git a/docus-segmentation/src/main/resources/mapper/OcrFileInfoMapper.xml b/docus-segmentation/src/main/resources/mapper/OcrFileInfoMapper.xml index 4398c25..49ce62b 100644 --- a/docus-segmentation/src/main/resources/mapper/OcrFileInfoMapper.xml +++ b/docus-segmentation/src/main/resources/mapper/OcrFileInfoMapper.xml @@ -13,6 +13,7 @@ + @@ -28,13 +29,14 @@ + - id, patient_id, file_title, pic_name, file_type, sort, serial_number, pic_url, pic_cut_url, angle, ocr_text, ocr_finish_time, ocr_status, assort_id, assort_name, rule_id,hit_key,rate,file_size,height,width, x,y, create_time, update_time + id, patient_id, file_title, pic_name, file_type, sort, serial_number, pic_url, pic_compress_url,pic_cut_url, angle, ocr_text, ocr_finish_time, ocr_status, assort_id, assort_name, rule_id,hit_key,rate,file_size,height,width, x,y, json_str,create_time, update_time diff --git a/docus-segmentation/src/main/resources/mapper/OcrFileInfoTestMapper.xml b/docus-segmentation/src/main/resources/mapper/OcrFileInfoTestMapper.xml index d282f04..bfc96a3 100644 --- a/docus-segmentation/src/main/resources/mapper/OcrFileInfoTestMapper.xml +++ b/docus-segmentation/src/main/resources/mapper/OcrFileInfoTestMapper.xml @@ -13,6 +13,7 @@ + @@ -28,13 +29,14 @@ + - id, patient_id, file_title, pic_name, file_type, sort, serial_number, pic_url, pic_cut_url, angle, ocr_text, ocr_finish_time, ocr_status, assort_id, assort_name, rule_id, hit_key, rate,file_size,height,width, x,y,create_time, update_time + id, patient_id, file_title, pic_name, file_type, sort, serial_number, pic_url,pic_compress_url, pic_cut_url, angle, ocr_text, ocr_finish_time, ocr_status, assort_id, assort_name, rule_id, hit_key, rate,file_size,height,width, x,y,json_str,create_time, update_time diff --git a/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java b/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java index 501e51c..a6ebf7c 100644 --- a/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java +++ b/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java @@ -1,33 +1,30 @@ package com.docus.server; +import cn.hutool.core.img.ImgUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; -import com.docus.core.util.json.JSON; -import com.docus.server.dto.segmentation.FileDTO; -import com.docus.server.dto.segmentation.UploadBatchFileRequest; import java.io.File; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; public class ImageProcessingExample { public static void main(String[] args) throws Exception { - UploadBatchFileRequest uploadBatchFileParam = new UploadBatchFileRequest(); - System.out.println(JSON.toJSON(uploadBatchFileParam)); - - FileDTO fileDto = new FileDTO(); - System.out.println(JSON.toJSON(fileDto)); - - List fileDtoList = new ArrayList<>(); - System.out.println(JSON.toJSON(fileDtoList)); +// UploadBatchFileRequest uploadBatchFileParam = new UploadBatchFileRequest(); +// System.out.println(JSON.toJSON(uploadBatchFileParam)); +// +// FileDTO fileDto = new FileDTO(); +// System.out.println(JSON.toJSON(fileDto)); +// +// List fileDtoList = new ArrayList<>(); +// System.out.println(JSON.toJSON(fileDtoList)); // 1. 调整图片大小 -// ImgUtil.scale(FileUtil.file("input.jpg"), FileUtil.file("output.jpg"), 0.5f); // 将input.jpg缩小为原来的一半,并保存为output.jpg + ImgUtil.scale(FileUtil.file("C:\\Users\\dataexa\\Desktop\\麻醉.jpg"), FileUtil.file("C:\\Users\\dataexa\\Desktop\\output-麻醉.jpg"), 0.5f); // 2. 裁剪图片 // ImgUtil.cut(FileUtil.file("C:\\Users\\dataexa\\Desktop\\麻醉.jpg"), FileUtil.file("C:\\Users\\dataexa\\Desktop\\output1.jpg"), new Rectangle(50, 50, 2400, (3527 / 2) / 2)); // 从input.jpg中裁剪出一个200x200的区域,保存为output.jpg