View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.formats.jpeg.exif;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.imaging.Imaging;
30  import org.apache.commons.imaging.common.ImageMetadata;
31  import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
32  import org.apache.commons.imaging.formats.tiff.TiffField;
33  import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
34  import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
35  import org.junit.jupiter.api.Test;
36  
37  public class AsciiFieldTest extends AbstractExifTest {
38  
39      @Test
40      public void testSingleImage() throws Exception {
41          final File imageFile = getTestImageByName("Canon Powershot SD750 - 2007.12.26.n.IMG_3704.JPG");
42  
43          final ImageMetadata metadata = Imaging.getMetadata(imageFile);
44          assertNotNull(metadata);
45          final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
46  
47          // note that exif might be null if no Exif metadata is found.
48          final TiffImageMetadata exif = jpegMetadata.getExif();
49          final List<TiffField> fields = exif.getAllFields();
50          final Map<Integer, TiffField> fieldMap = new HashMap<>();
51          // Build a simplified field tag -> field map, ignoring directory
52          // structures.
53          // Good enough for our purposes, since the image in question is known.
54          for (final TiffField field : fields) {
55              fieldMap.put(field.getTag(), field);
56          }
57  
58          final Map<Integer, Object> expectedFieldValues = new HashMap<>();
59          expectedFieldValues.put(TiffTagConstants.TIFF_TAG_MAKE.tag, "Canon");
60          expectedFieldValues.put(TiffTagConstants.TIFF_TAG_MODEL.tag, "Canon PowerShot SD750");
61          expectedFieldValues.put(TiffTagConstants.TIFF_TAG_DATE_TIME.tag, "2007:12:25 13:34:39");
62  
63          for (final Map.Entry<Integer, Object> tag : expectedFieldValues.entrySet()) {
64              assertTrue(fieldMap.containsKey(tag.getKey()));
65              final TiffField field = fieldMap.get(tag.getKey());
66              assertNotNull(field);
67              final Object value = field.getValue();
68              assertNotNull(value);
69              assertEquals(value, tag.getValue());
70          }
71      }
72  }