我将图像存储在数据库中作为Base64编码的字符串。我想读取这个字符串,将其解码为一个字节数组并显示它。到目前为止一切都很好。
现在我还想显示有关此图片的其他信息。拍摄/创建此图像时,我非常关心。
我尝试了几种方法。当文件系统中存在图像时,没有问题。但是,当我读取编码字符串,将其解码为字节时,会出现问题。
我使用元数据提取器库。
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.9.0</version>
</dependency>
public void processData(String data) {
if (data == null || data.isEmpty()) {
return;
}
byte[] base64Decoded = DatatypeConverter.parseBase64Binary(data);
read(base64Decoded );
//displayImage();
//displayImageInfo();
}
public void read(byte [] data) {
try {
InputStream inputStream = new ByteArrayInputStream(data);
BufferedImage image = ImageIO.read(inputStream);
retrieveImageInfo();
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
retrieveAdditiaonlInfo2(metadata);
} catch (Exception e) {
//throw new RuntimeException("Failed to read the image from bytes.", e);
}
}
private void retrieveImageInfo() {
imageWidth = (long) image.getWidth();
imageHeight = (long) image.getHeight();
imageSize = (long) data.length;
}
private void retrieveAdditiaonlInfo2(Metadata metadata) {
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.format("[%s] - %s = %s", directory.getName(), tag.getTagName(), tag.getDescription());
System.out.println();
}
if (directory.hasErrors()) {
for (String error : directory.getErrors()) {
System.err.format("ERROR: %s", error);
System.err.println();
}
}
}
}
这适用于从文件中读取的图像。但对于我们的数据库,它不起作用。它说,
引起:java.io.IOException:在文件的幻数可以确定之前,Stream结束。 在com.drew.imaging.FileTypeDetector.detectFileType(FileTypeDetector.java:97)
答案 0 :(得分:2)
经过研究和调试后,我发现了这一点。 BufferedInputStream 是必要的!
[[1]]
[1] -0.2053160 0.4000491 2.3253354 1.3603900
[[1]]
[1] 0.07303119 0.39996344 1.11404626 0.93861174
[[1]]
[1] -0.01384496 0.39998626 1.14267411 1.05170904
[[1]]
[1] -1.0078745 0.4004017 1.0993075 4.3916078
[[1]]
[1] 0.06992641 0.39997072 1.05224910 1.09107480
所以它有效!
有一件事,并非所有图像都包含元数据(我需要采用日期时间)。 以下是元数据的两个示例。
File imageFile = new File(filename);
Path path = imageFile.toPath();
byte[] data = Files.readAllBytes(path);
String str = DatatypeConverter.printBase64Binary(data);
//Store encoded data.
//Retrieve encoded string from the database.
byte [] data2 = DatatypeConverter.parseBase64Binary(str);
InputStream inputStream = new ByteArrayInputStream(data2);
BufferedInputStream bis = new BufferedInputStream(inputStream);
Metadata metadata = ImageMetadataReader.readMetadata(bis);
[JPEG] - Compression Type = Baseline
[JPEG] - Data Precision = 8 bits
[JPEG] - Image Height = 758 pixels
[JPEG] - Image Width = 1024 pixels
[JPEG] - Number of Components = 3
[JPEG] - Component 1 = Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
[JPEG] - Component 2 = Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JPEG] - Component 3 = Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JFIF] - Version = 1.1
[JFIF] - Resolution Units = none
[JFIF] - X Resolution = 1 dot
[JFIF] - Y Resolution = 1 dot
[JFIF] - Thumbnail Width Pixels = 0
[JFIF] - Thumbnail Height Pixels = 0