[英]Unknown bytes while reading an image from ImageIO after sending it with ImageIO
[英]ImageIo reading from byte data results in "Empty region!"
我有兩個服務,通過 redis(二進制安全)從原始 heic/heif 圖像交易二進制數據。 由於 ImageIo 不支持該圖像類型,我在 nodejs 中構建了一個單獨的服務,它只是將 heic/heif 圖像轉換為 jpeg。 在 nodejs 服務上轉換后,它再次將字符串編碼 (latin1) 圖像字節保存在 Redis 中,其他服務 (javax.imagio) 檢索它,將字符串解碼回字節並嘗試從中讀取。 在這一點上,我收到以下異常:
java.lang.IllegalArgumentException: Empty region!
at java.desktop/javax.imageio.ImageReader.computeRegions(ImageReader.java:2687)
我絕不是圖像處理專家,所以如果有人能解釋我做錯了什么,我會很高興。 下面你可以看到我的代碼:
我的字符串序列化器:
public static Optional<String> encodeBytesToString(byte[] data) {
return Optional.ofNullable((data == null ? null : new String(data, StandardCharsets.ISO_8859_1)));
}
public static byte[] decodeStringToBinary(String string) {
return (string == null ? null : string.getBytes(StandardCharsets.ISO_8859_1));
}
商業邏輯
BufferedImage image = imageService.readImage(decodeStringToBinary(convertedImage.getData()));
讀者:
public BufferedImage readImage(byte[] data) throws IOException {
return ImageIO.read(new ByteArrayInputStream(data));
}
computeRegions 方法(拋出異常)的文檔說明如下:
如果源區域或目標區域的寬度或高度最終為 0,則會拋出 IllegalArgumentException。
不知道為什么這些屬性為 0。我將以下庫用於 nodejs 服務: https://github.com/catdad-experiments/heic-convert如果您使用自述文件中的示例,它會起作用,它將輸出緩沖區保存到磁盤. 我想跳過它,直接將字節傳遞給 redis。 我是在做一些根本性的錯誤還是這里發生了什么?
在nodejs的代碼下面:
const outputBuffer = await convert({
buffer: inputBuffer,
format: OutputFormatEnum.JPEG,
quality: 1,
});
const encodedBin: string = await this.encodeToStringBinary(
Buffer.from(outputBuffer),
);
this.redisCache.hset(fileId, encodedBin);
問題是 nodejs 上的庫,我做了一些更改並忘記了回滾。 我大失敗。 我刪除了所有編碼內容,現在將原始字節存儲在 redis 上,現在工作正常,因為它承諾二進制安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.