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 java.io.File;
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.commons.imaging.AbstractImagingTest;
25 import org.apache.commons.imaging.ImagingException;
26 import org.apache.commons.imaging.bytesource.ByteSource;
27 import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
28
29 public abstract class AbstractExifTest extends AbstractImagingTest {
30
31 private static final ImageFilter HAS_EXIF_IMAGE_FILTER = AbstractExifTest::hasExifData;
32
33 private static final ImageFilter JPEG_IMAGE_FILTER = file -> file.getName().toLowerCase().endsWith(".jpg");
34
35 protected static List<File> getImagesWithExifData() throws IOException, ImagingException {
36 return getTestImages(HAS_EXIF_IMAGE_FILTER);
37 }
38
39 protected static List<File> getJpegImages() throws IOException, ImagingException {
40 return getTestImages(JPEG_IMAGE_FILTER);
41 }
42
43 protected static boolean hasExifData(final File file) {
44 if (!file.getName().toLowerCase().endsWith(".jpg")) {
45 return false;
46 }
47
48 try {
49 final ByteSource byteSource = ByteSource.file(file);
50 return new JpegImageParser().hasExifSegment(byteSource);
51 } catch (final Exception e) {
52 return false;
53 }
54 }
55
56 protected static boolean hasExifData(final String fileName, final byte[] bytes) {
57 try {
58 final ByteSource byteSource = ByteSource.array(bytes, fileName);
59 return new JpegImageParser().hasExifSegment(byteSource);
60 } catch (final Exception e) {
61 return false;
62 }
63 }
64
65 protected File getImageWithExifData() throws IOException, ImagingException {
66 return getTestImage(HAS_EXIF_IMAGE_FILTER);
67 }
68
69 }