use ImageWriter to export to png image.

This commit is contained in:
Yoshihiro OKUMURA 2022-10-05 16:00:29 +09:00
parent e43b688d7f
commit aa61113357
3 changed files with 14 additions and 10 deletions

View File

@ -13,12 +13,12 @@ mvn package
### run by jar ### run by jar
```shell ```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 ### run by fat jar
```shell ```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 ### library usage

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>jp.riken.neurodata.tools.BioFormatsImageInfo</groupId> <groupId>jp.riken.neurodata.tools.BioFormatsImageInfo</groupId>
<artifactId>bioformats-imageinfo</artifactId> <artifactId>bioformats-imageinfo</artifactId>
<version>1.1.0</version> <version>1.1.1</version>
<name>bioformats-imageinfo</name> <name>bioformats-imageinfo</name>
<url>https://neurodata.riken.jp</url> <url>https://neurodata.riken.jp</url>

View File

@ -4,7 +4,6 @@ import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image; import java.awt.Image;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -105,12 +104,14 @@ public class BioFormatsImageThumbnail {
protected static byte[] getPngByteArray(final BufferedImage image) throws BioFormatsImageException { protected static byte[] getPngByteArray(final BufferedImage image) throws BioFormatsImageException {
byte[] ret = null; byte[] ret = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);) { final ImageOutputStream ios = ImageIO.createImageOutputStream(baos);) {
image.flush(); final ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
ImageIO.write(image, "png", bos); final ImageWriteParam param = writer.getDefaultWriteParam();
bos.flush(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
bos.close(); writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();
ret = baos.toByteArray(); ret = baos.toByteArray();
} catch (final IOException e) { } catch (final IOException e) {
throw new BioFormatsImageException(e); throw new BioFormatsImageException(e);
@ -131,6 +132,9 @@ public class BioFormatsImageThumbnail {
default: default:
throw new BioFormatsImageException("Unsupported image format: " + mimeType); 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); final String dataUri = "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
return dataUri; return dataUri;