This commit is contained in:
oneao committed 2025-08-12 17:28:23 +08:00
1 parent 1fd0697aee
commit 9af3234672
13 files changed
+358 -26

No files matched your search

+97 -11
View File
@@ -263,24 +263,39 @@ POST /ocr
**Java调用实例**
```java
package cn.g3soft;
import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws IOException {
String API_URL = "http://localhost:8080/ocr";
String imagePath = "./demo.jpg";
public class Test {
File file = new File(imagePath);
byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
String base64Image = Base64.getEncoder().encodeToString(fileContent);
public static void main(String[] args) throws IOException {
String API_URL = "http://localhost:8866/ocr";
String imagePath = "D:\\code\\python-project\\boxocr\\100-2\\20241004082017A007.jpg";
// 压缩并限制最大500KB大小
long maxSizeBytes = 500 * 1024;
long compressStart = System.currentTimeMillis();
byte[] compressedBytes = compressImageToMaxSize(imagePath, maxSizeBytes);
long compressEnd = System.currentTimeMillis();
System.out.println("Image compression time: " + (compressEnd - compressStart) + " ms");
System.out.println("Compressed image size: " + compressedBytes.length + " bytes");
String base64Image = Base64.getEncoder().encodeToString(compressedBytes);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode payload = objectMapper.createObjectNode();
@@ -289,14 +304,18 @@ public class Main {
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, payload.toString());
RequestBody body = RequestBody.create(JSON, payload.toString());
long ocrStart = System.currentTimeMillis();
Request request = new Request.Builder()
.url(API_URL)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
long ocrEnd = System.currentTimeMillis();
System.out.println("OCR request elapsed time: " + (ocrEnd - ocrStart) + " ms");
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
@@ -309,9 +328,28 @@ public class Main {
JsonNode prunedResult = item.get("prunedResult");
System.out.println("Pruned Result [" + i + "]: " + prunedResult.toString());
// 提取 rec_texts 和 rec_scores
JsonNode recTextsNode = prunedResult.get("rec_texts");
JsonNode recScoresNode = prunedResult.get("rec_scores");
if (recTextsNode != null && recScoresNode != null && recTextsNode.isArray() && recScoresNode.isArray()) {
System.out.println("rec_texts:");
for (JsonNode textNode : recTextsNode) {
System.out.println(" " + textNode.asText());
}
System.out.println("rec_scores:");
for (JsonNode scoreNode : recScoresNode) {
System.out.println(" " + scoreNode.asDouble());
}
} else {
System.out.println("rec_texts or rec_scores not found or invalid format.");
}
// 保存 OCR 图片为文件
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);
@@ -322,6 +360,54 @@ public class Main {
}
}
}
/**
* 压缩图片,最大限制文件大小,不超过maxFileSizeBytes
* 通过递减压缩质量尝试实现大小限制,最低质量0.1
*/
public static byte[] compressImageToMaxSize(String imagePath, long maxFileSizeBytes) throws IOException {
File file = new File(imagePath);
BufferedImage image = ImageIO.read(file);
if (image == null) {
throw new IOException("Failed to read image from " + imagePath);
}
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float quality = 0.9f;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (quality >= 0.1f) {
baos.reset();
jpgWriteParam.setCompressionQuality(quality);
try (ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) {
jpgWriter.setOutput(ios);
jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam);
}
if (baos.size() <= maxFileSizeBytes) {
jpgWriter.dispose();
return baos.toByteArray();
}
quality -= 0.05f;
}
// 质量降至最低仍未达到限制,返回最后压缩结果
jpgWriter.dispose();
return baos.toByteArray();
}
}
```
# 警告/报错
## Please use PaddlePaddle with GPU version.
原因:当前 PaddlePaddle 版本不是GPU的,而是CPU的。
解决方法:安装GPU版本的PaddlePaddle