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  
22  import java.io.File;
23  import java.util.stream.Stream;
24  
25  import org.apache.commons.imaging.Imaging;
26  import org.apache.commons.imaging.common.RationalNumber;
27  import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
28  import org.apache.commons.imaging.formats.tiff.TiffField;
29  import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
30  import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
31  import org.apache.commons.imaging.internal.Debug;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.MethodSource;
35  
36  public class GpsTest extends AbstractExifTest {
37  
38      public static Stream<File> data() throws Exception {
39          return getImagesWithExifData().stream();
40      }
41  
42      @ParameterizedTest
43      @MethodSource("data")
44      public void test(final File imageFile) throws Exception {
45          if (imageFile.getParentFile().getName().toLowerCase().equals("@broken")) {
46              return;
47          }
48  
49          final JpegImageMetadata metadata = (JpegImageMetadata) Imaging.getMetadata(imageFile);
50          if (null == metadata) {
51              return;
52          }
53  
54          final TiffImageMetadata exifMetadata = metadata.getExif();
55          if (null == exifMetadata) {
56              return;
57          }
58  
59          final TiffImageMetadata.GpsInfo gpsInfo = exifMetadata.getGpsInfo();
60          if (null == gpsInfo) {
61              return;
62          }
63  
64          // TODO we should assert something here.
65          Debug.debug("imageFile " + imageFile);
66          Debug.debug("gpsInfo " + gpsInfo);
67          Debug.debug("gpsInfo longitude as degrees east " + gpsInfo.getLongitudeAsDegreesEast());
68          Debug.debug("gpsInfo latitude as degrees north " + gpsInfo.getLatitudeAsDegreesNorth());
69          Debug.debug();
70  
71      }
72  
73      /**
74       * @throws Exception if it cannot open the images.
75       */
76      @Test
77      public void testReadMetadata() throws Exception {
78          final File imageFile = new File(GpsTest.class.getResource("/images/jpeg/exif/2024-04-30_G012.JPG").getFile());
79          final JpegImageMetadata jpegMetadata = (JpegImageMetadata) Imaging.getMetadata(imageFile);
80          final TiffField gpsHPosErrorField = jpegMetadata.findExifValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_HOR_POSITIONING_ERROR);
81          final RationalNumber gpsHPosError = (RationalNumber) gpsHPosErrorField.getValue();
82          assertEquals(0.014, gpsHPosError.doubleValue());
83      }
84  }