首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从图像字节中检索创建日期

从图像字节中检索创建日期
EN

Stack Overflow用户
提问于 2016-04-18 11:16:14
回答 1查看 3.3K关注 0票数 2

我将图像作为Base64编码的字符串存储在数据库中。我想读取这个字符串,把它解码成一个字节数组,然后显示它。到目前一切尚好。

现在,我还想显示有关此图像的其他信息。我真的很在乎这张照片是什么时候拍的。

我尝试过几种方法。当文件系统中存在图像时,没有问题。但是,当我读取编码的字符串,将其解码为字节时,就会出现一个问题。

我使用了metadata-extractor库。

代码语言:javascript
复制
    <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:流结束之前,文件的神奇数字可以确定。在com.drew.imaging.FileTypeDetector.detectFileType(FileTypeDetector.java:97)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-18 15:12:01

经过研究和调试,我发现了这个。BufferedInputStream是必需的!

代码语言:javascript
复制
        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);

所以起作用了!

有一点,并不是所有的图像都包含元数据(我需要日期-时间)。以下是两个元数据示例。

代码语言:javascript
复制
[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
代码语言:javascript
复制
[JPEG] - Compression Type = Baseline
[JPEG] - Data Precision = 8 bits
[JPEG] - Image Height = 3104 pixels
[JPEG] - Image Width = 4192 pixels
[JPEG] - Number of Components = 3
[JPEG] - Component 1 = Y component: Quantization table 0, Sampling factors 2 horiz/1 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
[Exif IFD0] - Software = Flyme
[Exif IFD0] - Orientation = Top, left side (Horizontal / normal)
[Exif IFD0] - Unknown tag (0x0224) = 1
[Exif IFD0] - Unknown tag (0x0225) = 
[Exif IFD0] - Date/Time = 2016:04:13 10:20:08
[Exif IFD0] - Model = m1 note                        
[Exif IFD0] - Unknown tag (0x0222) = 0
[Exif IFD0] - Unknown tag (0x0223) = 0
[Exif IFD0] - Unknown tag (0x0220) = 0
[Exif IFD0] - Unknown tag (0x0221) = 0
[Exif IFD0] - Y Resolution = 72 dots per inch
[Exif IFD0] - X Resolution = 72 dots per inch
[Exif IFD0] - YCbCr Positioning = Datum point
[Exif IFD0] - Resolution Unit = Inch
[Exif IFD0] - Image Description =                                
[Exif IFD0] - Make = Meizu                          
[GPS] - GPS Img Direction = 49 degrees
[GPS] - GPS Img Direction Ref = Magnetic direction
[Exif SubIFD] - Date/Time Digitized = 2016:04:13 10:20:08
[Exif SubIFD] - Color Space = sRGB
[Exif SubIFD] - Date/Time Original = 2016:04:13 10:20:08
[Exif SubIFD] - FlashPix Version = 1.00
[Exif SubIFD] - Metering Mode = Center weighted average
[Exif SubIFD] - Exposure Bias Value = 0 EV
[Exif SubIFD] - Exif Image Height = 3104 pixels
[Exif SubIFD] - Exif Version = 2.20
[Exif SubIFD] - Exif Image Width = 4192 pixels
[Exif SubIFD] - Focal Length = 3.8 mm
[Exif SubIFD] - Digital Zoom Ratio = 1
[Exif SubIFD] - White Balance = (Other)
[Exif SubIFD] - Scene Capture Type = Standard
[Exif SubIFD] - Flash = Flash did not fire
[Exif SubIFD] - White Balance Mode = Auto white balance
[Exif SubIFD] - Exposure Mode = Auto exposure
[Exif SubIFD] - Exposure Time = 79999/1000000 sec
[Exif SubIFD] - ISO Speed Ratings = 810
[Exif SubIFD] - Components Configuration = YCbCr
[Exif SubIFD] - F-Number = f/2.2
[Exif SubIFD] - Exposure Program = Unknown (0)
[Interoperability] - Interoperability Index = Recommended Exif Interoperability Rules (ExifR98)
[Interoperability] - Interoperability Version = 1.00
[Exif Thumbnail] - Orientation = Top, left side (Horizontal / normal)
[Exif Thumbnail] - Compression = JPEG (old-style)
[Exif Thumbnail] - Thumbnail Offset = 962 bytes
[Exif Thumbnail] - YCbCr Positioning = Datum point
[Exif Thumbnail] - Thumbnail Length = 0 bytes
[Exif Thumbnail] - Y Resolution = 72 dots per inch
[Exif Thumbnail] - Resolution Unit = Inch
[Exif Thumbnail] - X Resolution = 72 dots per inch
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36692414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档