如何使用mediautil.jar,照片(jpg)文件插入(添加)gps写EXIF

时间:2015-10-03 15:59:19

标签: java gps exif

public static void main(String[] args) {
      InputStream fip = null;
      try {
          fip = new BufferedInputStream(new FileInputStream("D:\\[netbeans]\\2016.jpg"));
          LLJTran llj = new LLJTran(fip);
          try {
              llj.read(LLJTran.READ_INFO, true);
          } catch (LLJTranExceptienter code hereon ex) {
              // Logger.getLogger(EXIF02.class.getName()).log(Level.SEVERE, null, ex);
          }
          AbstractImageInfo imageInfo = llj.getImageInfo();
          if (!(imageInfo instanceof Exif)) {
              System.out.println("Sorry Image Does not have Exif. Exitting." + imageInfo);
              System.exit(1);
          }

          Exif exif = (Exif) imageInfo;
          IFD mainIfd = exif.getIFDs()[0];

          IFD gpsIfd = mainIfd.getIFD(Exif.GPSINFO);
          if (gpsIfd==null) {
              System.out.println("no");
          }else{
              System.out.println("yes");
          }
          Entry e;
           // Set Latitude
           e = new Entry(Exif.ASCII);
           e.setValue(0, 'N');

           gpsIfd.setEntry(new Integer(Exif.GPSLatitudeRef), 0, e);
        //run time error: Photo EXIF files no gps
           e = new Entry(Exif.RATIONAL);
           e.setValue(0, new Rational(45, 1));
           e.setValue(1, new Rational(35, 1));
           e.setValue(2, new Rational(25, 1));

           gpsIfd.setEntry(new Integer(Exif.GPSLatitude), 0, e);
           e = new Entry(Exif.BYTE);
           // Set Longitude
           e = new Entry(Exif.ASCII);
           e.setValue(0, 'E');

           gpsIfd.setEntry(new Integer(Exif.GPSLongitudeRef), 0, e);
           e = new Entry(Exif.RATIONAL);
           e.setValue(0, new Rational(87, 1));
           e.setValue(1, new Rational(40, 1));
           e.setValue(2, new Rational(30, 1));
           gpsIfd.setEntry(new Integer(Exif.GPSLongitude), 0, e);
           e = new Entry(Exif.BYTE);
           e.setValue(0, new Integer(1)); 
         // This picture is taken underwater :-)
           // Use 0 if it is taken above sea
           // level
           gpsIfd.setEntry(new Integer(Exif.GPSAltitudeRef), 0, e);
           e = new Entry(Exif.RATIONAL);
           e.setValue(0, new Rational(100, 1));
           gpsIfd.setEntry(new Integer(Exif.GPSAltitude), 0, e);
           llj.refreshAppx(); // Recreate Marker Data for changes done
           OutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\[netbeans]\\2016-2.jpg"));
           try {
           Transfer remaining of image to output with new header.
           llj.xferInfo(null, out, LLJTran.REPLACE, LLJTran.REPLACE);
           } catch (LLJTranException ex) {
            Logger.getLogger(EXIF02.class.getName()).log(Level.SEVERE, null, ex);
           }
           try {
           fip.close();
           } catch (IOException ex) {
           Logger.getLogger(EXIF02.class.getName()).log(Level.SEVERE, null, ex);
           }
           try {
           out.close();
           } catch (IOException ex) {
           Logger.getLogger(EXIF02.class.getName()).log(Level.SEVERE, null, ex);
           }
           llj.freeMemory();
      } catch (FileNotFoundException ex) {
          Logger.getLogger(EXIF02.class.getName()).log(Level.SEVERE, null, ex);

      }
      }

方法一:尝试使用元数据提取器2.8.1 API,只读GPS不//搜索GPS。 API查找setLatitude或setLongitude API [http://javadoc.metadata-extractor.googlecode.com/git/2.7.0/index.html][1]

方法二:使用mediachest [http://mediachest.sourceforge.net/mediautil/javadocs/mediautil/image/jpeg/LLJ// Tran.html] [2]。使用示例运行错误

使用照片文件试试gpsIfd值为null或不为null。

案例a:照片EXIF文件gps 运行错误      解析XML时出错:java.net.MalformedURLException      解析XML时出错:java.net.MalformedURLException      解析XML时出错:java.net.MalformedURLException 但照片updata gps是成功

案例b:照片EXIF文件没有gps      解析XML时出错:java.net.MalformedURLException      解析XML时出错:java.net.MalformedURLException      解析XML时出错:java.net.MalformedURLException

 Exception in thread "main" java.lang.NullPointerException
    at exif02.EXIF02.main(EXIF02.java:XXX)   PS:xxx number line

Java结果:1

但照片updata gps是失败

0 个答案:

没有答案