使用java commons-imaging访问jpeg标签

时间:2018-05-07 20:01:14

标签: java image jpeg tiff exif

如何在java中使用 common-imaging sanselan 访问(读/写)jpeg图像标记,这个link有一些关于主题。

enter image description here

我使用上面提到的库尝试了以下代码,只要sanselan,但没有感情。

public static void addImageHistoryTag(File file) {
    File dst = null;
    IImageMetadata metadata = null;
    JpegImageMetadata jpegMetadata = null;
    TiffImageMetadata exif = null;
    OutputStream os = null;
    TiffOutputSet outputSet = new TiffOutputSet();

    // establish metadata
    try {
        metadata = Sanselan.getMetadata(file);
    } catch (ImageReadException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // establish jpegMedatadata
    if (metadata != null) {
        jpegMetadata = (JpegImageMetadata) metadata;
    }

    // establish exif
    if (jpegMetadata != null) {
        exif = jpegMetadata.getExif();
    }

    // establish outputSet
    if (exif != null) {
        try {
            outputSet = exif.getOutputSet();
        } catch (ImageWriteException e) {
            e.printStackTrace();
        }
    }

    if (outputSet != null) {
        // check if field already EXISTS - if so remove
        TiffOutputField imageHistoryPre = outputSet
                .findField(TiffConstants.EXIF_TAG_XPCOMMENT);
        if (imageHistoryPre != null) {
            System.out.println("REMOVE");
            outputSet.removeField(TiffConstants.EXIF_TAG_XPCOMMENT);
        }
        // add field
        try {
            String fieldData = "Hallo";
            TiffOutputField imageHistory = new TiffOutputField(
                    TiffConstants.EXIF_TAG_XPTITLE,
                    TiffFieldTypeConstants.FIELD_TYPE_BYTE,
                    fieldData.length(),
                    fieldData.getBytes());

            TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
            exifDirectory.removeField(TiffConstants.EXIF_TAG_XPTITLE);
            exifDirectory.add(imageHistory);

        } catch (ImageWriteException e) {

            e.printStackTrace();
        }
    }

    try {
        dst = new File("D:\\aa\\111.jpeg");
        os = new FileOutputStream(dst);
        os = new BufferedOutputStream(os);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // write/update EXIF metadata to output stream
    try {
        new ExifRewriter().updateExifMetadataLossless(file,
                os, outputSet);
    } catch (ImageReadException e) {
        e.printStackTrace();
    } catch (ImageWriteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
    }
}

此代码不影响图像详细信息标记,所以我正在寻找可用于访问“标记字段”的正确常量

1 个答案:

答案 0 :(得分:0)

如果你正在使用公共成像,请不要使用Sanselan.getMetadata(file),使用Imaging.getMetadata(file)是一致的。据我所知,Sanselan因公共成像而被弃用。

此外,您可以尝试设置MicrosoftTagConstants,特别是EXIF_TAG_XPKEYWORDS。为此,您需要使用this answer