ImageUtils { @Nullable public static Geolocation getGeolocation(@Nullable final byte[] imageBytes) throws ImageProcessingException, IOException { if (imageBytes != null && imageBytes.length > 0) { final Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(imageBytes)), false); if (metadata != null) { final GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class); if (gpsDirectory != null) { return new GeolocationImpl(gpsDirectory); } } } return null; } private  ImageUtils(); static boolean isSupportedImage(@Nullable final byte[] imageBytes); @Nullable static Image createJpegThumbnail(@Nullable final byte[] imageBytes, final int lengthOfLongestSideInPixels); @Nullable static Geolocation getGeolocation(@Nullable final byte[] imageBytes); @Nullable static ImageOrientation getOrientation(@Nullable final byte[] imageBytes); @Nullable static BufferedImage convertToBufferedImage(@Nullable final byte[] imageBytes); @Nullable static byte[] convertToJpegByteArray(@Nullable final BufferedImage image); @Nullable static ImageType getImageType(@Nullable final byte[] imageBytes);  }
@Test public void testExtractLatLong() throws Exception { final double delta = 0.00001; Assert.assertNull(ImageUtils.getGeolocation(IMAGE_1)); final Geolocation geolocation1 = ImageUtils.getGeolocation(IMAGE_2); Assert.assertNotNull(geolocation1); Assert.assertEquals(40.44283333333333, geolocation1.getLatitude(), delta); Assert.assertEquals(-79.94633333333333, geolocation1.getLongitude(), delta); Assert.assertEquals(318.99533, geolocation1.getHeading(), delta); Assert.assertEquals("T", geolocation1.getHeadingRef()); Assert.assertEquals(318.00183, geolocation1.getAltitude(), delta); Assert.assertEquals((Integer)0, geolocation1.getAltitudeRef()); Assert.assertEquals(null, geolocation1.getGpsPrecision()); Assert.assertEquals(null, geolocation1.getGpsDatestamp()); Assert.assertEquals("20:32:31 UTC", geolocation1.getGpsTimestamp()); try { ImageUtils.getGeolocation(IMAGE_3); Assert.fail("ImageUtils.getGeolocation() should fail for IMAGE_3 since the image type is not supported."); } catch (ImageProcessingException e) { } catch (IOException e) { Assert.fail("Should not get an IOException for calling ImageUtils.getGeolocation() on IMAGE_3."); } try { ImageUtils.getGeolocation(IMAGE_4); Assert.fail("ImageUtils.getGeolocation() should fail for IMAGE_4 since the image type is not supported."); } catch (ImageProcessingException e) { } catch (IOException e) { Assert.fail("Should not get an IOException for calling ImageUtils.getGeolocation() on IMAGE_4."); } try { ImageUtils.getGeolocation(IMAGE_4); Assert.fail("ImageUtils.getGeolocation() should fail for IMAGE_4 since the image type is not supported."); } catch (ImageProcessingException e) { } catch (IOException e) { Assert.fail("Should not get an IOException for calling ImageUtils.getGeolocation() on IMAGE_4."); } Assert.assertNull(ImageUtils.getGeolocation(IMAGE_5)); final Geolocation geolocation2 = ImageUtils.getGeolocation(IMAGE_6); Assert.assertNotNull(geolocation2); Assert.assertEquals(45.50066666666667, geolocation2.getLatitude(), delta); Assert.assertEquals(9.110333333333333, geolocation2.getLongitude(), delta); Assert.assertEquals(null, geolocation2.getHeading()); Assert.assertEquals(null, geolocation2.getHeadingRef()); Assert.assertEquals(217, geolocation2.getAltitude(), delta); Assert.assertEquals((Integer)0, geolocation2.getAltitudeRef()); Assert.assertEquals(null, geolocation2.getGpsPrecision()); Assert.assertEquals("2011:05:06", geolocation2.getGpsDatestamp()); Assert.assertEquals("7:59:48 UTC", geolocation2.getGpsTimestamp()); try { ImageUtils.getGeolocation(NOT_AN_IMAGE); Assert.fail("ImageUtils.getGeolocation() should fail for NOT_AN_IMAGE since the image type is not supported."); } catch (ImageProcessingException e) { } catch (IOException e) { Assert.fail("Should not get an IOException for calling ImageUtils.getGeolocation() on NOT_AN_IMAGE."); } Assert.assertNull(ImageUtils.getGeolocation(null)); Assert.assertNull(ImageUtils.getGeolocation(new byte[]{})); }