com.drew.metadata 获取图片信息

本文介绍了一种使用Java程序读取JPEG图片元数据的方法。通过使用metadata-extractor库,可以轻松获取图片的各项属性,包括尺寸、分辨率、拍摄设备型号及日期等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<dependency>
    <groupId>com.drewnoakes</groupId>
    <artifactId>metadata-extractor</artifactId>
</dependency>
public static void main(String[] args) throws Exception {
        File mFile = new File("/Users/mac/Desktop/WechatIMG185.jpeg");
        Metadata metadata = ImageMetadataReader.readMetadata(mFile);
		// 获取所有项的信息
        Iterable<Directory> directoryIterable = metadata.getDirectories();
        if (directoryIterable != null) {
            Iterator<Directory> directoryIterator = directoryIterable.iterator();
            while (directoryIterator.hasNext()) {
                Directory directory = directoryIterator.next();
                Collection<Tag> tags = directory.getTags();
                for (Tag tag : tags) {
//                System.out.println(tag.getDescription());
                    System.out.println(tag);
                }
            }


            // 取具体某一项的信息
            Iterable<ExifSubIFDDirectory> exifSubIFDDirectoryIterable = metadata.getDirectoriesOfType(ExifSubIFDDirectory.class);
            if (exifSubIFDDirectoryIterable != null) {
                Iterator<ExifSubIFDDirectory> subIFDDirectoryIterator = exifSubIFDDirectoryIterable.iterator();
                if (subIFDDirectoryIterator != null && subIFDDirectoryIterator.hasNext()) {
                    ExifSubIFDDirectory dr = subIFDDirectoryIterator.next();
                    if (dr.containsTag(ExifSubIFDDirectory.TAG_USER_COMMENT)) {
                        System.out.println(dr.getDescription(ExifSubIFDDirectory.TAG_USER_COMMENT));
                    }
                }
            }
}

输出如下:

[JPEG] Compression Type - Baseline
[JPEG] Data Precision - 8 bits
[JPEG] Image Height - 4032 pixels
[JPEG] Image Width - 3024 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 - 72 dots
[JFIF] Y Resolution - 72 dots
[JFIF] Thumbnail Width Pixels - 0
[JFIF] Thumbnail Height Pixels - 0
[Exif IFD0] Make - Yiruikecorp
[Exif IFD0] Model - foodie
[Exif IFD0] Orientation - Top, left side (Horizontal / normal)
[Exif IFD0] X Resolution - 72 dots per inch
[Exif IFD0] Y Resolution - 72 dots per inch
[Exif IFD0] Resolution Unit - Inch
[Exif IFD0] YCbCr Positioning - Center of pixel array
[Exif SubIFD] Exif Version - 2.21
[Exif SubIFD] Date/Time Digitized - 2019:07:04 15:49:15
[Exif SubIFD] Components Configuration - YCbCr
[Exif SubIFD] User Comment - plain_02,-1
[Exif SubIFD] FlashPix Version - 1.00
[Exif SubIFD] Color Space - sRGB
[Exif SubIFD] Exif Image Width - 3024 pixels
[Exif SubIFD] Exif Image Height - 4032 pixels
[Exif SubIFD] Scene Capture Type - Standard
[Exif Thumbnail] Compression - JPEG (old-style)
[Exif Thumbnail] X Resolution - 72 dots per inch
[Exif Thumbnail] Y Resolution - 72 dots per inch
[Exif Thumbnail] Resolution Unit - Inch
[Exif Thumbnail] Thumbnail Offset - 394 bytes
[Exif Thumbnail] Thumbnail Length - 10125 bytes
[XMP] XMP Value Count - 1
[Photoshop] Caption Digest - 212 29 140 217 143 0 178 4 233 128 9 152 236 248 66 126
[Huffman] Number of Tables - 4 Huffman tables
[File Type] Detected File Type Name - JPEG
[File Type] Detected File Type Long Name - Joint Photographic Experts Group
[File Type] Detected MIME Type - image/jpeg
[File Type] Expected File Name Extension - jpg
[File] File Name - WechatIMG185.jpeg
[File] File Size - 2911336 bytes
[File] File Modified Date - 星期四 七月 04 15:50:35 +08:00 2019
plain_02,-1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值