“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”
Hi, I’m Lex the Lex who likes to bully Superman
Last time, we used Python to read the GPS information of the photo. We could get the date of the photo, the model of the phone, and so on
Precise location of the location. Java indicates denial ~~~
Today’s focus: Using Java to read the time when the photo was taken, GPS location, phone model and other information
Take you step by step to achieve the function, the end of the article has a complete source oh
Here’s the thing
Last time, python was used to locate the photo by GPS, as follows ↓ ↓ ↓
My girlfriend worked late at night to post a python video of my boyfriend discovering a shocking secret with 30 lines of code
Today is the day to show my true colors
Using the best language in the world – Java (probably getting blasted)
To implement the function once again
The general function is:
The photos are analyzed by the script and the location of the photos is located by GPS
In addition, you can also get information such as the shooting date and phone model.
The first effect
I found a photo posted by a beautiful woman from a wechat group
Java analyzes the photo as follows
The results are as follows:
{altitude =0 metres, phone =Xiaomi, latitude =24.41046111111111, model =MI MAX, longitude =103.41424722222223, shooting time =2018:12:01 16:37:32, Location = in honghe hani and yi autonomous prefecture in yunnan maitreya XXXX, XXX, XXX road hot spring hotel} latitude and longitude: 24.41046111111111, 103.41424722222223 shooting time: 2018: at 12:01 16:37:32 phone models: Xiaomi MI MAX location: Hot Spring XXX Hotel, No. XXX, XXX Road, Meili City, Honghe Hani and Yi Autonomous Prefecture, Yunnan Province, China {" status ": 0," result ": {" location" : {" LNG ": 103.42283328917563," lat ": 24.413805252378915}, "Business ":"","addressComponent": {" country ", "Chinese", "country_code" : 0, "country_code_iso" : "CHN", "country_code_iso2" : "CN", "Province", "yunnan province", "city" : "honghe hani and yi autonomous prefecture," "city_level" : 2, "district" : "maitreya city", "town" : ""," town_code ":" ", "adcode" : "532504", "street" : "hot springs road", "street_number" : ""," direction ":" ", "short" : ""}," cityCode ": 107}}Copy the code
Emmm, it’s hard to describe too much
Java implementation method
1. Introduce relevant JAR packages
Here we need to introduce two JAR packages to read the exIF information for the photo, which contains the complete information for the photo.
Resource download, put it at the end.
2. Read the original Exif information
First, using the JAR package tool,
Read out the original Exif information in the photo.
The complete code is as follows:
public static HashMap<String, Object> readPicInfo(String file_path) { HashMap<String, Object> map = new HashMap<String,Object>(); Tag tag = null; File jpegFile = new File(file_path); Metadata metadata; try { metadata = JpegMetadataReader.readMetadata(jpegFile); Iterator<Directory> it = metadata.getDirectories().iterator(); while (it.hasNext()) { Directory exif = it.next(); Iterator<Tag> tags = exif.getTags().iterator(); while (tags.hasNext()) { tag = (Tag) tags.next(); System.out.println(tag.getTagName()+"--"+tag.getDescription()); } } } catch (JpegProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return map; } public static void main(String[] args) {readPicInfo("C:\ Users\\pacer\ Desktop\ img\ others\ 10.jpg"); }Copy the code
Let’s see what information we can get:
Phone model taken, GPS precise location, shooting time, pixel, modification date
Even the elevation of the location was there.
3. GPS format conversion
The GPS information we read through exif is in a format similar to degrees, minutes, and seconds.
We need to compute the GPS information into decimal digits,
In this way, baidu Map API or other map API can be called to convert GPS information into geographic location information.
/*** * Longitude and latitude coordinate format conversion * @param Gps */ public double latitude_and_longitude_convert_to_decimal_system(String Gps) {String a = Gps. The split (" ° ") [0]. Replace (" ", ""); String b = Gps. The split (" ° ") [1]. The split (" '") [0]. Replace (" ", ""); String c = Gps. The split (" ° ") [1]. The split (" '") [1]. The replace (" ", ""). The replace (" \" ", ""); double gps_dou = Double.parseDouble(a)+Double.parseDouble(b)/60 + Double.parseDouble(c)/60/60; return gps_dou; }Copy the code
Using this function, we convert the latitude and longitude information to decimal numeric information.
4. Call map API to convert GPS coordinates to geographic location
What we get from exif raw information is a string of geographic coordinates.
If you want to convert to specific address information, you need to do this through the major map apis,
Users can register a Baidu Map API for free, and then call the interface provided by it.
You can convert the incoming GPS coordinates into address information. Of course, you can use mine here.
Call the interface method as follows:
//api_key: is your registered key value //coords: Is your latitude and longitude coordinates http://api.map.baidu.com/reverse_geocoding/v3/?ak= + api_key "+" & output = = json&coordtype wgs84ll & location = "+ coordsCopy the code
5. Complete code
Finally, put the code together.
Made a simple Java small project
With the Java environment, you can run ~~
Deep work and name