前言
可交换图像文件(Exchangeable Image File,Exif)信息图像在拍摄时保留的相关参数:比如图像信息(厂商,分辨率等),相机拍摄记录(ISO,白平衡,饱和度,锐度等),缩略图(缩略图宽度,高度等),GPS(拍摄时的经度,纬度,高度)等,按照图像文件标准存储在图像头文件。一般使用支持图像读取的软件即可查看部分参数,但是图像如果修改,Exif信息可能丢失。
问题
然而一些开发任务需要图片Exif信息某项信息进行后续处理,无法使用软件获取信息输入到程序中,对开发任务带来一些困难。
解决
引入drewnoakes库,该库可以分析照片、视频相关信息等强大功能。
有兴趣可关注微信公众号:Java烂笔头,回复:drewnoakes,查看完整源码及说明。
示例代码
Maven依赖
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.7.2</version> </dependency>
代码实现
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 java.io.File; public class ExifTest { public static void main(String[] args) throws Exception { File file = new File("D:\\Java学习\\二维码生成与解析\\mmexport1632468955620.jpg"); printImageTags(file); } /** * 读取照片相关信息 */ private static void printImageTags(File file) throws ImageProcessingException, Exception { Metadata metadata = ImageMetadataReader.readMetadata(file); String lat = ""; String log = ""; for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); //标签名 String desc = tag.getDescription(); //标签信息 System.out.println(tagName + " " + desc);//照片信息 } } } }
照片信息输出
(由于不同照片所包含信息不完全一样,部分相关信息没有)
Compression Type Baseline Data Precision 8 bits Image Height 1080 pixels Image Width 1440 pixels Number of Components 3 Component 1 Y component: Quantization table 0, Sampling factors 2 horiz/2 vert Component 2 Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert Component 3 Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert Version 1.1 Resolution Units none X Resolution 1 dot Y Resolution 1 dot Profile Size 536 Version 2.1.0 Class Display Device Color space RGB Profile Connection Space XYZ Profile Date/Time Wed Dec 31 08:00:00 CST 2 Signature acsp Rendering Intent Media-Relative Colorimetric XYZ values 0.9642029 1.0 0.8249054 Tag Count 9 Profile Description 1 enUS(sRGB ) Red Colorant (0.43606567, 0.2224884, 0.013916016) Green Colorant (0.3851471, 0.71687317, 0.097076416) Blue Colorant (0.1430664, 0.06060791, 0.71409607) Red TRC para(0x70617261): 40 bytes Green TRC para(0x70617261): 40 bytes Blue TRC para(0x70617261): 40 bytes Media White Point (0.9642029, 1.0, 0.8249054) Copyright 1 enUS(Google Inc. 2016)
应用场景
需求:根据照片(包含位置信息)查找该照片拍摄定位
思路:首先引入drewnoakes库读取照片所有信息,判断tag信息为经度、纬度,根据经纬度转换为具体坐标位置。
工具代码:
以下代码还引入alibaba.fasejson库对读取的Exif信息进一步分析
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; 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 java.io.File; import java.net.URL; public class EXIFUtil { public static void main(String[] args) throws Exception { File file = new File("D:\\Java学习\\二维码生成与解析\\mmexport1632468955620.jpg"); printImageTags(file); } /** * 读取照片里面的信息 */ private static void printImageTags(File file) throws ImageProcessingException, Exception { Metadata metadata = ImageMetadataReader.readMetadata(file); String lat = ""; String log = ""; for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); //标签名 String desc = tag.getDescription(); //标签信息 System.out.println(tagName + " " + desc);//照片信息 switch (tagName) { // 经度 case "GPS Longitude": lat = pointToLatlong(desc); break; // 纬度 case "GPS Latitude": log = pointToLatlong(desc); break; default: break; } } } String add = getAdd(log, lat); JSONObject jsonObject = JSON.parseObject(add); JSONArray jsonArray = jsonObject.getJSONArray("addrList"); JSONObject jso = jsonArray.getJSONObject(0); String allAdd = jso.getString("admName"); String arr[] = allAdd.split(","); String adds = jso.getString("name"); System.out.println("省:" + arr[0] + "\n市:" + arr[1] + "\n区:" + arr[2] + "\n" + adds); } /** * 经纬度格式 转换 * * @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(); } /** * @param log 经度 * * @param lat 纬度 * * @return */ public static String getAdd(String log, String lat) { //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项) String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010"; String res = ""; try { URL url = new URL(urlString); java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { res += line + "\n"; } in.close(); } catch (Exception e) { System.out.println("error in wapaction,and e is " + e.getMessage()); } return res; } }