Java获取照片的Exif信息,并解析GPS
Java獲取照片的Exif信息,并解析GPS
- 1. 效果圖
- 2. 什么是Exif?
- 3. 源代碼
- 參考
?? 這篇博客將介紹什么EXIF,以及如何用Java語言讀取Exif信息,并轉(zhuǎn)換經(jīng)緯度為位置信息;??
1. 效果圖
Exif包含的信息很多,示例如下:
[JPEG] - Compression Type = Baseline
[JPEG] - Data Precision = 8 bits
[JPEG] - Image Height = 3840 pixels
[JPEG] - Image Width = 1744 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 = inch
[JFIF] - X Resolution = 96 dots
[JFIF] - Y Resolution = 96 dots
[JFIF] - Thumbnail Width Pixels = 0
[JFIF] - Thumbnail Height Pixels = 0
[Exif IFD0] - Image Width = 1744 pixels
[Exif IFD0] - Image Height = 3840 pixels
[Exif IFD0] - Bits Per Sample = 8 8 8 bits/component/pixel
[Exif IFD0] - Make = HUAWEI
[Exif IFD0] - Model = ELS-AN00
[Exif IFD0] - Orientation = Unknown (0)
[Exif IFD0] - X Resolution = 72 dots per inch
[Exif IFD0] - Y Resolution = 72 dots per inch
[Exif IFD0] - Resolution Unit = Inch
[Exif IFD0] - Software = ELS-AN00 11.0.0.176(C00E170R3P6)
[Exif IFD0] - Date/Time = 2021:05:21 07:18:02
[Exif IFD0] - YCbCr Positioning = Center of pixel array
[Exif IFD0] - Device Setting Description = 105 112 112 0
[Exif SubIFD] - Document Name =
[Exif SubIFD] - Exposure Time = 2049/500000 sec
[Exif SubIFD] - F-Number = f/1.8
[Exif SubIFD] - Exposure Program = Program normal
[Exif SubIFD] - ISO Speed Ratings = 50
[Exif SubIFD] - Exif Version = 2.10
[Exif SubIFD] - Date/Time Original = 2021:05:21 07:18:02
[Exif SubIFD] - Date/Time Digitized = 2021:05:21 07:18:02
[Exif SubIFD] - Components Configuration = YCbCr
[Exif SubIFD] - Compressed Bits Per Pixel = 0.95 bits/pixel
[Exif SubIFD] - Shutter Speed Value = 1/999963864 sec
[Exif SubIFD] - Aperture Value = f/1.8
[Exif SubIFD] - Brightness Value = 0.0
[Exif SubIFD] - Exposure Bias Value = 0 EV
[Exif SubIFD] - Max Aperture Value = f/1.8
[Exif SubIFD] - Metering Mode = Multi-segment
[Exif SubIFD] - White Balance = Daylight
[Exif SubIFD] - Flash = Flash did not fire
[Exif SubIFD] - Focal Length = 4.4 mm
[Exif SubIFD] - Makernote = 65 117 116 111 0
[Exif SubIFD] - Sub-Sec Time = 242921
[Exif SubIFD] - Sub-Sec Time Original = 242921
[Exif SubIFD] - Sub-Sec Time Digitized = 242921
[Exif SubIFD] - FlashPix Version = 1.00
[Exif SubIFD] - Color Space = sRGB
[Exif SubIFD] - Exif Image Width = 1744 pixels
[Exif SubIFD] - Exif Image Height = 3840 pixels
[Exif SubIFD] - Sensing Method = One-chip color area sensor
[Exif SubIFD] - File Source = Digital Still Camera (DSC)
[Exif SubIFD] - Scene Type = Directly photographed image
[Exif SubIFD] - Custom Rendered = Custom process
[Exif SubIFD] - Exposure Mode = Auto exposure
[Exif SubIFD] - White Balance Mode = Auto white balance
[Exif SubIFD] - Digital Zoom Ratio = 1
[Exif SubIFD] - Focal Length 35 = 18 mm
[Exif SubIFD] - Scene Capture Type = Standard
[Exif SubIFD] - Gain Control = None
[Exif SubIFD] - Contrast = None
[Exif SubIFD] - Saturation = None
[Exif SubIFD] - Sharpness = None
[Exif SubIFD] - Subject Distance Range = Unknown
[Interoperability] - Interoperability Index = Recommended Exif Interoperability Rules (ExifR98)
[Interoperability] - Interoperability Version = 1.00
[GPS] - GPS Version ID = 2.200
[GPS] - GPS Latitude Ref = N
[GPS] - GPS Latitude = 40° 4’ 22.74"
[GPS] - GPS Longitude Ref = E
[GPS] - GPS Longitude = 116° 20’ 37.01"
[GPS] - GPS Altitude Ref = Below sea level
[GPS] - GPS Altitude = 0 metres
[GPS] - GPS Time-Stamp = 23:18:01.000 UTC
[GPS] - GPS Processing Method = CELLID
[GPS] - GPS Date Stamp = 2021:05:20
[Exif Thumbnail] - Image Width = 256 pixels
[Exif Thumbnail] - Image Height = 528 pixels
[Exif Thumbnail] - Compression = JPEG (old-style)
[Exif Thumbnail] - Orientation = Unknown (0)
[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 = 1622 bytes
[Exif Thumbnail] - Thumbnail Length = 26099 bytes
[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 = mg.jpg
[File] - File Size = 1771566 bytes
[File] - File Modified Date = 星期五 七月 09 14:30:00 +08:00 2021
獲取了常見的屬性: 壓縮格式、圖像寬度、圖像高度、拍攝手機、型號、手機系統(tǒng)版本號、gps版本、經(jīng)度、緯度、高度、UTC時間戳、gps日期、iso速率、曝光時間、曝光模式、光圈值、焦距、圖像色彩空間、文件源、場景類型等 效果圖如下:
2. 什么是Exif?
EXIF是 Exchangeable Image File的縮寫,這是一種專門為數(shù)碼相機照片設定的格式。這種格式可以用來記錄數(shù)字照片的屬性信息,例如相機的品牌及型號、相片的拍攝時間、拍攝時所設置的光圈大小、快門速度、ISO、照片的寬度、高度等信息。
最簡單易用的EXIF信息處理的Java包是Drew Noakes寫的metadata-extractor,該項目最新的版本是2.16.0,支持EXIF 2.2版本。
并不是每個JPG圖像文件都包含有EXIF信息,可以在Windows資源管理器單擊選中圖片后,如果該圖片包含EXIF信息,則在窗口狀態(tài)欄會顯示出相機的型號,否則exif信息丟失。
- http://www.drewnoakes.com/code/exif/ 可下載該項目的最新版本包括其源碼
- github地址:https://github.com/drewnoakes/metadata-extractor/releases?after=2.6.0
3. 源代碼
獲取圖片的壓縮格式、圖像寬度、圖像高度、拍攝手機、型號、手機系統(tǒng)版本號、gps版本、經(jīng)度、緯度、高度、UTC時間戳、gps日期、iso速率、曝光時間、曝光模式、光圈值、焦距、圖像色彩空間、文件源、場景類型等
pom依賴:
<!--獲取相機exif信息-->
<dependency><groupId>com.drewnoakes</groupId><artifactId>metadata-extractor</artifactId><version>2.16.0</version>
</dependency>
package com.getGps;import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;/**************************************Class Name: gpsExifInfo*Description: <獲取相機經(jīng)緯度并且轉(zhuǎn)換實際位置>*@author: seminar*************************************/
public class GpsExifInfo {public static void main(String[] args) throws ImageProcessingException, IOException {String imgDir = "E:\\imgs";File[] files = new File(imgDir).listFiles();for (File file : files) {if (!file.getName().endsWith(".jpg")) {continue;}System.out.println("----------------------------------------\n" + file.getName());// 獲取照片信息Map exifMap = readPicExifInfo(file);// 打印照片信息printPicExifInfo(exifMap);}}/*** 獲取圖片文件的Exif信息** @param file* @return* @throws ImageProcessingException* @throws IOException*/private static Map<String, String> readPicExifInfo(File file) throws ImageProcessingException, IOException {Map<String, String> map = new HashMap<>();Metadata metadata = ImageMetadataReader.readMetadata(file);for (Directory directory : metadata.getDirectories()) {for (Tag tag : directory.getTags()) {// 輸出所有屬性System.out.format("[%s] - %s = %s\n", directory.getName(), tag.getTagName(), tag.getDescription());map.put(tag.getTagName(), tag.getDescription());}if (directory.hasErrors()) {for (String error : directory.getErrors()) {System.err.format("ERROR: %s", error);}}}return map;}/*** 打印照片Exif信息** @param map*/private static void printPicExifInfo(Map<String, String> map) {String[] strings = new String[]{"Compression", "Image Width", "Image Height", "Make", "Model", "Software","GPS Version ID", "GPS Latitude", "GPS Longitude", "GPS Altitude", "GPS Time-Stamp", "GPS Date Stamp","ISO Speed Ratings", "Exposure Time", "Exposure Mode", "F-Number", "Focal Length 35", "Color Space", "File Source", "Scene Type"};String[] names = new String[]{"壓縮格式", "圖像寬度", "圖像高度", "拍攝手機", "型號", "手機系統(tǒng)版本號","gps版本", "經(jīng)度", "緯度", "高度", "UTC時間戳", "gps日期","iso速率", "曝光時間", "曝光模式", "光圈值", "焦距", "圖像色彩空間", "文件源", "場景類型"};for (int i = 0; i < strings.length; i++) {if (map.containsKey(strings[i])) {if ("GPS Latitude".equals(strings[i]) || "GPS Longitude".endsWith(strings[i])) {System.out.println(names[i] + " " + strings[i] + " : " + map.get(strings[i]) + ", °轉(zhuǎn)dec: " + latLng2Decimal(map.get(strings[i])));} else {System.out.println(names[i] + " " + strings[i] + " : " + map.get(strings[i]));}}}// 經(jīng)緯度轉(zhuǎn)location地址信息if (map.containsKey("GPS Latitude") && map.containsKey("GPS Longitude")) {convertLatLng2Loaction(latLng2Decimal(map.get("GPS Latitude")), latLng2Decimal(map.get("GPS Longitude")));}}/*** api_key:注冊的百度api的key* coords:經(jīng)緯度坐標* http://api.map.baidu.com/reverse_geocoding/v3/?ak="+api_key+"&output=json&coordtype=wgs84ll&location="+coords* <p>* 經(jīng)緯度轉(zhuǎn)地址信息** @param gps_latitude* @param gps_longitude*/private static void convertLatLng2Loaction(double gps_latitude, double gps_longitude) throws IOException {String apiKey = "FxzbLsuDDL4CS2U0M4KezOZZbGUY9iWtRn";String res = "";String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + apiKey + "&output=json&coordtype=wgs84ll&location=" + (gps_latitude + "," + gps_longitude);System.out.println("【url】" + url);CloseableHttpClient client = HttpClients.custom().build();HttpGet httpPost = new HttpGet(url);httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);CloseableHttpResponse resp = client.execute(httpPost);if (resp != null) {if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity entity = resp.getEntity();res = IOUtils.readStreamAsString(entity.getContent());System.out.println("resJson: " + res);JSONObject object = JSONObject.parseObject(res);if (object.containsKey("result")) {JSONObject result = object.getJSONObject("result");if (result.containsKey("addressComponent")) {JSONObject address = object.getJSONObject("result").getJSONObject("addressComponent");System.out.println("拍攝地點:" + address.get("country") + " " + address.get("province") + " " + address.get("city") + " " + address.get("district") + " "+ address.get("street") + " " + result.get("formatted_address") + " " + result.get("business"));}}}}}/**** 經(jīng)緯度坐標格式轉(zhuǎn)換(* °轉(zhuǎn)十進制格式)* @param gps*/public static double latLng2Decimal(String gps) {String a = gps.split("°")[0].replace(" ", "");String b = gps.split("°")[1].split("'")[0].replace(" ", "");String c = gps.split("°")[1].split("'")[1].replace(" ", "").replace("\"", "");double gps_dou = Double.parseDouble(a) + Double.parseDouble(b) / 60 + Double.parseDouble(c) / 60 / 60;return gps_dou;}
}
參考
- 官方示例~獲取方式 https://drewnoakes.com/code/exif/
- https://blog.csdn.net/liuxiao723846/article/details/45969337
- https://blog.csdn.net/weixin_42350212/article/details/118467097
總結
以上是生活随笔為你收集整理的Java获取照片的Exif信息,并解析GPS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本人因乳腺内有纤维瘤,微创手术费用高承受
- 下一篇: 求一个男人好听的微信名字!