u
This commit is contained in:
1 parent
d4f57850a1
commit
417834bd13
6 files changed
+141
-27
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 465 KiB |
@@ -1,38 +1,88 @@
|
||||
package com.crane.test;
|
||||
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 java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.util.Base64;
|
||||
import javax.imageio.*;
|
||||
import javax.imageio.stream.ImageOutputStream;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import okhttp3.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static byte[] compressImageToTargetSize(BufferedImage image, float initialQuality, int maxSizeKB) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
float quality = initialQuality;
|
||||
int maxBytes = maxSizeKB * 1024;
|
||||
|
||||
while (quality > 0.05f) {
|
||||
baos.reset();
|
||||
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
|
||||
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
|
||||
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||
jpgWriteParam.setCompressionQuality(quality); // 0.0 ~ 1.0
|
||||
|
||||
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
|
||||
jpgWriter.setOutput(ios);
|
||||
jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam);
|
||||
jpgWriter.dispose();
|
||||
ios.close();
|
||||
|
||||
if (baos.size() <= maxBytes) {
|
||||
break;
|
||||
}
|
||||
|
||||
quality -= 0.05f;
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public static void saveCompressedImageToFile(byte[] imageBytes, String outputPath) throws IOException {
|
||||
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
||||
fos.write(imageBytes);
|
||||
}
|
||||
System.out.println("✅ 压缩图像已保存到: " + outputPath);
|
||||
}
|
||||
|
||||
public class Test{
|
||||
public static void main(String[] args) throws IOException {
|
||||
String API_URL = "http://localhost:8866/ocr";
|
||||
String imagePath = "D:\\workspace\\code\\crane\\crane-api\\crane-test\\src\\main\\java\\com\\crane\\test\\namorni-preprava-namorni-doprava-kamionova-doprava-kontejneru-2-1024x768.jpg";
|
||||
String compressedImagePath = "D:\\workspace\\code\\crane\\crane-api\\crane-test\\compressed.jpg";
|
||||
|
||||
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; // 毫秒
|
||||
// 1. 加载原始图像
|
||||
BufferedImage originalImage = ImageIO.read(new File(imagePath));
|
||||
|
||||
System.out.println("图片转 BASE64 耗时: " + durationMs1 + " ms");
|
||||
BufferedImage resizedImage = originalImage; // 保持原图尺寸不变
|
||||
|
||||
// 3. 压缩图片到小于 500KB
|
||||
byte[] compressedBytes = compressImageToTargetSize(resizedImage, 1f, 5000);
|
||||
System.out.println("压缩后图片大小: " + (compressedBytes.length / 1024) + " KB");
|
||||
|
||||
// 👉 保存压缩图像到本地
|
||||
saveCompressedImageToFile(compressedBytes, compressedImagePath);
|
||||
|
||||
// 4. 转为 Base64 编码
|
||||
String base64Image = Base64.getEncoder().encodeToString(compressedBytes);
|
||||
long base64EndTime = System.nanoTime();
|
||||
long durationMs1 = (base64EndTime - base64StartTime) / 1_000_000;
|
||||
System.out.println("图片压缩 + BASE64 编码耗时: " + durationMs1 + " ms");
|
||||
|
||||
// 5. 构建请求
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ObjectNode payload = objectMapper.createObjectNode();
|
||||
payload.put("file", base64Image);
|
||||
payload.put("fileType", 1);
|
||||
|
||||
|
||||
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服务响应较慢可设置长点
|
||||
.connectTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.writeTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.readTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
@@ -44,26 +94,21 @@ public class Test{
|
||||
.build();
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
long endTime = System.nanoTime();
|
||||
long durationMs = (endTime - startTime) / 1_000_000; // 毫秒
|
||||
|
||||
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(prunedResult);
|
||||
// 获取 rec_texts 并打印
|
||||
JsonNode recTexts = prunedResult.get("rec_texts");
|
||||
if (recTexts != null && recTexts.isArray()) {
|
||||
System.out.println("📦 rec_texts:");
|
||||
|
||||
Reference in new issue
Block a user