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.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          
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  
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  }