通过 Java 技术手段,获取女朋友定位地址...
作者丨 肥仔哥哥1930
依赖导入
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.16.0</version></dependency>
准备工作
示例demo
package com.easylinkin.bm.extractor;
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 com.easylinkin.bm.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
/**
* @author zhengwen
**/
@Slf4j
public class ImgTestCode {
public static void main(String[] args) throws Exception {
File file = new File('C:\\Users\\zhengwen\\Desktop\\test\\IMG_20210820_093958.jpg');
readImageInfo(file);
}
/**
* 提取照片里面的信息
*
* @param file 照片文件
* @throws ImageProcessingException
* @throws Exception
*/
private static void readImageInfo(File file) throws ImageProcessingException, Exception {
Metadata metadata = ImageMetadataReader.readMetadata(file);
System.out.println('---打印全部详情---');
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.format('[%s] - %s = %s\n',
directory.getName(), tag.getTagName(), tag.getDescription());
}
if (directory.hasErrors()) {
for (String error : directory.getErrors()) {
System.err.format('ERROR: %s', error);
}
}
}
System.out.println('--打印常用信息---');
Double lat = null;
Double lng = null;
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
String tagName = tag.getTagName(); //标签名
String desc = tag.getDescription(); //标签信息
if (tagName.equals('Image Height')) {
System.err.println('图片高度: ' + desc);
} else if (tagName.equals('Image Width')) {
System.err.println('图片宽度: ' + desc);
} else if (tagName.equals('Date/Time Original')) {
System.err.println('拍摄时间: ' + desc);
} else if (tagName.equals('GPS Latitude')) {
System.err.println('纬度 : ' + desc);
System.err.println('纬度(度分秒格式) : ' + pointToLatlong(desc));
lat = latLng2Decimal(desc);
} else if (tagName.equals('GPS Longitude')) {
System.err.println('经度: ' + desc);
System.err.println('经度(度分秒格式): ' + pointToLatlong(desc));
lng = latLng2Decimal(desc);
}
}
}
System.err.println('--经纬度转地址--');
//经纬度转地主使用百度api
convertGpsToLoaction(lat, lng);
}
/**
* 经纬度格式 转换为 度分秒格式 ,如果需要的话可以调用该方法进行转换
*
* @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 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;
}
/**
* api_key:注册的百度api的key
* coords:经纬度坐标
* http://api.map.baidu.com/reverse_geocoding/v3/?ak='+api_key+'&output=json&coordtype=wgs84ll&location='+coords
* <p>
* 经纬度转地址信息
*
* @param gps_latitude 维度
* @param gps_longitude 精度
*/
private static void convertGpsToLoaction(double gps_latitude, double gps_longitude) throws IOException {
String apiKey = 'YNxcSCAphFvuPD4LwcgWXwC3SEZZc7Ra';
String res = '';
String url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=' + apiKey + '&output=json&coordtype=wgs84ll&location=' + (gps_latitude + ',' + gps_longitude);
System.err.println('【url】' + url);
res = HttpUtils.httpGet(url);
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.err.println('拍摄地点:' + address.get('country') + ' ' + address.get('province') + ' ' + address.get('city') + ' ' + address.get('district') + ' '
+ address.get('street') + ' ' + result.get('formatted_address') + ' ' + result.get('business'));
}
}
}
}
com.easylinkin.bm.extractor.ImgTestCode
---打印全部详情---
[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
[Exif IFD0] - Date/Time = 2021:08:20 09:39:58
[Exif IFD0] - Model = YOTA Y3
[Exif IFD0] - YCbCr Positioning = Center of pixel array
[Exif IFD0] - Resolution Unit = Inch
[Exif IFD0] - Y Resolution = 72 dots per inch
[Exif IFD0] - X Resolution = 72 dots per inch
[Exif IFD0] - Make = YOTA
[GPS] - GPS Date Stamp = 2021:08:20
[GPS] - GPS Altitude Ref = Below sea level
[GPS] - GPS Longitude Ref = E
[GPS] - GPS Longitude = 114° 24' 9.61'
[GPS] - GPS Processing Method = ASCII
[GPS] - GPS Latitude Ref = N
[GPS] - GPS Time-Stamp = 01:39:46.000 UTC
[GPS] - GPS Altitude = 21 metres
[GPS] - GPS Latitude = 30° 28' 40.67'
[Exif SubIFD] - Color Space = sRGB
[Exif SubIFD] - F-Number = f/1.9
[Exif SubIFD] - Date/Time Digitized = 2021:08:20 09:39:58
[Exif SubIFD] - Focal Length = 3.9 mm
[Exif SubIFD] - Aperture Value = f/1.9
[Exif SubIFD] - Exposure Mode = Auto exposure
[Exif SubIFD] - Sub-Sec Time Digitized = 819350
[Exif SubIFD] - Exif Image Height = 4032 pixels
[Exif SubIFD] - Focal Length 35 = 23 mm
[Exif SubIFD] - Scene Capture Type = Standard
[Exif SubIFD] - Sub-Sec Time Original = 819350
[Exif SubIFD] - Exposure Program = Unknown (0)
[Exif SubIFD] - White Balance Mode = Auto white balance
[Exif SubIFD] - Exif Image Width = 3024 pixels
[Exif SubIFD] - Sub-Sec Time = 819350
[Exif SubIFD] - Shutter Speed Value = 1/1022 sec
[Exif SubIFD] - Metering Mode = Center weighted average
[Exif SubIFD] - Date/Time Original = 2021:08:20 09:39:58
[Exif SubIFD] - Components Configuration = YCbCr
[Exif SubIFD] - Exif Version = 2.20
[Exif SubIFD] - Flash = Flash did not fire
[Exif SubIFD] - Brightness Value = 0.0
[Exif SubIFD] - ISO Speed Ratings = 103
[Exif SubIFD] - Sensing Method = One-chip color area sensor
[Exif SubIFD] - FlashPix Version = 1.00
[Exif SubIFD] - Exposure Time = 1/1023 sec
[Interoperability] - Interoperability Index = Recommended Exif Interoperability Rules (ExifR98)
[Interoperability] - Interoperability Version = 1.00
[Exif Thumbnail] - Y Resolution = 72 dots per inch
[Exif Thumbnail] - Thumbnail Length = 21538 bytes
[Exif Thumbnail] - Thumbnail Offset = 959 bytes
[Exif Thumbnail] - Compression = JPEG (old-style)
[Exif Thumbnail] - Resolution Unit = Inch
[Exif Thumbnail] - X Resolution = 72 dots per inch
[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 = IMG_20210820_093958.jpg
[File] - File Size = 5215044 bytes
[File] - File Modified Date = 星期五 八月 20 09:39:59 +08:00 2021
--打印常用信息---
初始化HttpClientTest~~~开始
图片高度: 4032 pixels
图片宽度: 3024 pixels
经度: 114° 24' 9.61'
经度(度分秒格式): 114.40266944444446
纬度 : 30° 28' 40.67'
纬度(度分秒格式) : 30.477963888888887
拍摄时间: 2021:08:20 09:39:58
--经纬度转地址--
【url】http://api.map.baidu.com/reverse_geocoding/v3/?ak=YNxcSCAphFvuPD4LwcgWXwC3SEZZc7Ra&output=json&coordtype=wgs84ll&location=30.477963888888887,114.40266944444446
初始化HttpClientTest~~~结束
拍摄地点:中国 湖北省 武汉市 洪山区 软件园路 湖北省武汉市洪山区软件园路9 关山,光谷天地
总结与衍生想法
————————————————
版权声明:本文为CSDN博主「肥仔哥哥1930」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
赞 (0)