refactor(pngutil): 优化试卷id识别为学生和试卷组合对象

- 将分析试卷id方法返回类型改为StudentExamId,包含学生id和试卷id
- 修改正则匹配逻辑,从单个id提取改为依次提取试卷id和学生id
- 增加StudentExamId类,封装学生id和试卷id字段
- 试卷生成与接口数据构建均添加学生相关信息,完善数据结构
- 相关测试调用修改为使用StudentExamId类型并打印对象信息
This commit is contained in:
lbw
2025-12-12 17:37:28 +08:00
parent e729ddc829
commit feabb6d4b1
8 changed files with 52 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -66,7 +66,10 @@ public class ExamWordsController {
.build();
Map<String, Object> data = new HashMap<>();
data.put("assessment_id", examWordsDO.getId());
data.put("examId", examWordsDO.getId());
data.put("studentId", studentIds.get(0));
data.put("studentStr","小明三班一年级");
data.put("examStr", examWordsDO.getTitle());
data.put("words", assessmentWords);
data.put("answer", assessmentWords);

View File

@@ -0,0 +1,16 @@
package com.yinlihupo.enlish.service.model.bo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class StudentExamId {
private Integer studentId;
private Integer examId;
}

View File

@@ -2,6 +2,7 @@ package com.yinlihupo.enlish.service.utils;
import com.yinlihupo.enlish.service.constant.ExamWordsConstant;
import com.yinlihupo.enlish.service.model.bo.CoordinatesXY;
import com.yinlihupo.enlish.service.model.bo.StudentExamId;
import com.yinlihupo.enlish.service.model.bo.Word;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
@@ -192,12 +193,12 @@ public class PngUtil {
}
}
public static Integer analyzeExamWordsId(String imagePath, String tessdataPath, List<CoordinatesXY> coordinatesXIES) {
public static StudentExamId analyzeExamWordsId(String imagePath, String tessdataPath, List<CoordinatesXY> coordinatesXIES) {
// 1. 读取图片
Mat src = Imgcodecs.imread(imagePath);
if (src.empty()) {
System.out.println("无法加载图片");
return 0;
return null;
}
// 2. 截取左上角区域 (ROI)
@@ -234,23 +235,32 @@ public class PngUtil {
// 6. 使用正则表达式提取 ID
// 匹配 "Assessment_id" 后面的数字
Pattern pattern = Pattern.compile("id[:\\s_]+(\\d+)");
Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
String id = matcher.group(1);
System.out.println("-------------------------");
System.out.println("成功提取 ID: " + id);
System.out.println("-------------------------");
return Integer.parseInt(id);
} else {
System.out.println("未找到匹配的 ID 格式");
Pattern pattern = Pattern.compile("\\d+");
StudentExamId studentExamId = getStudentExamId(pattern, result);
if (studentExamId.getExamId() != 0 && studentExamId.getStudentId() != 0) {
return studentExamId;
}
} catch (TesseractException e) {
System.err.println("OCR 识别出错: " + e.getMessage());
}
return 0;
return null;
}
private static @NonNull StudentExamId getStudentExamId(Pattern pattern, String result) {
Matcher matcher = pattern.matcher(result);
StudentExamId studentExamId = new StudentExamId(0, 0);
for (int i = 0; i < 2; i++) {
if (matcher.find()) {
String group = matcher.group();
if (i == 0) {
studentExamId.setExamId(Integer.parseInt(group));
} else {
studentExamId.setStudentId(Integer.parseInt(group));
}
}
}
return studentExamId;
}
// 辅助方法Mat 转 BufferedImage

Binary file not shown.

Before

Width:  |  Height:  |  Size: 450 KiB

After

Width:  |  Height:  |  Size: 461 KiB

View File

@@ -1,6 +1,7 @@
package com.yinlihupo.enlish.service.omr;
import com.yinlihupo.enlish.service.model.bo.CoordinatesXY;
import com.yinlihupo.enlish.service.model.bo.StudentExamId;
import com.yinlihupo.enlish.service.utils.PngUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@@ -41,7 +42,7 @@ public class TestOmr {
public void testInteger(){
String filePath = "C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\p3.png";
List<CoordinatesXY> coordinatesXIES = PngUtil.analysisXY(filePath);
Integer examWordsId = PngUtil.analyzeExamWordsId(filePath, tessdataPath, coordinatesXIES);
log.info("examWordsId:{}", examWordsId);
StudentExamId studentExamId = PngUtil.analyzeExamWordsId(filePath, tessdataPath, coordinatesXIES);
log.info("studentExamId:{}",studentExamId);
}
}

View File

@@ -42,8 +42,12 @@ public class ExamTest {
.build();
Map<String, Object> data = new HashMap<>();
data.put("id", examWordsDO.getId());
data.put("examId", examWordsDO.getId());
data.put("studentId", 1);
data.put("studentStr","小明三班一年级");
data.put("examStr", examWordsDO.getTitle());
data.put("words", assessmentWords);
data.put("answer", assessmentWords);
// 4. 渲染并输出
try (XWPFTemplate template = XWPFTemplate.compile("C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\assessment_v5.docx", config)) {