替换GPS标签值[编辑EXIF]

时间:2017-03-16 03:20:16

标签: java metadata jpeg exif

我正在尝试删除标记,以便能够使用更正后的值重新添加标记。有问题的exif标签是:GPSImgDirection但它似乎没有保存新值(我猜测它没有删除/找到标签)

我究竟做错了什么?我一直在尝试从apache(WriteExifMetadataExample.java)开始关注示例文件。

P.S。我在Rational值中设置常量值以简化代码示例...

public static void setBearing(File f, Double bearing)
    throws IOException, ImageReadException, ImageWriteException{

    try (FileOutputStream fos = new FileOutputStream(f.getParent()+ File.separator+"SUPERTMPDUMP12324231.jpg",false);
            OutputStream os = new BufferedOutputStream(fos);) {

        TiffOutputSet outputSet = null;

        final ImageMetadata metadata = Imaging.getMetadata(new File(f.getAbsolutePath()));
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
        if (null != jpegMetadata) {
            final TiffImageMetadata exif = jpegMetadata.getExif();

            if (null != exif) {
                outputSet = exif.getOutputSet();
            }
        }

        if (null == outputSet) {
            outputSet = new TiffOutputSet();
        }

        Rational r = new Rational(bearing);    
        final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();

        exifDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION );
        exifDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, new RationalNumber(454349, 10000));


        new ExifRewriter().updateExifMetadataLossless(f, os,
                outputSet);
        //Replace file with new meta data
        Files.move(Paths.get(f.getPath()+ File.separator+"SUPERTMPDUMP12324231.jpg"), Paths.get(f.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
    }

}

1 个答案:

答案 0 :(得分:0)

发现了这个问题,因为它出现在" GPS"目录我需要:

final TiffOutputDirectory exifDirectory = outputSet.getOrCreateGPSDirectory();

而不是

final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();