优化图片尺寸

This commit is contained in:
super
2026-05-24 02:31:58 +08:00
parent 1e087c1aae
commit a1376b51b0
7 changed files with 114 additions and 58 deletions

View File

@@ -35,18 +35,21 @@ class SimilarAsinImageEmbedderTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(src, "jpg", baos);
byte[] thumb = embedder.resizeImage("https://example.com/big.jpg", baos.toByteArray());
SimilarAsinImageEmbedder.ResizedImage thumb = embedder.resizeImage("https://example.com/big.jpg", baos.toByteArray());
assertNotNull(thumb);
assertTrue(thumb.length > 0, "thumb 不可为空");
assertTrue(thumb.length <= SimilarAsinImageEmbedder.MAX_THUMB_SIZE_BYTES,
"thumb 字节应 <= 40KB 上限,实际=" + thumb.length);
assertNotNull(thumb.bytes());
assertTrue(thumb.bytes().length > 0, "thumb 不可为空");
assertTrue(thumb.bytes().length <= SimilarAsinImageEmbedder.MAX_THUMB_SIZE_BYTES,
"thumb 字节应 <= 300KB 上限,实际=" + thumb.bytes().length);
BufferedImage decoded = ImageIO.read(new java.io.ByteArrayInputStream(thumb));
// 验证返回的像素尺寸与实际解码一致
assertEquals(SimilarAsinImageEmbedder.TARGET_LONG_EDGE_PX, Math.max(thumb.width(), thumb.height()),
"长边应等于 TARGET_LONG_EDGE_PX=" + SimilarAsinImageEmbedder.TARGET_LONG_EDGE_PX);
BufferedImage decoded = ImageIO.read(new java.io.ByteArrayInputStream(thumb.bytes()));
assertNotNull(decoded, "thumb 应可被 ImageIO 重新解码");
int longEdge = Math.max(decoded.getWidth(), decoded.getHeight());
assertEquals(SimilarAsinImageEmbedder.TARGET_LONG_EDGE_PX, longEdge,
"长边应等于 TARGET_LONG_EDGE_PX=300");
assertEquals(thumb.width(), decoded.getWidth(), "返回宽度应与解码宽度一致");
assertEquals(thumb.height(), decoded.getHeight(), "返回高度应与解码高度一致");
}
@Test