From aa61113357535a1ae5cfde3521a213ce06cb1093 Mon Sep 17 00:00:00 2001 From: Yoshihiro OKUMURA Date: Wed, 5 Oct 2022 16:00:29 +0900 Subject: [PATCH] use ImageWriter to export to png image. --- README.md | 4 ++-- pom.xml | 2 +- .../tools/BioFormatsImageThumbnail.java | 18 +++++++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0a09adb..85fc816 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,12 @@ mvn package ### run by jar ```shell -java -jar ./target/dist/lib/bioformats-imageinfo-1.1.0.jar "[path to image file]" +java -jar ./target/dist/lib/bioformats-imageinfo-1.1.1.jar "[path to image file]" ``` ### run by fat jar ```shell -java -jar ./target/bioformats-imageinfo-1.1.0-jar-with-dependencies.jar "[path to image file]" +java -jar ./target/bioformats-imageinfo-1.1.1-jar-with-dependencies.jar "[path to image file]" ``` ### library usage diff --git a/pom.xml b/pom.xml index b8c191d..43c77b4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 jp.riken.neurodata.tools.BioFormatsImageInfo bioformats-imageinfo - 1.1.0 + 1.1.1 bioformats-imageinfo https://neurodata.riken.jp diff --git a/src/main/java/jp/riken/neurodata/tools/BioFormatsImageThumbnail.java b/src/main/java/jp/riken/neurodata/tools/BioFormatsImageThumbnail.java index 3d78357..19a6504 100644 --- a/src/main/java/jp/riken/neurodata/tools/BioFormatsImageThumbnail.java +++ b/src/main/java/jp/riken/neurodata/tools/BioFormatsImageThumbnail.java @@ -4,7 +4,6 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; -import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; @@ -105,12 +104,14 @@ public class BioFormatsImageThumbnail { protected static byte[] getPngByteArray(final BufferedImage image) throws BioFormatsImageException { byte[] ret = null; - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - BufferedOutputStream bos = new BufferedOutputStream(baos);) { - image.flush(); - ImageIO.write(image, "png", bos); - bos.flush(); - bos.close(); + try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final ImageOutputStream ios = ImageIO.createImageOutputStream(baos);) { + final ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next(); + final ImageWriteParam param = writer.getDefaultWriteParam(); + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + writer.setOutput(ios); + writer.write(null, new IIOImage(image, null, null), param); + writer.dispose(); ret = baos.toByteArray(); } catch (final IOException e) { throw new BioFormatsImageException(e); @@ -131,6 +132,9 @@ public class BioFormatsImageThumbnail { default: throw new BioFormatsImageException("Unsupported image format: " + mimeType); } + if (bytes.length == 0) { + throw new BioFormatsImageException("Failed to convert to " + mimeType + " format"); + } final String dataUri = "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes); return dataUri;