java获取jpg格式的gps信息

博客介绍了使用Java获取JPG格式图片GPS信息的方法,先在pom里添加Maven,再编写测试类Test,调用pic方法后页面显示,控制台输出位置信息。还提到微信传图会使位置丢失,建议用USB传输图片。

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

1.java获取jpg格式的gps信息

1.先在pom里面添加maven

<!--图片gps-->
        <dependency>
            <groupId>com.drewnoakes</groupId>
            <artifactId>metadata-extractor</artifactId>
            <version>2.6.2</version>
        </dependency>

2.编写测试类Test

import java.io.File;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * java读取照片信息
 */
@Controller
public class Test{
    @ResponseBody
    @RequestMapping("/pic")
    public String nihao() throws Exception, Exception{
        File file = new File("E:/图片地址/2.jpg");
        String str=printImageTags(file);
        return str;
    }
    /**
     * 读取照片里面的信息
     */
    private static String printImageTags(File file) throws ImageProcessingException, Exception{
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        String str=new String();
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();  //标签名
                String desc = tag.getDescription(); //标签信息

                if (tagName.equals("Image Height")) {
                    str+="Image Height : "+desc+"<br/>";
                    System.out.println("图片高度: "+desc);
                } else if (tagName.equals("Image Width")) {
                    str+="Image Width : "+desc+"<br/>";
                    System.out.println("图片宽度: "+desc);
                } else if (tagName.equals("Date/Time Original")) {
                    System.out.println("拍摄时间: "+desc);
                }else if (tagName.equals("GPS Latitude")) {
                    str+="Latitude : "+pointToLatlong(desc)+"&nbsp&nbsp&nbsp&nbsp"+desc+"<br/>";
                    System.err.println("纬度 : "+desc);
//                	System.err.println("纬度(度分秒格式) : "+pointToLatlong(desc));
                } else if (tagName.equals("GPS Longitude")) {
                    str+="Longitude : "+pointToLatlong(desc)+"&nbsp&nbsp&nbsp&nbsp"+desc+"<br/>";
                    System.err.println("经度: "+desc);
//                	System.err.println("经度(度分秒格式): "+pointToLatlong(desc));
                }
            }
        }
        return str;
    }
    /**
     * 经纬度格式  转换为  度分秒格式 ,如果需要的话可以调用该方法进行转换
     * @param point 坐标点
     * @return
     */
    public static 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();
    }
}

然后调用pic方法,页面显示
在这里插入图片描述
控制台也会输出位置信息。
对了,好像微信传图片会让位置丢失,即使是原图也一样。最好用usb来传输图片哦!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值