This commit is contained in:
oneao committed 2025-07-01 17:29:47 +08:00
1 parent 7c64e29ffb
commit a54ecde32d
12 files changed
+360 -12

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

@@ -11,19 +11,30 @@ import java.util.Base64;
public class Test{
public static void main(String[] args) throws IOException {
String API_URL = "http://localhost:8080/ocr";
String imagePath = "./demo.jpg";
String API_URL = "http://localhost:8866/ocr";
String imagePath = "D:\\workspace\\code\\crane\\crane-api\\crane-test\\src\\main\\java\\com\\crane\\test\\tempImageNYfUPw-750x563.webp";
File file = new File(imagePath);
long base64StartTime = System.nanoTime();
byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
String base64Image = Base64.getEncoder().encodeToString(fileContent);
long base64EndTime = System.nanoTime();
long durationMs1 = (base64EndTime - base64StartTime) / 1_000_000; // 毫秒
System.out.println("图片转 BASE64 耗时: " + durationMs1 + " ms");
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode payload = objectMapper.createObjectNode();
payload.put("file", base64Image);
payload.put("fileType", 1);
OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 连接超时,建议20-30秒
.writeTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 写请求体超时
.readTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 读取响应超时,OCR服务响应较慢可设置长点
.build();
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, payload.toString());
@@ -32,25 +43,35 @@ public class Test{
.post(body)
.build();
long startTime = System.nanoTime();
try (Response response = client.newCall(request).execute()) {
long endTime = System.nanoTime();
long durationMs = (endTime - startTime) / 1_000_000; // 毫秒
System.out.println("⏱️ 请求耗时: " + durationMs + " ms");
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
JsonNode result = root.get("result");
JsonNode ocrResults = result.get("ocrResults");
for (int i = 0; i < ocrResults.size(); i++) {
JsonNode item = ocrResults.get(i);
JsonNode prunedResult = item.get("prunedResult");
System.out.println("Pruned Result [" + i + "]: " + prunedResult.toString());
String ocrImageBase64 = item.get("ocrImage").asText();
byte[] ocrImageBytes = Base64.getDecoder().decode(ocrImageBase64);
String ocrImgPath = "ocr_result_" + i + ".jpg";
try (FileOutputStream fos = new FileOutputStream(ocrImgPath)) {
fos.write(ocrImageBytes);
System.out.println("Saved OCR image to: " + ocrImgPath);
System.out.println(prunedResult);
// 获取 rec_texts 并打印
JsonNode recTexts = prunedResult.get("rec_texts");
if (recTexts != null && recTexts.isArray()) {
System.out.println("📦 rec_texts:");
for (JsonNode text : recTexts) {
System.out.println(" ➤ " + text.asText());
}
} else {
System.out.println("❗ 未检测到 rec_texts 字段");
}
}
} else {
Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB