Compare commits
6 Commits
8e3a599294
...
v1.1.1
Author | SHA1 | Date | |
---|---|---|---|
aa61113357 | |||
e43b688d7f | |||
6bd2bbc572 | |||
6d29af7f72 | |||
f81a1c2fbd | |||
847a81534b |
@ -13,12 +13,12 @@ mvn package
|
|||||||
|
|
||||||
### run by jar
|
### run by jar
|
||||||
```shell
|
```shell
|
||||||
java -jar ./target/dist/lib/bioformats-imageinfo-1.0.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.0.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
|
||||||
@ -26,14 +26,17 @@ java -jar ./target/bioformats-imageinfo-1.0.0-jar-with-dependencies.jar "[path t
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import jp.riken.neurodata.tools.BioFormatsImageInfo;
|
import jp.riken.neurodata.tools.BioFormatsImageInfo;
|
||||||
import jp.riken.neurodata.tools.BioFormatsException;
|
import jp.riken.neurodata.tools.BioFormatsImageException;
|
||||||
|
import jp.riken.neurodata.tools.BioFormatsImageThumbnail;
|
||||||
|
|
||||||
String path = "[path to image file]";
|
String path = "[path to image file]";
|
||||||
String format = "";
|
String format = "";
|
||||||
Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
||||||
|
Map<String, Object> thumbnail = new LinkedHashMap<String, Object>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format = BioFormatsImageInfo.readMetadata(path, metadata);
|
format = BioFormatsImageInfo.readMetadata(path, metadata);
|
||||||
|
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
||||||
} catch (BioFormatsImageException e) {
|
} catch (BioFormatsImageException e) {
|
||||||
// error occurred
|
// error occurred
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
4
pom.xml
4
pom.xml
@ -5,13 +5,13 @@
|
|||||||
<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>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<bio-formats.version>6.10.0</bio-formats.version>
|
<bio-formats.version>6.10.1</bio-formats.version>
|
||||||
<bioformats-imageinfo.mainClass>jp.riken.neurodata.tools.BioFormatsImageInfo</bioformats-imageinfo.mainClass>
|
<bioformats-imageinfo.mainClass>jp.riken.neurodata.tools.BioFormatsImageInfo</bioformats-imageinfo.mainClass>
|
||||||
|
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
@ -271,8 +271,8 @@ public class BioFormatsImageInfo {
|
|||||||
results.put("status", status);
|
results.put("status", status);
|
||||||
results.put("message", message);
|
results.put("message", message);
|
||||||
results.put("format", format);
|
results.put("format", format);
|
||||||
results.put("thumbnail", thumbnail.isEmpty() ? null : thumbnail);
|
|
||||||
results.put("metadata", metadata.isEmpty() ? null : metadata);
|
results.put("metadata", metadata.isEmpty() ? null : metadata);
|
||||||
|
results.put("thumbnail", thumbnail.isEmpty() ? null : thumbnail);
|
||||||
System.out.println(getJsonString(results));
|
System.out.println(getJsonString(results));
|
||||||
|
|
||||||
if (!status) {
|
if (!status) {
|
||||||
|
@ -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;
|
||||||
@ -31,7 +30,7 @@ public class BioFormatsImageThumbnail {
|
|||||||
protected static final int BACKGROUND_COLOR = 0x000000;
|
protected static final int BACKGROUND_COLOR = 0x000000;
|
||||||
protected static final double MAXIMUM_SCALE = 2.0;
|
protected static final double MAXIMUM_SCALE = 2.0;
|
||||||
|
|
||||||
protected static BufferedImage resize(final BufferedImage image, final int width, final int height) {
|
public static BufferedImage resize(final BufferedImage image, final int width, final int height) {
|
||||||
final boolean hasAlpha = image.getColorModel().hasAlpha();
|
final boolean hasAlpha = image.getColorModel().hasAlpha();
|
||||||
final int imageWidth = image.getWidth();
|
final int imageWidth = image.getWidth();
|
||||||
final int imageHeight = image.getHeight();
|
final int imageHeight = image.getHeight();
|
||||||
@ -46,7 +45,7 @@ public class BioFormatsImageThumbnail {
|
|||||||
int resizeHeight = imageHeight;
|
int resizeHeight = imageHeight;
|
||||||
Image resizeImage = image;
|
Image resizeImage = image;
|
||||||
if (scale != 1.0) {
|
if (scale != 1.0) {
|
||||||
// resize image if dimension is larger than maximum dimention.
|
// resize image if dimension is different with requrested dimension.
|
||||||
resizeWidth = (int) (scale * (double) imageWidth);
|
resizeWidth = (int) (scale * (double) imageWidth);
|
||||||
resizeHeight = (int) (scale * (double) imageHeight);
|
resizeHeight = (int) (scale * (double) imageHeight);
|
||||||
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
||||||
@ -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;
|
||||||
@ -149,12 +153,21 @@ public class BioFormatsImageThumbnail {
|
|||||||
throw new BioFormatsImageException("unsupported file: " + path);
|
throw new BioFormatsImageException("unsupported file: " + path);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final int series = 0;
|
int series = 0;
|
||||||
|
final int seriesCount = reader.getSeriesCount();
|
||||||
|
for (int i = 0; i < seriesCount; i++) {
|
||||||
|
reader.setSeries(i);
|
||||||
|
if (reader.isThumbnailSeries()) {
|
||||||
|
series = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader.setSeries(series);
|
||||||
final int no = 0;
|
final int no = 0;
|
||||||
// System.out.println("series count: " + reader.getSeriesCount());
|
// System.out.println("series count: " + seriesCount);
|
||||||
// System.out.println("image count: " + reader.getImageCount());
|
// System.out.println("image count: " + reader.getImageCount());
|
||||||
// System.out.println("resolution: " + reader.getResolutionCount());
|
// System.out.println("resolution: " + reader.getResolutionCount());
|
||||||
reader.setSeries(series);
|
// System.out.println("thubmail series: " + series);
|
||||||
ret = reader.openThumbImage(no);
|
ret = reader.openThumbImage(no);
|
||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
for (Throwable t = e; t != null; t = t.getCause()) {
|
for (Throwable t = e; t != null; t = t.getCause()) {
|
||||||
@ -233,8 +246,8 @@ public class BioFormatsImageThumbnail {
|
|||||||
final String format = image.getColorModel().hasAlpha() ? "png" : "jpeg";
|
final String format = image.getColorModel().hasAlpha() ? "png" : "jpeg";
|
||||||
// image = resize(image, 160, 160);
|
// image = resize(image, 160, 160);
|
||||||
final String dataUri = getDataUri(image, "image/" + format);
|
final String dataUri = getDataUri(image, "image/" + format);
|
||||||
// System.out.println(dataUri);
|
System.out.println(dataUri);
|
||||||
saveFile(dataUri, "/var/tmp/thumbnail." + format);
|
// saveFile(dataUri, "/var/tmp/thumbnail." + format);
|
||||||
} catch (final BioFormatsImageException e) {
|
} catch (final BioFormatsImageException e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
Reference in New Issue
Block a user