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

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

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

@ -1,12 +1,13 @@
package org.jeecg.modules.ocr.utils;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.*;
import javax.imageio.ImageIO;
/**
* @Description
@ -17,49 +18,52 @@ public class ImageUtils {
/*
Java使ImageIOThumbnailsJava Advanced Imaging APIJAI使ImageIO
*/
public static void main(String[] args) {
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);
public static void main(String[] args) throws FileNotFoundException {
}
/**
*
* @param inputImagePath
*
* @param sourceImagePath
* @param outputImagePath
* @param maxWidth
* @param maxHeight
* @param scale
* @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 {
// 读取原始图片
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// 计算压缩比例,并确定输出图片的大小
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("图片压缩完成!");
Thumbnails.of(new File(sourceImagePath))
.scale(scale) //图片大小(长宽)压缩比例 从0-11表示原图
.outputQuality(quality) //图片质量压缩比例 从0-1越接近1质量越好
.toOutputStream(new FileOutputStream(outputImagePath));
} catch (IOException e) {
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