中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java從圖片中讀取圖片的元數(shù)據(jù)Exif信息

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
一般情況下是java程序讀取不到gps等擴展信息的。如果想要解析到里面的信息需要下載一個jar包,metadata-extractor-2.6.4.jar(下載地址: http://code.google.com/p/metadata-extractor/),這個jar提供了支持獲取擴展信息的功能。
import java.io.File;
import java.io.IOException;
 
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
 
public class SampleUsage
{
   /**
    * 圖片信息獲取metadata元數(shù)據(jù)信息
    * @param fileName 需要解析的文件
    * @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;
    }
 
    /**
     * 讀取metadata里面的信息
     * @param sourceFile 源文件
     * @param metadata metadata元數(shù)據(jù)信息
     * @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")) {
                    //圖片高度
                    imgInfoBean.setImgHeight(desc);
                } else if (tagName.equals("Image Width")) {
                    //圖片寬度
                    imgInfoBean.setImgWidth(desc);
                } else if (tagName.equals("Date/Time Original")) {
                    //拍攝時間
                    imgInfoBean.setDateTime(desc);
                } else if (tagName.equals("GPS Altitude")) {
                    //海拔
                    imgInfoBean.setAltitude(desc);
                } else if (tagName.equals("GPS Latitude")) {
                    //緯度
                    imgInfoBean.setLatitude(pointToLatlong(desc));
                } else if (tagName.equals("GPS Longitude")) {
                    //經(jīng)度
                    imgInfoBean.setLongitude(pointToLatlong(desc));
                }
            }
            for (String error : directory.getErrors()){
                System.err.println("ERROR: " + error);
            }
        }
        return imgInfoBean;
    }
 
    /**
     * 經(jīng)緯度轉(zhuǎn)換  度分秒轉(zhuǎn)換
     * @param point 坐標(biāo)點
     * @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());
    }
}
 

文件信息bean類。
public class ImgInfoBean {
 
    private String imgHeight ;//圖片高度
    private String imgWidth ;//圖片寬度
    private String dateTime ;//拍攝時間
    private String altitude ;//海拔
    private String latitude;//緯度
    private String longitude ;//經(jīng)度
    private Long imgSize;    //文件大小
    private String imgName;  //文件名稱
    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 getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
     
    public String toString (){
        return "[圖片信息]文件名稱:"+ this.imgName+"   文件大小:"+this.imgSize +"  高度:"+this.imgHeight+"  寬度:"+this.imgWidth+"  拍攝時間:"+this.dateTime+"  海拔:"+this.altitude+"   緯度:"+this.latitude+"  經(jīng)度:"+this.longitude;
    }
}

標(biāo)簽: Google

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:iOS實現(xiàn)毛玻璃效果

下一篇:Android下通過wifi調(diào)用打印機打印