java中jpeg的地理定位
Posted
技术标签:
【中文标题】java中jpeg的地理定位【英文标题】:Geolocation from jpeg in java 【发布时间】:2012-07-11 07:25:39 【问题描述】:我的朋友正在使用 Struts (Java),我们正在尝试查找 JPEG 图片的 GeoLocation。
我不知道在哪里阅读或如何实现它的 java 代码。
我想获得一张 jpeg 格式的图片,并将其显示在网站上的谷歌地图上。
你如何将这些信息从 Exif 标签传递到谷歌地图,以便我可以在地图上看到位置
【问题讨论】:
地理位置位于 EXIF 标签中。尝试自己获取它,google 给了我很多如何从 JPEG 中提取 EXIF 信息的结果。如果您有更具体的问题,请再次提问或完善您的问题。 【参考方案1】:geolocation 部分与 Struts 无关。 Struts 只涉及数据的呈现。
图像的地理位置有时存储在 JPEG 文件的元数据中。您可以使用 this one 之类的库从 JPEG 图像中提取元数据。
请注意,许多相机不存储地理位置数据。
【讨论】:
哦,他们没有,那我的教授在说什么。我的教授要求我们做一个项目,现在我们遇到了一些我们还不知道的事情,让我读一下你的图书馆页面,看看这对我来说是否有意义谢谢 你如何将这些信息从 Exif 标签传递到谷歌地图你知道吗? 你需要自己研究一下。如果您到处寻找,但仍然卡住,您可以在此处打开另一个问题。【参考方案2】:您的问题分为两部分
-
从 Jpeg 图像中提取 gps 信息
如何将提取的信息提供给谷歌地图。
这里详细讨论第一部分
阅读图像元现在通过apache commons-imaging library 得到了极大的简化和精简
/**
* Reference : https://github.com/apache/commons-imaging/blob/master/src/test/java/org/apache/commons/imaging/examples/MetadataExample.java
*/
public static void readImageMeta(final File imgFile) throws ImageReadException, IOException
/** get all metadata stored in EXIF format (ie. from JPEG or TIFF). **/
final ImageMetadata metadata = Imaging.getMetadata(imgFile);
System.out.println(metadata);
System.out.println("--------------------------------------------------------------------------------");
/** Get specific meta data information by drilling down the meta **/
if (metadata instanceof JpegImageMetadata)
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE);
printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
// simple interface to GPS data
final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
if (null != exifMetadata)
final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
if (null != gpsInfo)
final String gpsDescription = gpsInfo.toString();
final double longitude = gpsInfo.getLongitudeAsDegreesEast();
final double latitude = gpsInfo.getLatitudeAsDegreesNorth();
System.out.println(" " + "GPS Description: " + gpsDescription);
System.out.println(" " + "GPS Longitude (Degrees East): " + longitude);
System.out.println(" " + "GPS Latitude (Degrees North): " + latitude);
// more specific example of how to manually access GPS values
final TiffField gpsLatitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
final TiffField gpsLatitudeField = jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE);
final TiffField gpsLongitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
final TiffField gpsLongitudeField = jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null && gpsLongitudeField != null)
// all of these values are strings.
final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
final RationalNumber[] gpsLatitude = (RationalNumber[]) (gpsLatitudeField.getValue());
final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
final RationalNumber[] gpsLongitude = (RationalNumber[]) gpsLongitudeField.getValue();
final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
// This will format the gps info like so:
//
// gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
// gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
System.out.println(" " + "GPS Latitude: " + gpsLatitudeDegrees.toDisplayString() + " degrees, " + gpsLatitudeMinutes.toDisplayString() + " minutes, " + gpsLatitudeSeconds.toDisplayString() + " seconds " + gpsLatitudeRef);
System.out.println(" " + "GPS Longitude: " + gpsLongitudeDegrees.toDisplayString() + " degrees, " + gpsLongitudeMinutes.toDisplayString() + " minutes, " + gpsLongitudeSeconds.toDisplayString() + " seconds " + gpsLongitudeRef);
private static void printTagValue(final JpegImageMetadata jpegMetadata, TagInfo tagInfo)
final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
if (field == null)
System.out.println(tagInfo.name + ": " + "Not Found.");
else
System.out.println(tagInfo.name + ": " + field.getValueDescription());
public static void main(String[] args) throws IOException, ImageReadException
File sourceFile = new File("/Users/vivek/myimage.jpg");
readImageMeta(sourceFile);
上面的代码会给出这个输出
Exif metadata:
Root:
ImageWidth: 3024
ImageLength: 4032
BitsPerSample: 8, 8, 8
PhotometricInterpretation: 2
Make: 'Apple'
Model: 'iPhone 6s'
Orientation: 1
SamplesPerPixel: 3
XResolution: 72
YResolution: 72
ResolutionUnit: 2
Software: 'Adobe Photoshop CC 2015 (Macintosh)'
DateTime: '2016:05:17 16:55:55'
YCbCrPositioning: 1
ExifOffset: 300
GPSInfo: 868
Exif:
ExposureTime: 1/100 (0.01)
FNumber: 11/5 (2.2)
ExposureProgram: 2
PhotographicSensitivity: 40
ExifVersion: 48, 50, 50, 49
DateTimeOriginal: '2016:05:08 18:03:57'
DateTimeDigitized: '2016:05:08 18:03:57'
ComponentsConfiguration: 1, 2, 3, 0
ShutterSpeedValue: 35113/5284 (6.645)
ApertureValue: 7983/3509 (2.275)
BrightnessValue: 13026/2395 (5.439)
ExposureCompensation: 0
MeteringMode: 5
Flash: 16
FocalLength: 83/20 (4.15)
SubjectArea: 618, 1555, 310, 311
SubSecTimeOriginal: '523'
SubSecTimeDigitized: '523'
FlashpixVersion: 48, 49, 48, 48
ColorSpace: 1
ExifImageWidth: 3024
ExifImageLength: 4032
SensingMethod: 2
SceneType: 1
CustomRendered: 4
ExposureMode: 0
WhiteBalance: 0
FocalLengthIn35mmFormat: 29
SceneCaptureType: 0
LensSpecification: 83/20 (4.15), 83/20 (4.15), 11/5 (2.2), 11/5 (2.2)
LensMake: 'Apple'
LensModel: 'iPhone 6s back camera 4.15mm f/2.2'
Gps:
GPSLatitudeRef: 'N'
GPSLatitude: 13, 21, 3305/100 (33.05)
GPSLongitudeRef: 'E'
GPSLongitude: 75, 33, 2034/100 (20.34)
GPSAltitudeRef: 0
GPSAltitude: 83651/95 (880.537)
GPSTimeStamp: 12, 33, 57
GPSSpeedRef: 'K'
GPSSpeed: 21/100 (0.21)
GPSImgDirectionRef: 'T'
GPSImgDirection: 1654/31 (53.355)
GPSDestBearingRef: 'T'
GPSDestBearing: 1654/31 (53.355)
GPSDateStamp: '2016:05:08'
Unknown Tag (0x1f): 5
Sub: (jpegImageData)
Compression: 6
XResolution: 72
YResolution: 72
ResolutionUnit: 2
JpgFromRawStart: 1274
JpgFromRawLength: 8211
Photoshop (IPTC) metadata:
Date Created: 20160508
Time Created: 180357+0000
--------------------------------------------------------------------------------
GPSLatitudeRef: 'N'
GPSLatitude: 13, 21, 3305/100 (33.05)
GPSLongitudeRef: 'E'
GPSLongitude: 75, 33, 2034/100 (20.34)
GPS Description: [GPS. Latitude: 13 degrees, 21 minutes, 33.05 seconds N, Longitude: 75 degrees, 33 minutes, 20.34 seconds E]
GPS Longitude (Degrees East): 75.55565
GPS Latitude (Degrees North): 13.359180555555556
GPS Latitude: 13 degrees, 21 minutes, 33.05 seconds N
GPS Longitude: 75 degrees, 33 minutes, 20.34 seconds E
更多参考库文档here Maven 回购链接here. 注意:目前它是 alpha 版本,但您可以信任 apache:D。
干杯!
【讨论】:
以上是关于java中jpeg的地理定位的主要内容,如果未能解决你的问题,请参考以下文章
来自邮政编码的 Java Swing GoogleMaps 地理定位