Example usage for org.apache.commons.imaging.formats.jpeg.iptc PhotoshopApp13Data getRecords

List of usage examples for org.apache.commons.imaging.formats.jpeg.iptc PhotoshopApp13Data getRecords

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.jpeg.iptc PhotoshopApp13Data getRecords.

Prototype

public List<IptcRecord> getRecords() 

Source Link

Usage

From source file:joachimeichborn.geotag.io.jpeg.PictureMetadataWriter.java

private void setGeocoding(final Path aTargetFile) throws IOException, ImageReadException, ImageWriteException {
    final Path sourceFile = picture.getFile();

    final PhotoshopApp13Data photoshopMetadata = getPhotoshopMetadata(sourceFile);
    if (photoshopMetadata == null) {
        throw new ImageReadException("Could not obtain photoshop metadata");
    }/* w  ww . j a  v a2 s .co  m*/

    try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(aTargetFile.toFile()))) {
        final Geocoding geocoding = picture.getGeocoding();

        final EnumSet<IptcTypes> filterTypes = EnumSet.noneOf(IptcTypes.class);
        filterTypes.add(IptcTypes.CONTENT_LOCATION_NAME);
        filterTypes.add(IptcTypes.CITY);
        filterTypes.add(IptcTypes.SUBLOCATION);
        filterTypes.add(IptcTypes.PROVINCE_STATE);
        filterTypes.add(IptcTypes.COUNTRY_PRIMARY_LOCATION_CODE);
        filterTypes.add(IptcTypes.COUNTRY_PRIMARY_LOCATION_NAME);

        final List<IptcRecord> records = photoshopMetadata.getRecords();
        final Iterator<IptcRecord> iter = records.iterator();
        while (iter.hasNext()) {
            final IptcRecord record = iter.next();
            final IptcType type = record.iptcType;
            if (filterTypes.contains(type)) {
                iter.remove();
            }
        }

        records.add(new IptcRecord(IptcTypes.CONTENT_LOCATION_NAME,
                geocoding != null ? geocoding.getLocationName() : ""));
        records.add(new IptcRecord(IptcTypes.CITY, geocoding != null ? geocoding.getCity() : ""));
        records.add(new IptcRecord(IptcTypes.SUBLOCATION, geocoding != null ? geocoding.getSublocation() : ""));
        records.add(new IptcRecord(IptcTypes.PROVINCE_STATE,
                geocoding != null ? geocoding.getProvinceState() : ""));
        records.add(new IptcRecord(IptcTypes.COUNTRY_PRIMARY_LOCATION_CODE,
                geocoding != null ? geocoding.getCountryCode() : ""));
        records.add(new IptcRecord(IptcTypes.COUNTRY_PRIMARY_LOCATION_NAME,
                geocoding != null ? geocoding.getCountryName() : ""));

        final List<IptcBlock> rawBlocks = photoshopMetadata.getRawBlocks();

        new JpegIptcRewriter().writeIPTC(sourceFile.toFile(), os, new PhotoshopApp13Data(records, rawBlocks));
    }
}