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.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.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.Test;
36  
37  public class AsciiFieldTest extends ExifBaseTest {
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 Map<String, Object> params = new HashMap<>();
44  
45          final ImageMetadata metadata = Imaging.getMetadata(imageFile, params);
46          assertNotNull(metadata);
47          final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
48  
49          // note that exif might be null if no Exif metadata is found.
50          final TiffImageMetadata exif = jpegMetadata.getExif();
51          final List<TiffField> fields = exif.getAllFields();
52          final Map<Integer,TiffField> fieldMap = new HashMap<>();
53          // Build a simplified field tag -> field map, ignoring directory
54          // structures.
55          // Good enough for our purposes, since the image in question is known.
56          for (final TiffField field : fields) {
57              fieldMap.put(field.getTag(), field);
58          }
59  
60          final Map<Integer,Object> expectedFieldValues = new HashMap<>();
61          expectedFieldValues.put(
62                  TiffTagConstants.TIFF_TAG_MAKE.tag, "Canon");
63          expectedFieldValues.put(
64                  TiffTagConstants.TIFF_TAG_MODEL.tag,
65                  "Canon PowerShot SD750");
66          expectedFieldValues.put(
67                  TiffTagConstants.TIFF_TAG_DATE_TIME.tag,
68                  "2007:12:25 13:34:39");
69  
70          for (final Map.Entry<Integer, Object> tag : expectedFieldValues.entrySet()) {
71              assertTrue(fieldMap.containsKey(tag.getKey()));
72              final TiffField field = fieldMap.get(tag.getKey());
73              assertNotNull(field);
74              final Object value = field.getValue();
75              assertNotNull(value);
76              assertEquals(value, tag.getValue());
77          }
78      }
79  }