public void testExtractMetadata() throws Exception { File withExif = new File("Source/com/drew/metadata/exif/test/withExif.jpg"); Metadata metadata = JpegMetadataReader.readMetadata(withExif); assertTrue(metadata.containsDirectory(ExifDirectory.class)); Directory directory = metadata.getDirectory(ExifDirectory.class); assertEquals("80", directory.getString(ExifDirectory.TAG_ISO_EQUIVALENT)); }
private static Directory getMetaDirectory(File file) { try { Metadata metadata = JpegMetadataReader.readMetadata(file); return metadata.getDirectory(ExifDirectory.class); } catch (JpegProcessingException e) { e.printStackTrace(); } return null; }
@Before public void setUp() throws Exception { File nikonJpeg = new File("Tests/com/drew/metadata/exif/nikonMakernoteType1.jpg"); Metadata metadata = JpegMetadataReader.readMetadata(nikonJpeg); _nikonDirectory = metadata.getDirectory(NikonType1MakernoteDirectory.class); _exifSubIFDDirectory = metadata.getDirectory(ExifSubIFDDirectory.class); _exifIFD0Directory = metadata.getDirectory(ExifIFD0Directory.class); _thumbDirectory = metadata.getDirectory(ExifThumbnailDirectory.class); }
/* (non-Javadoc) * @see org.inbio.m3s.dao.multimedia.MetadataExtractorDAO#init(java.lang.String) */ public void init(String fileAddress) throws IllegalArgumentException { logger.info("using the fileAddress: '" + fileAddress + "'"); this.fileAddress = fileAddress; try { File jpegFile = new File(fileAddress); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); this.exifDirectory = metadata.getDirectory(ExifDirectory.class); } catch (JpegProcessingException e) { e.printStackTrace(); throw new IllegalArgumentException( "The fileAddress [" + fileAddress + "] doesn't exist. ", e.getCause()); } }
private void extractMetadata(UploadItem item, Image image) { InputStream in = null; try { in = new FileInputStream(item.getFile()); Metadata metadata = JpegMetadataReader.readMetadata(in); Directory exifDirectory = metadata.getDirectory(ExifDirectory.class); Directory jpgDirectory = metadata.getDirectory(JpegDirectory.class); setupCameraModel(image, exifDirectory); setupDimensions(image, exifDirectory, jpgDirectory); setupCreatedDate(image, exifDirectory); } catch (Exception e) { addError(item, image, Constants.IMAGE_SAVING_ERROR); } finally { try { in.close(); } catch (IOException e) { addError(item, image, Constants.IMAGE_SAVING_ERROR); } } }
public ImageGeo(String filename) { try { error = false; File jpegFile = new File(filename); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); GpsDirectory gpsdir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class); Rational latpart[] = gpsdir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE); Rational lonpart[] = gpsdir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE); String northing = gpsdir.getString(GpsDirectory.TAG_GPS_LATITUDE_REF); String easting = gpsdir.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF); try { alt = gpsdir.getDouble(GpsDirectory.TAG_GPS_ALTITUDE); } catch (Exception ex) { } double latsign = 1.0d; if (northing.equalsIgnoreCase("S")) latsign = -1.0d; double lonsign = 1.0d; if (easting.equalsIgnoreCase("W")) lonsign = -1.0d; lat = (Math.abs(latpart[0].doubleValue()) + latpart[1].doubleValue() / 60.0d + latpart[2].doubleValue() / 3600.0d) * latsign; lon = (Math.abs(lonpart[0].doubleValue()) + lonpart[1].doubleValue() / 60.0d + lonpart[2].doubleValue() / 3600.0d) * lonsign; if (Double.isNaN(lat) || Double.isNaN(lon)) error = true; } catch (Exception ex) { error = true; } System.out.println(filename + ": (" + lat + ", " + lon + ")"); }
/** * Reads metadata from an {@link InputStream}. * * @param inputStream a stream from which the file data may be read. The stream must be positioned * at the beginning of the file's data. * @return a populated {@link Metadata} object containing directories of tags with values and any * processing errors. * @throws ImageProcessingException if the file type is unknown, or for general processing errors. */ @NotNull public static Metadata readMetadata(@NotNull final InputStream inputStream) throws ImageProcessingException, IOException { BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream); if (fileType == FileType.Jpeg) return JpegMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Tiff || fileType == FileType.Arw || fileType == FileType.Cr2 || fileType == FileType.Nef || fileType == FileType.Orf || fileType == FileType.Rw2) return TiffMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Psd) return PsdMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Png) return PngMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Bmp) return BmpMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Gif) return GifMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Ico) return IcoMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Pcx) return PcxMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Riff) return WebpMetadataReader.readMetadata(bufferedInputStream); if (fileType == FileType.Raf) return RafMetadataReader.readMetadata(bufferedInputStream); throw new ImageProcessingException("File format is not supported"); }
/** * Executes the sample usage program. * * @param args command line parameters */ public static void main(String[] args) { File file = new File("Tests/Data/withIptcExifGps.jpg"); // There are multiple ways to get a Metadata object for a file // // SCENARIO 1: UNKNOWN FILE TYPE // // This is the most generic approach. It will transparently determine the file type and invoke // the appropriate // readers. In most cases, this is the most appropriate usage. This will handle JPEG, TIFF, // GIF, BMP and RAW // (CRW/CR2/NEF/RW2/ORF) files and extract whatever metadata is available and understood. // try { Metadata metadata = ImageMetadataReader.readMetadata(file); print(metadata); } catch (ImageProcessingException e) { // handle exception } catch (IOException e) { // handle exception } // // SCENARIO 2: SPECIFIC FILE TYPE // // If you know the file to be a JPEG, you may invoke the JpegMetadataReader, rather than the // generic reader // used in approach 1. Similarly, if you knew the file to be a TIFF/RAW image you might use // TiffMetadataReader, // PngMetadataReader for PNG files, BmpMetadataReader for BMP files, or GifMetadataReader for // GIF files. // // Using the specific reader offers a very, very slight performance improvement. // try { Metadata metadata = JpegMetadataReader.readMetadata(file); print(metadata); } catch (JpegProcessingException e) { // handle exception } catch (IOException e) { // handle exception } // // APPROACH 3: SPECIFIC METADATA TYPE // // If you only wish to read a subset of the supported metadata types, you can do this by // passing the set of readers to use. // // This currently only applies to JPEG file processing. // try { // We are only interested in handling Iterable<JpegSegmentMetadataReader> readers = Arrays.asList(new ExifReader(), new IptcReader()); Metadata metadata = JpegMetadataReader.readMetadata(file, readers); print(metadata); } catch (JpegProcessingException e) { // handle exception } catch (IOException e) { // handle exception } }
private static void extractExif(ImageEntry e) { double deg; double min, sec; double lon, lat; Metadata metadata = null; Directory dir = null; try { metadata = JpegMetadataReader.readMetadata(e.getFile()); dir = metadata.getDirectory(GpsDirectory.class); } catch (CompoundException p) { e.setExifCoor(null); e.setPos(null); return; } try { // longitude Rational[] components = dir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE); deg = components[0].doubleValue(); min = components[1].doubleValue(); sec = components[2].doubleValue(); if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) throw new IllegalArgumentException(); lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); if (dir.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') { lon = -lon; } // latitude components = dir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE); deg = components[0].doubleValue(); min = components[1].doubleValue(); sec = components[2].doubleValue(); if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) throw new IllegalArgumentException(); lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); if (Double.isNaN(lat)) throw new IllegalArgumentException(); if (dir.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') { lat = -lat; } // Store values e.setExifCoor(new LatLon(lat, lon)); e.setPos(e.getExifCoor()); } catch (CompoundException p) { // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) try { Double longitude = dir.getDouble(GpsDirectory.TAG_GPS_LONGITUDE); Double latitude = dir.getDouble(GpsDirectory.TAG_GPS_LATITUDE); if (longitude == null || latitude == null) throw new CompoundException(""); // Store values e.setExifCoor(new LatLon(latitude, longitude)); e.setPos(e.getExifCoor()); } catch (CompoundException ex) { e.setExifCoor(null); e.setPos(null); } } catch (Exception ex) { // (other exceptions, e.g. #5271) System.err.println("Error when reading EXIF from file: " + ex); e.setExifCoor(null); e.setPos(null); } // compass direction value Rational direction = null; try { direction = dir.getRational(GpsDirectory.TAG_GPS_IMG_DIRECTION); if (direction != null) { e.setExifImgDir(direction.doubleValue()); } } catch (Exception ex) { // (CompoundException and other exceptions, e.g. #5271) // Do nothing } }