|
@@ -0,0 +1,93 @@
|
|
|
|
+package com.qmth.teachcloud.mark.utils;
|
|
|
|
+
|
|
|
|
+import com.qmth.teachcloud.common.bean.dto.mark.PictureConfig;
|
|
|
|
+import com.qmth.teachcloud.mark.dto.mark.manage.TrackDTO;
|
|
|
|
+
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
+import javax.imageio.ImageReadParam;
|
|
|
|
+import javax.imageio.ImageReader;
|
|
|
|
+import javax.imageio.stream.ImageInputStream;
|
|
|
|
+import java.awt.*;
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+
|
|
|
|
+public class AiUtil {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成评卷区在原图中的位置
|
|
|
|
+ *
|
|
|
|
+ * @param subFile 原图
|
|
|
|
+ * @param pictureConfig 评卷区坐标
|
|
|
|
+ */
|
|
|
|
+ public TrackDTO setOffset(File subFile, PictureConfig pictureConfig) {
|
|
|
|
+ try {
|
|
|
|
+ BufferedImage image = ImageIO.read(subFile);
|
|
|
|
+ int width = image.getWidth();
|
|
|
|
+ int height = image.getHeight();
|
|
|
|
+
|
|
|
|
+ TrackDTO trackDTO = new TrackDTO();
|
|
|
|
+ int x = (int) (width * pictureConfig.getX());
|
|
|
|
+ int y = (int) (height * pictureConfig.getY());
|
|
|
|
+ int w = (int) (width * pictureConfig.getW());
|
|
|
|
+ int h = (int) (height * pictureConfig.getH());
|
|
|
|
+
|
|
|
|
+ trackDTO.setOffsetX(x + w / 3);
|
|
|
|
+ trackDTO.setOffsetY(y + h / 3);
|
|
|
|
+ return trackDTO;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 裁剪图片
|
|
|
|
+ *
|
|
|
|
+ * @param sourceFile 源文件
|
|
|
|
+ * @param x x起点
|
|
|
|
+ * @param y y起点
|
|
|
|
+ * @param width 宽度
|
|
|
|
+ * @param height 调试
|
|
|
|
+ * @param outFile 输出文件
|
|
|
|
+ */
|
|
|
|
+ public File subImg(File sourceFile, int x, int y, int width, int height, File outFile) {
|
|
|
|
+ ImageInputStream iis = null;
|
|
|
|
+ try {
|
|
|
|
+ if (outFile.exists()) {
|
|
|
|
+ outFile.delete();
|
|
|
|
+ }
|
|
|
|
+ //图片输入流
|
|
|
|
+ iis = ImageIO.createImageInputStream(sourceFile);
|
|
|
|
+ //图片读取器
|
|
|
|
+ Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
|
|
|
|
+
|
|
|
|
+ if (it.hasNext()) {
|
|
|
|
+ ImageReader r = it.next();
|
|
|
|
+ //设置输入流
|
|
|
|
+ r.setInput(iis, true);
|
|
|
|
+ //读取参数
|
|
|
|
+ ImageReadParam param = r.getDefaultReadParam();
|
|
|
|
+ //创建要截取的矩形范围
|
|
|
|
+ Rectangle rect = new Rectangle(x, y, width, height);
|
|
|
|
+ //设置截取范围参数
|
|
|
|
+ param.setSourceRegion(rect);
|
|
|
|
+ //读取截图数据
|
|
|
|
+ BufferedImage bi = r.read(0, param);
|
|
|
|
+ // 保存图片
|
|
|
|
+ ImageIO.write(bi, "jpg", outFile);
|
|
|
|
+ }
|
|
|
|
+ return outFile;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ iis.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|