Java reads the exif information in JPEG.

Source: Internet
Author: User

The camera will record some extended information beyond the basic information of the image, such as the longitude and latitude information, storage and exif. Generally, the java program cannot read the extended information such as gps. If you want to parse the information inside, you need to download a jar package, metadata-extractor-2.6.4.jar (: http://code.google.com/p/metadata-extractor/), which provides the ability to get extension information. The specific implementation code is as follows:

Package com. drew. metadata; import java. io. file; import java. io. IOException; import com. drew. imaging. imageMetadataReader; import com. drew. imaging. imageProcessingException; public class SampleUsage {/*** obtain metadata information for image information * @ param fileName file to be parsed * @ return */public ImgInfoBean parseImgInfo (String fileName) {File file = new File (fileName); ImgInfoBean imgInfoBean = null; try {Metadata metadata = ImageMetadataReader. readMetadata (file); imgInfoBean = printImageTags (file, metadata);} catch (ImageProcessingException e) {System. err. println ("error 1a:" + e);} catch (IOException e) {System. err. println ("error 1b:" + e);} return imgInfoBean ;} /*** read the information in metadata * @ param sourceFile source File * @ param metadata information * @ return */private ImgInfoBean printImageTags (File sourceFile, Metadata metadata) {ImgInfoBean imgInfoBean = new ImgInfoBean (); imgInfoBean. setImgSize (sourceFile. getTotalSpace (); imgInfoBean. setImgName (sourceFile. getName (); for (Directory directory: metadata. getDirectories () {for (Tag tag: directory. getTags () {String tagName = tag. getTagName (); String desc = tag. getDescription (); if (tagName. equals ("Image Height") {// Image Height imgInfoBean. setImgHeight (desc);} else if (tagName. equals ("Image Width") {// Image Width imgInfoBean. setImgWidth (desc);} else if (tagName. equals ("Date/Time Original") {// shooting Time imgInfoBean. setDateTime (desc);} else if (tagName. equals ("GPS Altitude") {// imgInfoBean. setAltitude (desc);} else if (tagName. equals ("GPS Latitude") {// Latitude imgInfoBean. setLatitude (pointToLatlong (desc);} else if (tagName. equals ("GPS longpolling") {// Longitude imgInfoBean. setlongpolling (pointToLatlong (desc) ;}}for (String error: directory. getErrors () {System. err. println ("ERROR:" + error) ;}return imgInfoBean ;} /*** second-to-Second Conversion of latitude and longitude conversion degree * @ param point coordinate point * @ return */public 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 ();} public static void main (String [] args) {ImgInfoBean imgInfoBean = new SampleUsage (). parseImgInfo ("C: \ DSC_4564.JPG"); System. out. println (imgInfoBean. toString ());}}

 

File Information bean class.
Package com. drew. metadata; public class ImgInfoBean {private String imgHeight; // picture height private String imgWidth; // picture width private String dateTime; // shooting time private String altitude; // elevation private String latitude; // latitude private String longpolling; // longitude private Long imgSize; // file size private String imgName; // file name public Long getImgSize () {return imgSize;} public void setImgSize (Long imgSize) {this. imgSize = imgSize;} public String getImgName () {return imgName;} public void setImgName (String imgName) {this. imgName = imgName;} public String getImgHeight () {return imgHeight;} public void setImgHeight (String imgHeight) {this. imgHeight = imgHeight;} public String getImgWidth () {return imgWidth;} public void setImgWidth (String imgWidth) {this. imgWidth = imgWidth;} public String getDateTime () {return dateTime;} public void setDateTime (String dateTime) {this. dateTime = dateTime;} public String getAltitude () {return altitude;} public void setAltitude (String altitude) {this. altitude = altitude;} public String getLatitude () {return latitude;} public void setLatitude (String latitude) {this. latitude = latitude;} public String getlongpolling () {return longpolling;} public void setlongpolling (String longpolling) {this. longpolling = longpolling;} public String toString () {return "[Image Information] File Name:" + this. imgName + "file size:" + this. imgSize + "height:" + this. imgHeight + "width:" + this. imgWidth + "shooting time:" + this. dateTime + "altitude:" + this. altitude + "latitude:" + this. latitude + "longitude:" + this. longpolling ;}}

 

The above code can be used to obtain the exif information in the image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.