1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.imaging.formats.jpeg.iptc;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.stream.Stream;
28
29 import org.apache.commons.imaging.bytesource.ByteSource;
30 import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
31 import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
32 import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.MethodSource;
35
36 public class IptcAddTest extends IptcBaseTest {
37
38 public static Stream<File> data() throws Exception {
39 return getJpegImages().stream();
40 }
41
42
43
44
45 @ParameterizedTest
46 @MethodSource("data")
47 public void testAddIptcData(final File imageFile) throws Exception {
48 final ByteSource byteSource = ByteSource.file(imageFile);
49
50 final JpegImagingParameters params = new JpegImagingParameters();
51
52 final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(byteSource, params);
53 if (metadata == null) {
54
55 return;
56 }
57
58 final List<IptcBlock> newBlocks = new ArrayList<>(metadata.photoshopApp13Data.getNonIptcBlocks());
59 final List<IptcRecord> oldRecords = metadata.photoshopApp13Data.getRecords();
60
61 final List<IptcRecord> newRecords = new ArrayList<>();
62 for (final IptcRecord record : oldRecords) {
63 if (record.iptcType != IptcTypes.CITY && record.iptcType != IptcTypes.CREDIT) {
64 newRecords.add(record);
65 }
66 }
67
68 newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
69 newRecords.add(new IptcRecord(IptcTypes.CREDIT, "William Sorensen"));
70
71 final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);
72
73 byte[] bytes;
74 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
75 new JpegIptcRewriter().writeIptc(byteSource, os, newData);
76 bytes = os.toByteArray();
77 }
78
79 final ByteSource updateByteSource = ByteSource.array(bytes, "test.jpg");
80 final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(updateByteSource, params);
81
82 assertNotNull(outMetadata);
83 assertEquals(outMetadata.getItems().size(), newRecords.size());
84 }
85 }