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