u
This commit is contained in:
1 parent
1fd0697aee
commit
9af3234672
13 files changed
+358
-26
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 408 KiB |
@@ -46,6 +46,11 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 186 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 189 KiB |
@@ -0,0 +1,143 @@
|
||||
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 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 Test {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String API_URL = "http://localhost:8866/ocr";
|
||||
String imagePath = "D:\\code\\python-project\\boxocr\\conno\\6ffd244b-1d27-4084-998b-e4d7ac076c5f.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();
|
||||
payload.put("file", "http://117.72.182.135:9000/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMTo5MDAxL3Rlc3QvMjAyNDEwMDUwNDA3MzVBMjk0LmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUZZTEUwQ0NJOTJQOUYyQ0ozTUc5JTJGMjAyNTA4MTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwODEyVDA2MDAwMVomWC1BbXotRXhwaXJlcz00MzE5OSZYLUFtei1TZWN1cml0eS1Ub2tlbj1leUpoYkdjaU9pSklVelV4TWlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKaFkyTmxjM05MWlhraU9pSkdXVXhGTUVORFNUa3lVRGxHTWtOS00wMUhPU0lzSW1WNGNDSTZNVGMxTlRBeU1USTBPU3dpY0dGeVpXNTBJam9pWVdSdGFXNGlmUS5rcWFORTVfalRBOWlVZTl3Um1acjVhemxCbzQzeGVOWnYyb09GYWhjbjhXQ25UZzlqREt3bktVekJiemkxVWZEQUlqenhCaXlLN19GSXZaNjFSeXNrQSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmdmVyc2lvbklkPW51bGwmWC1BbXotU2lnbmF0dXJlPTk0MjQyZDIwMTgwNWM4NWM1ZWZjZTg5MzgxYWU5M2M4YjE0OWQ3N2EzNTZhYWEzMGJlYWVmNTlmODMxOTk5M2Y");
|
||||
payload.put("fileType", 1);
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.connectTimeout(0, java.util.concurrent.TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, java.util.concurrent.TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(0, java.util.concurrent.TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
|
||||
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
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);
|
||||
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());
|
||||
|
||||
// 提取 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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.err.println("Request failed with HTTP code: " + response.code());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 压缩图片,最大限制文件大小,不超过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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user