|
|
@ -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中有几个可以使用的库,比如ImageIO、Thumbnails和Java Advanced Imaging API(JAI)。以下是一个使用ImageIO库来进行图片压缩的示例:
|
|
|
|
当涉及到图像压缩时,Java中有几个可以使用的库,比如ImageIO、Thumbnails和Java Advanced Imaging API(JAI)。以下是一个使用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-1,1表示原图
|
|
|
|
.scale(scale) //图片大小(长宽)压缩比例 从0-1,1表示原图
|
|
|
@ -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|