二期内容提交

This commit is contained in:
2026-08-04 10:07:45 +08:00
parent b2b4ff429d
commit 57b54a5236
184 changed files with 13195 additions and 5 deletions
@@ -5,6 +5,8 @@ import com.baiyue.integration.dto.ocr.WeightOcrBatchRequest;
import com.baiyue.integration.dto.ocr.WeightOcrBatchResult;
import com.baiyue.integration.security.InternalServiceTokenGuard;
import com.baiyue.integration.service.ocr.WeightOcrBatchService;
import com.baiyue.integration.service.ocr.WeightOcrSingleImageService;
import com.baiyue.integration.dto.ocr.WeightOcrImageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
@@ -14,6 +16,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.MediaType;
import java.io.IOException;
@@ -27,6 +32,9 @@ public class WeightOcrController {
@Resource
private WeightOcrBatchService batchService;
@Resource
private WeightOcrSingleImageService singleImageService;
@Resource
private InternalServiceTokenGuard tokenGuard;
@@ -38,4 +46,13 @@ public class WeightOcrController {
tokenGuard.verify(token);
return ApiResponse.success(batchService.recognize(request));
}
@Operation(summary = "识别单张称重图片")
@PostMapping(value = "/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponse<WeightOcrImageResult> recognizeSingle(
@RequestHeader(value = "X-Internal-Service-Token", required = false) String token,
@RequestPart("file") MultipartFile file) throws IOException {
tokenGuard.verify(token);
return ApiResponse.success(singleImageService.recognize(file));
}
}
@@ -0,0 +1,75 @@
package com.baiyue.integration.service.ocr;
import com.baiyue.common.core.api.ResultCode;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.integration.config.IntegrationProperties;
import com.baiyue.integration.dto.ocr.WeightOcrBatchRequest;
import com.baiyue.integration.dto.ocr.WeightOcrImageResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
@Service
public class WeightOcrSingleImageService {
private static final Set<String> CONTENT_TYPES = Set.of("image/jpeg", "image/png", "image/bmp");
private final IntegrationProperties properties;
private final WeightOcrBatchService batches;
public WeightOcrSingleImageService(IntegrationProperties properties, WeightOcrBatchService batches) {
this.properties = properties;
this.batches = batches;
}
public WeightOcrImageResult recognize(MultipartFile file) throws IOException {
IntegrationProperties.BaiduOcr config = properties.baiduOcr();
if (config == null || config.inputRoot() == null) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "OCR输入目录未配置");
}
if (file == null || file.isEmpty()) {
throw new BizException(ResultCode.BAD_REQUEST, "称重图片不能为空");
}
String contentType = file.getContentType() == null ? "" : file.getContentType().toLowerCase(Locale.ROOT);
if (!CONTENT_TYPES.contains(contentType)) {
throw new BizException(ResultCode.BAD_REQUEST, "称重图片仅支持 JPG、PNG、BMP");
}
if (file.getSize() > config.effectiveMaxImageSize().toBytes()) {
throw new BizException(ResultCode.BAD_REQUEST, "称重图片超过允许大小");
}
String extension = "image/png".equals(contentType) ? ".png"
: ("image/bmp".equals(contentType) ? ".bmp" : ".jpg");
String requestId = UUID.randomUUID().toString();
Path root = config.inputRoot().toAbsolutePath().normalize();
Path requestDirectory = root.resolve("single").resolve(requestId).normalize();
if (!requestDirectory.startsWith(root)) {
throw new BizException(ResultCode.FORBIDDEN, "OCR输入路径越界");
}
Files.createDirectories(requestDirectory);
Path image = requestDirectory.resolve("weight" + extension);
try {
file.transferTo(image);
var batch = batches.recognize(new WeightOcrBatchRequest(
"single/" + requestId, "single-" + requestId, null, null, false));
if (batch.results() == null || batch.results().size() != 1) {
throw new BizException(ResultCode.INTERNAL_ERROR, "OCR单图识别结果数量异常");
}
return batch.results().get(0);
} finally {
deleteRequestDirectory(requestDirectory);
}
}
private void deleteRequestDirectory(Path directory) {
try (var paths = Files.walk(directory)) {
paths.sorted(Comparator.reverseOrder()).forEach(path -> {
try { Files.deleteIfExists(path); } catch (IOException ignored) { }
});
} catch (IOException ignored) { }
}
}