search
HomeJavajavaTutorialJava reads Exif metadata information of images

package com.drew.metadata;
 
import java.io.File;
import java.io.IOException;
 
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
 
public class SampleUsage
{
   /**
    * 图片信息获取metadata元数据信息
    * @param fileName 需要解析的文件
    * @return
    */
    public ImgInfoBean parseImgInfo (String fileName)
    {
        File file = new File(fileName);
        ImgInfoBean imgInfoBean = null;
        try {
            Metadata metadata = ImageMetadataReader.readMetadata(file);
            imgInfoBean = printImageTags(file, metadata);
        } catch (ImageProcessingException e) {
            System.err.println("error 1a: " + e);
        } catch (IOException e) {
            System.err.println("error 1b: " + e);
        }
        return imgInfoBean;
    }
 
    /**
     * 读取metadata里面的信息
     * @param sourceFile 源文件
     * @param metadata metadata元数据信息
     * @return
     */
    private ImgInfoBean printImageTags(File sourceFile, Metadata metadata)
    {
        ImgInfoBean imgInfoBean = new ImgInfoBean ();
        imgInfoBean.setImgSize(sourceFile.getTotalSpace());
        imgInfoBean.setImgName(sourceFile.getName());
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();
                String desc = tag.getDescription();
                if (tagName.equals("Image Height")) {
                    //图片高度
                    imgInfoBean.setImgHeight(desc);
                } else if (tagName.equals("Image Width")) {
                    //图片宽度
                    imgInfoBean.setImgWidth(desc);
                } else if (tagName.equals("Date/Time Original")) {
                    //拍摄时间
                    imgInfoBean.setDateTime(desc);
                } else if (tagName.equals("GPS Altitude")) {
                    //海拔
                    imgInfoBean.setAltitude(desc);
                } else if (tagName.equals("GPS Latitude")) {
                    //纬度
                    imgInfoBean.setLatitude(pointToLatlong(desc));
                } else if (tagName.equals("GPS Longitude")) {
                    //经度
                    imgInfoBean.setLongitude(pointToLatlong(desc));
                }
            }
            for (String error : directory.getErrors()){
                System.err.println("ERROR: " + error);
            }
        }
        return imgInfoBean;
    }
 
    /**
     * 经纬度转换  度分秒转换
     * @param point 坐标点
     * @return
     */
    public String pointToLatlong (String point ) {
        Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim());
        Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1, point.indexOf("'")).trim());
        Double miao = Double.parseDouble(point.substring(point.indexOf("'")+1, point.indexOf("\"")).trim());
        Double duStr = du + fen / 60 + miao / 60 / 60 ;
        return duStr.toString();
    }
 
    public static void main(String[] args)
    {
        ImgInfoBean imgInfoBean = new SampleUsage().parseImgInfo("C:\\DSC_4564.JPG");
        System.out.println(imgInfoBean.toString());
    }
}

文件信息bean类

package com.drew.metadata;
 
public class ImgInfoBean {
 
    private String imgHeight ;//图片高度
    private String imgWidth ;//图片宽度
    private String dateTime ;//拍摄时间
    private String altitude ;//海拔
    private String latitude;//纬度
    private String longitude ;//经度
    private Long imgSize;    //文件大小
    private String imgName;  //文件名称
    public Long getImgSize() {
        return imgSize;
    }
    public void setImgSize(Long imgSize) {
        this.imgSize = imgSize;
    }
    public String getImgName() {
        return imgName;
    }
    public void setImgName(String imgName) {
        this.imgName = imgName;
    }
    public String getImgHeight() {
        return imgHeight;
    }
    public void setImgHeight(String imgHeight) {
        this.imgHeight = imgHeight;
    }
    public String getImgWidth() {
        return imgWidth;
    }
    public void setImgWidth(String imgWidth) {
        this.imgWidth = imgWidth;
    }
    public String getDateTime() {
        return dateTime;
    }
    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }
    public String getAltitude() {
        return altitude;
    }
    public void setAltitude(String altitude) {
        this.altitude = altitude;
    }
    public String getLatitude() {
        return latitude;
    }
    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
 
    public String toString (){
        return "[图片信息]文件名称:"+ this.imgName+"   文件大小:"+this.imgSize +"  高度:"+this.imgHeight+"  宽度:"+this.imgWidth+"  拍摄时间:"+this.dateTime+"  海拔:"+this.altitude+"   纬度:"+this.latitude+"  经度:"+this.longitude;
    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.