iptc classification java code example

https:‮w//‬ww.lautturi.com
iptc classification java code example

The IPTC (International Press Telecommunications Council) classification is a standardized system for categorizing news articles based on their content and subject matter. In Java, you can use the com.drew.metadata.iptc.IptcDirectory class from the metadata-extractor library to read and write IPTC classification data in image files.

Here is an example of how to use the IptcDirectory class to read IPTC classification data from a JPEG image file in Java:

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.iptc.IptcDirectory;

import java.io.File;

public class IptcClassificationExample {
    public static void main(String[] args) throws Exception {
        // Read the metadata from the image file
        File imageFile = new File("image.jpg");
        Metadata metadata = ImageMetadataReader.readMetadata(imageFile);

        // Get the IPTC directory
        IptcDirectory iptcDirectory = metadata.getFirstDirectoryOfType(IptcDirectory.class);

        // Read the IPTC classification data
        String classification = iptcDirectory.getString(IptcDirectory.TAG_CLASSIFICATION);
        String subjectCode = iptcDirectory.getString(IptcDirectory.TAG_SUBJECT_CODE);
        String sceneCode = iptcDirectory.getString(IptcDirectory.TAG_SCENE_CODE);
        String subjectNewsCode = iptcDirectory.getString(IptcDirectory.TAG_SUBJECT_NEWS_CODE);

        // Print the IPTC classification data
        System.out.println("Classification: " + classification);
        System.out.println("Subject code: " + subjectCode);
        System.out.println("Scene code: " + sceneCode);
        System.out.println("Subject news code: " + subjectNewsCode);
    }
}

In this example, we use the ImageMetadataReader class to read the metadata from the image file, and then we get the IptcDirectory object using the getFirstDirectoryOfType method. We can then use the getString method of the IptcDirectory object to read the IPTC classification data, such as the classification, subjectCode, sceneCode, and subjectNewsCode.

For more information on the IPTC classification system and how to use the IptcDirectory class in Java, you can refer to the metadata-extractor documentation or other online resources.

Created Time:2017-11-01 22:29:53  Author:lautturi