1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.jpeg.iptc;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import java.util.List;
28
29 import org.apache.commons.imaging.ImagingException;
30 import org.apache.commons.imaging.bytesource.ByteSource;
31 import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
32 import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
33 import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
34 import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
35 import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
36 import org.apache.commons.imaging.test.TestResources;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 public class IptcParserTest {
43
44
45
46
47
48
49
50 @Test
51 public void testEncodingSupport() throws IOException, ImagingException {
52
53 final File file = TestResources.resourceToFile("/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg");
54 final JpegImageParser parser = new JpegImageParser();
55 final JpegImageMetadata metadata = (JpegImageMetadata) parser.getMetadata(file);
56 final JpegPhotoshopMetadata photoshopMetadata = metadata.getPhotoshop();
57 @SuppressWarnings("unchecked")
58 final List<GenericImageMetadataItem> items = (List<GenericImageMetadataItem>) photoshopMetadata.getItems();
59 final GenericImageMetadataItem thanksInMandarin = items.get(3);
60
61 assertArrayEquals("\u8c22\u8c22".getBytes(StandardCharsets.UTF_8), thanksInMandarin.getText().getBytes(StandardCharsets.UTF_8));
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 @Test
79 public void testSkipBlockTypes() throws ImagingException, IOException {
80 final File imageFile = TestResources.resourceToFile("/images/jpeg/photoshop/IMAGING-246/FallHarvestKitKat_07610.jpg");
81 final JpegImageMetadata metadata = (JpegImageMetadata) new JpegImageParser().getMetadata(ByteSource.file(imageFile), new JpegImagingParameters());
82 final JpegPhotoshopMetadata photoshopMetadata = metadata.getPhotoshop();
83 final PhotoshopApp13Data photoshopApp13Data = photoshopMetadata.photoshopApp13Data;
84 final List<IptcBlock> blocks = photoshopApp13Data.getRawBlocks();
85 assertEquals(2, blocks.size());
86 for (final IptcBlock block : blocks) {
87 if (block.getBlockType() == 1028 || block.getBlockType() == 1061) {
88
89
90 final byte[] data = block.getBlockData();
91 assertTrue(data.length > 0);
92 } else {
93 fail("Unexpected block type found: " + block.getBlockType());
94 }
95 }
96 }
97 }