public static Integer readImageOrientation(File imageFile) { Metadata metadata = null; try { metadata = ImageMetadataReader.readMetadata(imageFile); } catch (Exception e) { // Any exception here is handled as if image // format doesn't support EXIF return null; } Directory directory = metadata.getDirectory(ExifIFD0Directory.class); JpegDirectory jpegDirectory = (JpegDirectory) metadata.getDirectory(JpegDirectory.class); int orientation = 1; try { orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION); /*int width = jpegDirectory.getImageWidth(); int height = jpegDirectory.getImageHeight();*/ } catch (Exception e) { Logger.warn("Could not get orientation"); } return orientation; }
@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); }
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; }
// ---------------------------------------------------------------------------------------------------- private String _readExifDateStr(File jpegFile) { try { Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); ExifSubIFDDirectory dir = metadata.getDirectory(ExifSubIFDDirectory.class); Date d = dir.getDate(36868); return m_sdf.format(d); } catch (Exception e) { return "_0000-00-00_"; } }
// ---------------------------------------------------------------------------------------------------- private long _readExifDate(File jpegFile) { try { Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); ExifSubIFDDirectory dir = metadata.getDirectory(ExifSubIFDDirectory.class); Date d = dir.getDate(36868); return d.getTime(); } catch (Exception e) { return -1; } }
/** * Performs the Exif data extraction, adding found values to the specified instance of <code> * Metadata</code>. */ public Metadata extract(Metadata metadata) { if (_data == null) { return metadata; } Directory directory = metadata.getDirectory(IptcDirectory.class); // find start of data int offset = 0; try { while (offset < _data.length - 1 && get32Bits(offset) != 0x1c02) { offset++; } } catch (MetadataException e) { directory.addError("Couldn't find start of Iptc data (invalid segment)"); return metadata; } // for each tag while (offset < _data.length) { // identifies start of a tag if (_data[offset] != 0x1c) { break; } // we need at least five bytes left to read a tag if ((offset + 5) >= _data.length) { break; } offset++; int directoryType; int tagType; int tagByteCount; try { directoryType = _data[offset++]; tagType = _data[offset++]; tagByteCount = get32Bits(offset); } catch (MetadataException e) { directory.addError("Iptc data segment ended mid-way through tag descriptor"); return metadata; } offset += 2; if ((offset + tagByteCount) > _data.length) { directory.addError("data for tag extends beyond end of iptc segment"); break; } processTag(directory, directoryType, tagType, offset, tagByteCount); offset += tagByteCount; } return metadata; }
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); } } }
private void scanPictures(String filePath, boolean shared) { File file = new File(filePath); ContentValues values = new ContentValues(); String mime = "image/" + FilenameUtils.getExtension(filePath); fillCommonValues(values, Constants.FILE_TYPE_PICTURES, filePath, file, mime, shared); try { Metadata metadata = ImageMetadataReader.readMetadata(file); ExifIFD0Directory dir = metadata.getDirectory(ExifIFD0Directory.class); ExifIFD0Descriptor desc = new ExifIFD0Descriptor(dir); String title = desc.getWindowsTitleDescription(); if (StringUtils.isNullOrEmpty(title, true)) { title = FilenameUtils.getBaseName(file.getName()); } String artist = desc.getWindowsAuthorDescription(); if (StringUtils.isNullOrEmpty(artist, true)) { artist = dir.getString(ExifIFD0Directory.TAG_ARTIST, "UTF-8"); } if (StringUtils.isNullOrEmpty(artist, true)) { artist = ""; } String album = ""; String year = dir.getString(ExifIFD0Directory.TAG_DATETIME); if (StringUtils.isNullOrEmpty(year, true)) { year = ""; } values.put(Columns.TITLE, title); values.put(Columns.ARTIST, artist); values.put(Columns.ALBUM, album); values.put(Columns.YEAR, year); } catch (Throwable e) { String displayName = FilenameUtils.getBaseName(file.getName()); values.put(Columns.TITLE, displayName); values.put(Columns.ARTIST, ""); values.put(Columns.ALBUM, ""); values.put(Columns.YEAR, ""); } ShareFilesDB db = ShareFilesDB.intance(); db.insert(values); }
/* (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()); } }
/** * Tries to read the orientation data from the given image {@link Metadata}. Returns <code>null * </code> if not found or if an error occurs. */ @Nullable public static ImageOrientation getOrientation(@Nullable final Metadata metadata) { if (metadata != null) { final Directory exifIfd0Directory = metadata.getDirectory(ExifIFD0Directory.class); if (exifIfd0Directory != null) { try { return findById(exifIfd0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION)); } catch (Exception e) { LOG.info( "ImageOrientation.getOrientation(): Exception while trying to read the orientation data from the EXIF. Ignoring and just returning null"); } } } return null; }
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 + ")"); }
protected final Media buildMedia(final MediaUploadBatch context) throws MediaReaderException, IOException { final String fileName = getFile().getAbsolutePath(); try { final Metadata rawMetadata = ImageMetadataReader.readMetadata(getFile()); final ExifThumbnailDirectory thumbnailDirectory = rawMetadata.getDirectory(ExifThumbnailDirectory.class); final byte[] thumbnail; if (thumbnailDirectory != null && thumbnailDirectory.hasThumbnailData()) { thumbnail = thumbnailDirectory.getThumbnailData(); } else { thumbnail = new byte[0]; } final Map<String, Object> metadata = new HashMap<>(rawMetadata.getDirectoryCount()); for (final Directory directory : rawMetadata.getDirectories()) { copy(directory, metadata); preProcess(context, directory, metadata); } return new Media(getFile(), fileName, context.getTemplate(), metadata, thumbnail); } catch (final ImageProcessingException e) { throw new MediaReaderException(e); } }
/** * @param filename * @return */ private HashMap<String, String> extractExifDatas(String filename) { HashMap<String, String> datas = new HashMap<String, String>(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH'h'mm.ss"); try { File jpegFile = new File(filename); Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); // reading exif ExifSubIFDDirectory directory = metadata.getDirectory(ExifSubIFDDirectory.class); if (directory != null) { Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL); if (date != null) { datas.put("date", formatter.format(date)); } } } catch (JpegProcessingException e) { System.err.println("Problem during datas extraction : " + e.getMessage()); } catch (IOException e) { System.err.println("Problem during datas extraction" + e.getMessage()); } catch (ImageProcessingException e) { System.err.println("Problem during datas extraction" + e.getMessage()); } return datas; }
/** * Constructor call with a parameter needed * * @param jpegFile * @throws ImageProcessingException * @throws IOException */ public EXIFMetadataExtractor(File jpegFile) throws ImageProcessingException, IOException { this.metadata = ImageMetadataReader.readMetadata(jpegFile); this.directory = (ExifSubIFDDirectory) metadata.getDirectory(ExifSubIFDDirectory.class); }
/** * Reads filename and store in private variables * * @param filename * @throws ImageProcessingException * @throws IOException */ public void readImageFile(String filename) throws ImageProcessingException, IOException { File jpegFile = new File(filename); this.metadata = ImageMetadataReader.readMetadata(jpegFile); this.directory = (ExifSubIFDDirectory) metadata.getDirectory(ExifSubIFDDirectory.class); }
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 } }