master
周文涛 2 years ago
parent 38fabe6bff
commit eb9b7fd11a

@ -8,10 +8,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.DictModel; import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.AssertUtils; import org.jeecg.common.util.*;
import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.filter.FileTypeFilter;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.entity.OcrIdentifyDetail; import org.jeecg.modules.ocr.entity.OcrIdentifyDetail;
import org.jeecg.modules.ocr.entity.OcrRuleCheck; import org.jeecg.modules.ocr.entity.OcrRuleCheck;
@ -27,10 +29,17 @@ import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -210,14 +219,34 @@ public class ApiController {
public Result restartTask(@RequestBody JSONObject jsonObject) throws InterruptedException { public Result restartTask(@RequestBody JSONObject jsonObject) throws InterruptedException {
System.out.println(jsonObject.toJSONString()); System.out.println(jsonObject.toJSONString());
//获取已执行完的任务 //获取已执行完的任务
List<OcrIdentify> list = ocrIdentifyService.list(new LambdaQueryWrapper<OcrIdentify>()); //List<OcrIdentify> list = ocrIdentifyService.list(new LambdaQueryWrapper<OcrIdentify>());
int i =0; List<OcrIdentifyDetail> list = ocrIdentifyDetailService.list();
for (OcrIdentifyDetail ocrIdentifyDetail : list) {
String imgPath = ocrIdentifyDetail.getImageUrl();
JSONObject semanticResult = JSONObject.parseObject(ocrIdentifyDetail.getSemanticResult());
File file = new File(imgPath);
//当原图片存在时.
if (file.exists()) {
int i = imgPath.lastIndexOf("/");
String fileUrl = "/data/ocrImage/drawDashed" + imgPath.substring(0, i);
//判断新目录是否存在,不存在则新建
FileOUtils.folderCreate(fileUrl);
String outputImagePath = fileUrl + imgPath.substring(i + 1, imgPath.length());
List<List<Point>> resultToPoints = ImageUtils.semanticResultToPoints(semanticResult);
if (resultToPoints.size()>0) {
ImageUtils.drawDashedRectangleOnImages(file.getAbsolutePath(),resultToPoints,outputImagePath);
ocrIdentifyDetail.setDrawDashedImageUrl(outputImagePath);
}
}
}
ocrIdentifyDetailService.updateBatchById(list);
/*int i =0;
for (OcrIdentify ocrIdentify : list) { for (OcrIdentify ocrIdentify : list) {
if (ocrIdentify.getStatus().equals("1")) { if (ocrIdentify.getStatus().equals("1")) {
ocrIdentifyService.updateTaskResultInfo(ocrIdentify.getId()); ocrIdentifyService.updateTaskResultInfo(ocrIdentify.getId());
}else{} }else{}
i++; i++;
} }*/
return Result.OK(list.size()+"个任务."); return Result.OK(list.size()+"个任务.");
} }
@ -232,8 +261,4 @@ public class ApiController {
return Result.OK(""); return Result.OK("");
} }
public static void main(String[] args) { }
}
}
//curl --request POST --url http://127.0.0.1:8000/semantic --header 'content-type: application/json' --data '{ "task_id": "1682299148529958914","img_path": "http://47.103.213.109:8072/files/nfs/ocr/shared_directory/09360a383f464ea0ad056145ec5b62e9/4abf97c877bbe4e4d091aef8411edd81.jpeg"}'

@ -97,6 +97,14 @@ public class OcrIdentifyDetailController extends JeecgController<OcrIdentifyDeta
record.setThumbnailImagePreviewUrl("http://47.103.213.109:8072/files"+record.getThumbnailImageUrl()); record.setThumbnailImagePreviewUrl("http://47.103.213.109:8072/files"+record.getThumbnailImageUrl());
} }
} }
//虚线图片
if (StringUtils.isNotBlank(record.getDrawDashedImageUrl())) {
File file=new File(record.getDrawDashedImageUrl());
//当原图片存在时.
if (file.exists()) {
record.setDrawDashedImagePreviewUrl("http://47.103.213.109:8072/files"+record.getDrawDashedImageUrl());
}
}
String imagePreviewUrl="http://47.103.213.109:8072/files"+record.getImageUrl(); String imagePreviewUrl="http://47.103.213.109:8072/files"+record.getImageUrl();
record.setImagePreviewUrl(imagePreviewUrl); record.setImagePreviewUrl(imagePreviewUrl);
}else{ }else{

@ -54,6 +54,10 @@ public class OcrIdentifyDetail implements Serializable {
@Excel(name = "图片地址", width = 15) @Excel(name = "图片地址", width = 15)
@ApiModelProperty(value = "图片地址") @ApiModelProperty(value = "图片地址")
private java.lang.String imageUrl; private java.lang.String imageUrl;
/**虚线图片地址*/
@Excel(name = "虚线图片地址", width = 15)
@ApiModelProperty(value = "虚线图片地址")
private java.lang.String drawDashedImageUrl;
/**预览图片地址*/ /**预览图片地址*/
@Excel(name = "预览图片地址", width = 15) @Excel(name = "预览图片地址", width = 15)
@ApiModelProperty(value = "预览图片地址") @ApiModelProperty(value = "预览图片地址")
@ -103,6 +107,10 @@ public class OcrIdentifyDetail implements Serializable {
@ApiModelProperty(value = "缩率图片预览地址") @ApiModelProperty(value = "缩率图片预览地址")
@TableField(exist = false) @TableField(exist = false)
private String thumbnailImagePreviewUrl; private String thumbnailImagePreviewUrl;
@ApiModelProperty(value = "虚线图片预览地址")
@TableField(exist = false)
private String drawDashedImagePreviewUrl;
///=========================================== ///===========================================
/*字段校验成功对象.*//* /*字段校验成功对象.*//*
@TableField(exist = false) @TableField(exist = false)

@ -1,5 +1,6 @@
package org.jeecg.modules.ocr.service.impl; package org.jeecg.modules.ocr.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -18,6 +19,7 @@ import org.jeecg.modules.ocr.mapper.OcrIdentifyMapper;
import org.jeecg.modules.ocr.model.*; import org.jeecg.modules.ocr.model.*;
import org.jeecg.modules.ocr.service.*; import org.jeecg.modules.ocr.service.*;
import org.jeecg.modules.ocr.utils.ArrayOUtils; import org.jeecg.modules.ocr.utils.ArrayOUtils;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.jeecg.modules.ocr.utils.ImageUtils; import org.jeecg.modules.ocr.utils.ImageUtils;
import org.jeecg.modules.ocr.utils.StrCharUtil; import org.jeecg.modules.ocr.utils.StrCharUtil;
import org.jeecg.modules.ocr.vo.OcrIdentifyVo; import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
@ -29,9 +31,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.awt.*;
import java.io.File; import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -103,21 +107,37 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
ocrIdentifyDetail.setIdentifyId(identifyId); ocrIdentifyDetail.setIdentifyId(identifyId);
ocrIdentifyDetail.setImageName(imgName); ocrIdentifyDetail.setImageName(imgName);
ocrIdentifyDetail.setImageUrl(imgPath); ocrIdentifyDetail.setImageUrl(imgPath);
//压缩图片
File file = new File(imgPath); File file = new File(imgPath);
//当原图片存在时. //当原图片存在时.
if (file.exists()) { if (file.exists()) {
int i = imgPath.lastIndexOf("/"); int i = imgPath.lastIndexOf("/");
String fileUrl = "/data/thumbnail" + imgPath.substring(0, i); //压缩图片
//判断新目录是否存在,不存在则新建 if(true){
ImageUtils.folderCreate(fileUrl); String fileUrl = "/data/ocrImage/thumbnail" + imgPath.substring(0, i);
String outputImagePath = fileUrl + imgPath.substring(i + 1, imgPath.length()); //判断新目录是否存在,不存在则新建
File thumbnailFile = new File(outputImagePath); FileOUtils.folderCreate(fileUrl);
//如果上次生成过缩率图,就不生成了 String outputImagePath = fileUrl + imgPath.substring(i + 1, imgPath.length());
if (!thumbnailFile.exists()) { File thumbnailFile = new File(outputImagePath);
ImageUtils.compressImage(file.getAbsolutePath(), outputImagePath, 0.3f, 0.3f); //如果上次生成过缩率图,就不生成了
if (!thumbnailFile.exists()) {
ImageUtils.compressImage(file.getAbsolutePath(), outputImagePath, 0.3f, 0.3f);
}
ocrIdentifyDetail.setThumbnailImageUrl(outputImagePath);
}
//绘制虚线
if(true){
String fileUrl = "/data/ocrImage/drawDashed" + imgPath.substring(0, i);
//判断新目录是否存在,不存在则新建
FileOUtils.folderCreate(fileUrl);
String outputImagePath = fileUrl + imgPath.substring(i + 1, imgPath.length());
File thumbnailFile = new File(outputImagePath);
List<List<Point>> resultToPoints = ImageUtils.semanticResultToPoints(semanticResult);
if (resultToPoints.size()>0) {
ImageUtils.drawDashedRectangleOnImages(file.getAbsolutePath(),resultToPoints,outputImagePath);
ocrIdentifyDetail.setDrawDashedImageUrl(outputImagePath);
}
} }
ocrIdentifyDetail.setThumbnailImageUrl(outputImagePath);
} }
//ocrIdentifyDetail.setStatus(taskStatus); //ocrIdentifyDetail.setStatus(taskStatus);
ocrIdentifyDetail.setStatus(""); ocrIdentifyDetail.setStatus("");

@ -73,6 +73,25 @@ public class FileOUtils {
return files; return files;
} }
/**
*
* @param folder
*/
public static void folderCreate(String folder){
if (StringUtils.isNotBlank(folder)) {
File file =new File(folder);
if (file.exists()) {
if (file.isDirectory()) {/*System.out.println("dir exists");*/}
else {
System.out.println("the same name file exists, can not create dir");
}
} else {
//System.out.println("目录不存在,创建");
file.mkdirs();
}
}
}
public static void main(String[] args) { public static void main(String[] args) {
List<String> stringList = fileLists(null,"E:\\workspace_2\\ocr\\images1"); List<String> stringList = fileLists(null,"E:\\workspace_2\\ocr\\images1");
System.out.println(stringList); System.out.println(stringList);

@ -1,12 +1,25 @@
package org.jeecg.modules.ocr.utils; package org.jeecg.modules.ocr.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javafx.util.Pair;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.awt.Graphics2D;
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.*; import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
/** /**
@ -19,19 +32,35 @@ public class ImageUtils {
Java使ImageIOThumbnailsJava Advanced Imaging APIJAI使ImageIO Java使ImageIOThumbnailsJava Advanced Imaging APIJAI使ImageIO
*/ */
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
String sourceImagePath="C:\\Users\\Denim\\Desktop\\aabebdf71887c6c2707c65211cac2d3a.jpeg"; String sourceImagePath = "C:\\Users\\Denim\\Desktop\\aabebdf71887c6c2707c65211cac2d3a.jpeg";
String outputImagePath="C:\\Users\\Denim\\Desktop\\ds-aabebdf71887c6c2707c65211cac2d3a.jpeg"; String outputImagePath = "C:\\Users\\Denim\\Desktop\\ds-aabebdf71887c6c2707c65211cac2d3a.jpeg";
compressImage(sourceImagePath,outputImagePath,0.25f,0.25f); //compressImage(sourceImagePath,outputImagePath,0.25f,0.25f);
List<Point> points = new ArrayList<>();
points.add(new Point(9, 2924));
points.add(new Point(107, 2924));
points.add(new Point(107, 2959));
points.add(new Point(9, 2959));
List<Point> points2 = new ArrayList<>();
points2.add(new Point(1282, 1576));
points2.add(new Point(2764, 1594));
points2.add(new Point(2763, 1671));
points2.add(new Point(1281, 1653));
List<List<Point>> pointList = new ArrayList<>();
pointList.add(points);
pointList.add(points2);
drawDashedRectangleOnImages("http://47.103.213.109:8072/files/nfs/ocr/shared_directory/fdd50089f33e4b4391b8c88b6299735b/a86aff69fc3ff5397d038b4dbcde6165.jpeg", pointList, "C:\\Users\\Denim\\Desktop\\output_image.png");
//drawDashedRectangleOnImage("http://47.103.213.109:8072/files/nfs/ocr/shared_directory/fdd50089f33e4b4391b8c88b6299735b/a86aff69fc3ff5397d038b4dbcde6165.jpeg", points, "C:\\Users\\Denim\\Desktop\\output_image.png");
} }
/** /**
* *
*
* @param sourceImagePath * @param sourceImagePath
* @param outputImagePath * @param outputImagePath
* @param scale * @param scale
* @param quality * @param quality
*/ */
public static void compressImage(String sourceImagePath, String outputImagePath,Float scale,Float quality){ public static void compressImage(String sourceImagePath, String outputImagePath, Float scale, Float quality) {
try { try {
Thumbnails.of(new File(sourceImagePath)) Thumbnails.of(new File(sourceImagePath))
.scale(scale) //图片大小(长宽)压缩比例 从0-11表示原图 .scale(scale) //图片大小(长宽)压缩比例 从0-11表示原图
@ -43,46 +72,109 @@ public class ImageUtils {
} }
} }
// 获取图片的旋转角度信息 /**
private static int getOrientation(File imageFile) throws IOException { * 线
// TODO: 从图片的元数据中获取旋转角度信息,具体实现略 *
// 这里假设返回的旋转角度为0 * @param imagePath
return 90; * @param points
} * @param outputPath
*/
public static void drawDashedRectangleOnImage(String imagePath, List<Point> points, String outputPath) {
try {
// Load the image
BufferedImage image = ImageIO.read(new URL(imagePath));
Graphics2D g2d = image.createGraphics();
// 设置线的颜色
g2d.setColor(new Color(255, 30, 30));
// 定义虚线的样式
float[] dash = {5.0f};
BasicStroke dashed =
new BasicStroke(3.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
5.0f, dash, 0.0f);
g2d.setStroke(dashed);
// 创建一个多边形
Polygon polygon = new Polygon();
for (Point point : points) {
polygon.addPoint(point.x, point.y);
}
// 画出多边形
g2d.drawPolygon(polygon);
// 根据旋转角度调整图片 g2d.dispose();
private static BufferedImage adjustOrientation(BufferedImage image, int orientation) { // Save the image
// TODO: 根据旋转角度调整图片,具体实现略 ImageIO.write(image, "png", new File(outputPath));
// 这里假设不进行任何旋转调整 } catch (IOException e) {
return image; e.printStackTrace();
}
} }
// 压缩图片
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(); public static void drawDashedRectangleOnImages(String imagePath, List<List<Point>> pointList, String outputPath) {
g2d.drawImage(scaledImage, 0, 0, null); try {
g2d.dispose(); // Load the image
BufferedImage image = ImageIO.read(new URL(imagePath));
return compressedImage; for (List<Point> points : pointList) {
} Graphics2D g2d = image.createGraphics();
// 设置线的颜色
g2d.setColor(new Color(255, 30, 30));
// 定义虚线的样式
float[] dash = {5.0f};
BasicStroke dashed =
new BasicStroke(3.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
5.0f, dash, 0.0f);
g2d.setStroke(dashed);
// 创建一个多边形
Polygon polygon = new Polygon();
for (Point point : points) {
polygon.addPoint(point.x, point.y);
}
// 画出多边形
g2d.drawPolygon(polygon);
g2d.dispose();
}
// Save the image
ImageIO.write(image, "png", new File(outputPath));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void folderCreate(String folder){ /**
if (StringUtils.isNotBlank(folder)) { *
File file =new File(folder); *
if (file.exists()) { * @param semanticResult
if (file.isDirectory()) {/*System.out.println("dir exists");*/} * @return
else { */
System.out.println("the same name file exists, can not create dir"); public static List<List<Point>> semanticResultToPoints(JSONObject semanticResult) {
List<List<Point>> lists = new ArrayList<>();
for (String s : semanticResult.keySet()) {
if ("ocr_res".equals(s) || "其他".equals(s)) {
continue;
}
JSONArray jsonArray = semanticResult.getJSONArray(s);
if (jsonArray.size() > 0) {
List<JSONObject> jos = jsonArray.toJavaList(JSONObject.class);
for (JSONObject jo : jos) {
List<Point> points = new ArrayList<>();
if (jo.containsKey("area")) {
List<JSONObject> areas = jo.getJSONArray("area").toJavaList(JSONObject.class);
for (JSONObject area : areas) {
Integer[] doubles = area.values().toArray(new Integer[0]);
points.add(new Point(doubles[0], doubles[1]));
}
}
if (points.size() > 0) {
lists.add(points);
}
} }
} else {
//System.out.println("目录不存在,创建");
file.mkdirs();
} }
} }
return lists;
} }
} }

Loading…
Cancel
Save