master
周文涛 2 years ago
parent e922f825b6
commit 8c18316bb4

@ -39,6 +39,13 @@
<artifactId>drag-free</artifactId> <artifactId>drag-free</artifactId>
<version>1.0.1</version> <version>1.0.1</version>
</dependency> </dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<!-- 积木报表 mongo redis 支持包 <!-- 积木报表 mongo redis 支持包
<dependency> <dependency>
<groupId>org.jeecgframework.jimureport</groupId> <groupId>org.jeecgframework.jimureport</groupId>

@ -107,8 +107,6 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
File file = new File(imgPath); File file = new File(imgPath);
//当原图片存在时. //当原图片存在时.
if (file.exists()) { if (file.exists()) {
int maxWidth = 800;// 压缩后图片的最大宽度
int maxHeight = 600;// 压缩后图片的最大高度
int i = imgPath.lastIndexOf("/"); int i = imgPath.lastIndexOf("/");
String fileUrl = "/data/thumbnail" + imgPath.substring(0, i); String fileUrl = "/data/thumbnail" + imgPath.substring(0, i);
//判断新目录是否存在,不存在则新建 //判断新目录是否存在,不存在则新建
@ -117,7 +115,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
File thumbnailFile = new File(outputImagePath); File thumbnailFile = new File(outputImagePath);
//如果上次生成过缩率图,就不生成了 //如果上次生成过缩率图,就不生成了
if (!thumbnailFile.exists()) { if (!thumbnailFile.exists()) {
ImageUtils.compressImage(file.getAbsolutePath(), outputImagePath, maxWidth, maxHeight); ImageUtils.compressImage(file.getAbsolutePath(), outputImagePath, 0.5f, 0.5f);
} }
ocrIdentifyDetail.setThumbnailImageUrl(outputImagePath); ocrIdentifyDetail.setThumbnailImageUrl(outputImagePath);
} }
@ -321,7 +319,8 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
public void updateTaskResultInfo(String id){ public void updateTaskResultInfo(String id){
OcrIdentifyVo ocrIdentifyVo = this.findById(id); OcrIdentifyVo ocrIdentifyVo = this.findById(id);
List<OcrIdentifyDetail> identifyDetails = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyVo.getId()); List<OcrIdentifyDetail> identifyDetails = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyVo.getId());
for (OcrIdentifyDetail identifyDetail : identifyDetails) {
/*for (OcrIdentifyDetail identifyDetail : identifyDetails) {
JSONObject semanticResult = JSONObject.parseObject(identifyDetail.getSemanticResult()); JSONObject semanticResult = JSONObject.parseObject(identifyDetail.getSemanticResult());
String imgPath = identifyDetail.getImageUrl(); String imgPath = identifyDetail.getImageUrl();
// 进行数据化 结构 // 进行数据化 结构
@ -415,7 +414,8 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
identifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化 identifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化
ocrIdentifyDetailService.updateById(identifyDetail); ocrIdentifyDetailService.updateById(identifyDetail);
} }
} }*/
//=======规则检查配置 //=======规则检查配置
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo(); OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
Map<String, Map<String, String>> configRuleTypeMap = ocrRuleCheckVo.getConfigRuleTypeMap(); Map<String, Map<String, String>> configRuleTypeMap = ocrRuleCheckVo.getConfigRuleTypeMap();
@ -443,6 +443,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
if (ruleValidation) { if (ruleValidation) {
fieldRightMap.put(tag, result); fieldRightMap.put(tag, result);
} else if (lastResult == null || !lastResult.getRuleValidation()) { } else if (lastResult == null || !lastResult.getRuleValidation()) {
result.setFailureReason(result.getTagName()+"未获取到结果");
fieldRightMap.put(tag, result); fieldRightMap.put(tag, result);
} }
} }

@ -1,12 +1,13 @@
package org.jeecg.modules.ocr.utils; package org.jeecg.modules.ocr.utils;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.awt.Graphics2D;
import javax.imageio.ImageIO; import java.awt.Image;
import java.awt.*; import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.*;
import java.io.IOException; import javax.imageio.ImageIO;
/** /**
* @Description * @Description
@ -17,49 +18,52 @@ public class ImageUtils {
/* /*
Java使ImageIOThumbnailsJava Advanced Imaging APIJAI使ImageIO Java使ImageIOThumbnailsJava Advanced Imaging APIJAI使ImageIO
*/ */
public static void main(String[] args) { public static void main(String[] args) throws FileNotFoundException {
String inputImagePath = "C:\\Users\\Denim\\Desktop\\385cb7b31e14c05c1aa1613170118772.jpeg"; // 输入图片的路径
String outputImagePath = "C:\\Users\\Denim\\Desktop\\thumbnail\\sl-385cb7b31e14c05c1aa1613170118772.jpeg"; // 压缩后图片的保存路径
int maxWidth = 800; // 压缩后图片的最大宽度
int maxHeight = 600; // 压缩后图片的最大高度
compressImage(inputImagePath, outputImagePath, maxWidth, maxHeight);
} }
/** /**
* *
* @param inputImagePath * @param sourceImagePath
* @param outputImagePath * @param outputImagePath
* @param maxWidth * @param scale
* @param maxHeight * @param quality
*/ */
public static void compressImage(String inputImagePath, String outputImagePath, int maxWidth, int maxHeight) { public static void compressImage(String sourceImagePath, String outputImagePath,Float scale,Float quality){
try { try {
// 读取原始图片 Thumbnails.of(new File(sourceImagePath))
File inputFile = new File(inputImagePath); .scale(scale) //图片大小(长宽)压缩比例 从0-11表示原图
BufferedImage inputImage = ImageIO.read(inputFile); .outputQuality(quality) //图片质量压缩比例 从0-1越接近1质量越好
// 计算压缩比例,并确定输出图片的大小 .toOutputStream(new FileOutputStream(outputImagePath));
double widthRatio = (double) maxWidth / inputImage.getWidth();
double heightRatio = (double) maxHeight / inputImage.getHeight();
double ratio = Math.min(widthRatio, heightRatio);
int newWidth = (int) (inputImage.getWidth() * ratio);
int newHeight = (int) (inputImage.getHeight() * ratio);
// 创建缩放后的图片对象
BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.getType());
// 绘制缩放后的图像
Graphics2D graphics2D = outputImage.createGraphics();
graphics2D.drawImage(inputImage, 0, 0, newWidth, newHeight, null);
graphics2D.dispose();
// 写入压缩后的图片到输出路径
ImageIO.write(outputImage, "jpg", new File(outputImagePath));
System.out.println("图片压缩完成!");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/*
`compressImage`使`ImageIO``BufferedImage`使`Graphics2D` // 获取图片的旋转角度信息
JPEGJPEG private static int getOrientation(File imageFile) throws IOException {
*/ // TODO: 从图片的元数据中获取旋转角度信息,具体实现略
// 这里假设返回的旋转角度为0
return 90;
}
// 根据旋转角度调整图片
private static BufferedImage adjustOrientation(BufferedImage image, int orientation) {
// TODO: 根据旋转角度调整图片,具体实现略
// 这里假设不进行任何旋转调整
return image;
}
// 压缩图片
private static BufferedImage compressImage(BufferedImage image, int targetWidth, int targetHeight) {
Image scaledImage = image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = compressedImage.createGraphics();
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
return compressedImage;
}

Loading…
Cancel
Save