List of usage examples for org.apache.commons.imaging Imaging getMetadata
public static IImageMetadata getMetadata(final File file) throws ImageReadException, IOException
From source file:com.ubb.imaging.ExifMetadataWriter.java
/** * This method illustrates how to set the GPS values in JPEG EXIF metadata. * /*from w ww . j a v a 2s.co m*/ * @param jpegImageFile * A source image file. * @param dst * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void setExifGPSTag(final File jpegImageFile, final File dst) throws IOException, ImageReadException, ImageWriteException { OutputStream os = null; boolean canThrow = false; try { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } // if file does not contain any exif metadata, we create an empty // set of exif metadata. Otherwise, we keep all of the other // existing tags. if (null == outputSet) { outputSet = new TiffOutputSet(); } { // Example of how to add/update GPS info to output set. // New York City final double longitude = -74.0; // 74 degrees W (in Degrees East) final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees // North) outputSet.setGPSInDegrees(longitude, latitude); } os = new FileOutputStream(dst); os = new BufferedOutputStream(os); new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); canThrow = true; } finally { IoUtils.closeQuietly(canThrow, os); } }
From source file:com.cons.gps2exif.exif.ReadMetaData.java
public static void printMetaData(final File file) throws ImageReadException, IOException, IllegalArgumentException, IllegalAccessException { // get all metadata stored in EXIF format (ie. from JPEG or TIFF). ImageMetadata metadata = Imaging.getMetadata(file); if (!(metadata instanceof JpegImageMetadata)) { throw new RuntimeException("Only support " + JpegImageMetadata.class.getSimpleName()); }//from w w w . ja v a 2s. c o m JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; // JPEG EXIF metadata List<Field> exifTagfields = getTagInfoDefinedInClass(ExifTagConstants.class); System.out.println("=== " + ExifTagConstants.class.getSimpleName() + " ==="); printTagInfos(exifTagfields, jpegMetadata); // Tiff metadata List<Field> tiffTagfields = getTagInfoDefinedInClass(TiffTagConstants.class); System.out.println("=== " + TiffTagConstants.class.getSimpleName() + " ==="); printTagInfos(tiffTagfields, jpegMetadata); // Gps metadata List<Field> gpsTagfields = getTagInfoDefinedInClass(GpsTagConstants.class); System.out.println("=== " + GpsTagConstants.class.getSimpleName() + " ==="); printTagInfos(gpsTagfields, jpegMetadata); // simple interface to EXIF jpegMetadata TiffImageMetadata exifMetadata = jpegMetadata.getExif(); if (null != exifMetadata) { System.out.println("=== ExifMetadata ==="); jpegMetadata.getExif().getAllFields().stream().forEach(System.out::println); System.out.println(); } // All IImageMetadataItem List<ImageMetadataItem> items = jpegMetadata.getItems(); printIImageMetadataItems(items); }
From source file:net.mozq.picto.core.ProcessCore.java
private static ImageMetadata getImageMetadata(Path imagePath) throws IOException { File imageFile = imagePath.toFile(); ImageMetadata imageMetadata;//from ww w . ja va 2 s . c om try { imageMetadata = Imaging.getMetadata(imageFile); } catch (ImageReadException e) { imageMetadata = null; } return imageMetadata; }
From source file:net.tourbook.tour.photo.TourPhotoManager.java
/** * This example illustrates how to set the GPS values in JPEG EXIF metadata. * //from ww w. ja v a 2 s.c om * @param jpegImageFile * A source image file. * @param destinationFile * The output file. * @param latitude * @param longitude * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ private void setExifGPSTag_IntoImageFile_WithExifRewriter(final File jpegImageFile, final File destinationFile, final double latitude, final double longitude) throws IOException, ImageReadException, ImageWriteException { OutputStream os = null; try { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } // if file does not contain any exif metadata, we create an empty // set of exif metadata. Otherwise, we keep all of the other // existing tags. if (null == outputSet) { outputSet = new TiffOutputSet(); } { // Example of how to add/update GPS info to output set. // New York City // final double longitude = -74.0; // 74 degrees W (in Degrees East) // final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees // North) outputSet.setGPSInDegrees(longitude, latitude); } os = new FileOutputStream(destinationFile); os = new BufferedOutputStream(os); /** * the lossless method causes an exception after 3 times writing the image file, * therefore the lossy method is used * * <pre> * * org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter$ExifOverflowException: APP1 Segment is too long: 65564 * at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.writeSegmentsReplacingExif(ExifRewriter.java:552) * at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:393) * at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:293) * at net.tourbook.photo.PhotosAndToursView.setExifGPSTag_IntoPhoto(PhotosAndToursView.java:2309) * at net.tourbook.photo.PhotosAndToursView.setExifGPSTag(PhotosAndToursView.java:2141) * * </pre> */ // new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); // new ExifRewriter().updateExifMetadataLossy(jpegImageFile, os, outputSet); os.close(); os = null; } finally { if (os != null) { try { os.close(); } catch (final IOException e) { } } } }
From source file:org.apache.commons.imaging.examples.MetadataExample.java
public static void metadataExample(final File file) throws ImageReadException, IOException { // get all metadata stored in EXIF format (ie. from JPEG or TIFF). final ImageMetadata metadata = Imaging.getMetadata(file); // System.out.println(metadata); if (metadata instanceof JpegImageMetadata) { final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; // Jpeg EXIF metadata is stored in a TIFF-based directory structure // and is identified with TIFF tags. // Here we look for the "x resolution" tag, but // we could just as easily search for any other tag. //// w ww . ja va 2 s . co m // see the TiffConstants file for a list of TIFF tags. System.out.println("file: " + file.getPath()); // print out various interesting EXIF tags. printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_XRESOLUTION); printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_ISO); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_APERTURE_VALUE); printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE); printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF); printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE); printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF); printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE); System.out.println(); // simple interface to GPS data final TiffImageMetadata exifMetadata = jpegMetadata.getExif(); if (null != exifMetadata) { final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS(); if (null != gpsInfo) { final String gpsDescription = gpsInfo.toString(); final double longitude = gpsInfo.getLongitudeAsDegreesEast(); final double latitude = gpsInfo.getLatitudeAsDegreesNorth(); System.out.println(" " + "GPS Description: " + gpsDescription); System.out.println(" " + "GPS Longitude (Degrees East): " + longitude); System.out.println(" " + "GPS Latitude (Degrees North): " + latitude); } } // more specific example of how to manually access GPS values final TiffField gpsLatitudeRefField = jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF); final TiffField gpsLatitudeField = jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE); final TiffField gpsLongitudeRefField = jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF); final TiffField gpsLongitudeField = jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE); if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null && gpsLongitudeField != null) { // all of these values are strings. final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue(); final RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField.getValue()); final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue(); final RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField.getValue(); final RationalNumber gpsLatitudeDegrees = gpsLatitude[0]; final RationalNumber gpsLatitudeMinutes = gpsLatitude[1]; final RationalNumber gpsLatitudeSeconds = gpsLatitude[2]; final RationalNumber gpsLongitudeDegrees = gpsLongitude[0]; final RationalNumber gpsLongitudeMinutes = gpsLongitude[1]; final RationalNumber gpsLongitudeSeconds = gpsLongitude[2]; // This will format the gps info like so: // // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E System.out.println(" " + "GPS Latitude: " + gpsLatitudeDegrees.toDisplayString() + " degrees, " + gpsLatitudeMinutes.toDisplayString() + " minutes, " + gpsLatitudeSeconds.toDisplayString() + " seconds " + gpsLatitudeRef); System.out.println(" " + "GPS Longitude: " + gpsLongitudeDegrees.toDisplayString() + " degrees, " + gpsLongitudeMinutes.toDisplayString() + " minutes, " + gpsLongitudeSeconds.toDisplayString() + " seconds " + gpsLongitudeRef); } System.out.println(); final List<ImageMetadataItem> items = jpegMetadata.getItems(); for (int i = 0; i < items.size(); i++) { final ImageMetadataItem item = items.get(i); System.out.println(" " + "item: " + item); } System.out.println(); } }
From source file:org.apache.commons.imaging.examples.SampleUsage.java
@SuppressWarnings("unused") public SampleUsage() { try {// ww w . j a v a2 s . c o m // <b>Code won't work unless these variables are properly // initialized. // Imaging works equally well with File, byte array or InputStream // inputs.</b> final BufferedImage someImage = null; final byte someBytes[] = null; final File someFile = null; final InputStream someInputStream = null; final OutputStream someOutputStream = null; // <b>The Imaging class provides a simple interface to the library. // </b> // <b>how to read an image: </b> final byte imageBytes[] = someBytes; final BufferedImage image_1 = Imaging.getBufferedImage(imageBytes); // <b>methods of Imaging usually accept files, byte arrays, or // inputstreams as arguments. </b> final BufferedImage image_2 = Imaging.getBufferedImage(imageBytes); final File file = someFile; final BufferedImage image_3 = Imaging.getBufferedImage(file); final InputStream is = someInputStream; final BufferedImage image_4 = Imaging.getBufferedImage(is); // <b>Write an image. </b> final BufferedImage image = someImage; final File dst = someFile; final ImageFormat format = ImageFormats.PNG; final Map<String, Object> optionalParams = new HashMap<>(); Imaging.writeImage(image, dst, format, optionalParams); final OutputStream os = someOutputStream; Imaging.writeImage(image, os, format, optionalParams); // <b>get the image's embedded ICC Profile, if it has one. </b> final byte iccProfileBytes[] = Imaging.getICCProfileBytes(imageBytes); final ICC_Profile iccProfile = Imaging.getICCProfile(imageBytes); // <b>get the image's width and height. </b> final Dimension d = Imaging.getImageSize(imageBytes); // <b>get all of the image's info (ie. bits per pixel, size, // transparency, etc.) </b> final ImageInfo imageInfo = Imaging.getImageInfo(imageBytes); if (imageInfo.getColorType() == ImageInfo.ColorType.GRAYSCALE) { System.out.println("Grayscale image."); } if (imageInfo.getHeight() > 1000) { System.out.println("Large image."); } // <b>try to guess the image's format. </b> final ImageFormat imageFormat = Imaging.guessFormat(imageBytes); imageFormat.equals(ImageFormats.PNG); // <b>get all metadata stored in EXIF format (ie. from JPEG or // TIFF). </b> final ImageMetadata metadata = Imaging.getMetadata(imageBytes); // <b>print a dump of information about an image to stdout. </b> Imaging.dumpImageFile(imageBytes); // <b>get a summary of format errors. </b> final FormatCompliance formatCompliance = Imaging.getFormatCompliance(imageBytes); } catch (final Exception e) { } }
From source file:org.apache.commons.imaging.examples.WriteExifMetadataExample.java
/** * This example illustrates how to add/update EXIF metadata in a JPEG file. * // w w w . j a v a 2s .c o m * @param jpegImageFile * A source image file. * @param dst * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void changeExifMetadata(final File jpegImageFile, final File dst) throws IOException, ImageReadException, ImageWriteException { try (FileOutputStream fos = new FileOutputStream(dst); OutputStream os = new BufferedOutputStream(fos);) { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } // if file does not contain any exif metadata, we create an empty // set of exif metadata. Otherwise, we keep all of the other // existing tags. if (null == outputSet) { outputSet = new TiffOutputSet(); } { // Example of how to add a field/tag to the output set. // // Note that you should first remove the field/tag if it already // exists in this directory, or you may end up with duplicate // tags. See above. // // Certain fields/tags are expected in certain Exif directories; // Others can occur in more than one directory (and often have a // different meaning in different directories). // // TagInfo constants often contain a description of what // directories are associated with a given tag. // final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory(); // make sure to remove old value if present (this method will // not fail if the tag does not exist). exifDirectory.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE); exifDirectory.add(ExifTagConstants.EXIF_TAG_APERTURE_VALUE, new RationalNumber(3, 10)); } { // Example of how to add/update GPS info to output set. // New York City final double longitude = -74.0; // 74 degrees W (in Degrees East) final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees // North) outputSet.setGPSInDegrees(longitude, latitude); } // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME); new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); } }
From source file:org.apache.commons.imaging.examples.WriteExifMetadataExample.java
/** * This example illustrates how to remove a tag (if present) from EXIF * metadata in a JPEG file./*w w w .j ava 2 s. c o m*/ * * In this case, we remove the "aperture" tag from the EXIF metadata if * present. * * @param jpegImageFile * A source image file. * @param dst * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void removeExifTag(final File jpegImageFile, final File dst) throws IOException, ImageReadException, ImageWriteException { try (FileOutputStream fos = new FileOutputStream(dst); OutputStream os = new BufferedOutputStream(fos)) { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } if (null == outputSet) { // file does not contain any exif metadata. We don't need to // update the file; just copy it. FileUtils.copyFile(jpegImageFile, dst); return; } { // Example of how to remove a single tag/field. // There are two ways to do this. // Option 1: brute force // Note that this approach is crude: Exif data is organized in // directories. The same tag/field may appear in more than one // directory, and have different meanings in each. outputSet.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE); // Option 2: precision // We know the exact directory the tag should appear in, in this // case the "exif" directory. // One complicating factor is that in some cases, manufacturers // will place the same tag in different directories. // To learn which directory a tag appears in, either refer to // the constants in ExifTagConstants.java or go to Phil Harvey's // EXIF website. final TiffOutputDirectory exifDirectory = outputSet.getExifDirectory(); if (null != exifDirectory) { exifDirectory.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE); } } new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); } }
From source file:org.apache.commons.imaging.examples.WriteExifMetadataExample.java
/** * This example illustrates how to set the GPS values in JPEG EXIF metadata. * // w w w. j a v a2 s . com * @param jpegImageFile * A source image file. * @param dst * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void setExifGPSTag(final File jpegImageFile, final File dst) throws IOException, ImageReadException, ImageWriteException { try (FileOutputStream fos = new FileOutputStream(dst); OutputStream os = new BufferedOutputStream(fos)) { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } // if file does not contain any exif metadata, we create an empty // set of exif metadata. Otherwise, we keep all of the other // existing tags. if (null == outputSet) { outputSet = new TiffOutputSet(); } { // Example of how to add/update GPS info to output set. // New York City final double longitude = -74.0; // 74 degrees W (in Degrees East) final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees // North) outputSet.setGPSInDegrees(longitude, latitude); } new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); } }
From source file:org.imaging.CommonsImagingVariousExamples.java
public CommonsImagingVariousExamples() { try {//from w ww .j av a 2 s .c o m // <b>Code won't work unless these variables are properly // initialized. // Imaging works equally well with File, byte array or InputStream // inputs.</b> final BufferedImage someImage = null; final byte someBytes[] = null; final File someFile = null; final InputStream someInputStream = null; final OutputStream someOutputStream = null; // <b>The Imaging class provides a simple interface to the library. // </b> // <b>how to read an image: </b> final byte imageBytes[] = someBytes; final BufferedImage image_1 = Imaging.getBufferedImage(imageBytes); // <b>methods of Imaging usually accept files, byte arrays, or // inputstreams as arguments. </b> final BufferedImage image_2 = Imaging.getBufferedImage(imageBytes); final File file = someFile; final BufferedImage image_3 = Imaging.getBufferedImage(file); final InputStream is = someInputStream; final BufferedImage image_4 = Imaging.getBufferedImage(is); // <b>Write an image. </b> final BufferedImage image = someImage; final File dst = someFile; final ImageFormat format = ImageFormats.PNG; final Map<String, Object> optionalParams = new HashMap<String, Object>(); Imaging.writeImage(image, dst, format, optionalParams); final OutputStream os = someOutputStream; Imaging.writeImage(image, os, format, optionalParams); // <b>get the image's embedded ICC Profile, if it has one. </b> final byte iccProfileBytes[] = Imaging.getICCProfileBytes(imageBytes); final ICC_Profile iccProfile = Imaging.getICCProfile(imageBytes); // <b>get the image's width and height. </b> final Dimension d = Imaging.getImageSize(imageBytes); // <b>get all of the image's info (ie. bits per pixel, size, // transparency, etc.) </b> final ImageInfo imageInfo = Imaging.getImageInfo(imageBytes); if (imageInfo.getColorType() == ImageInfo.ColorType.GRAYSCALE) { System.out.println("Grayscale image."); } if (imageInfo.getHeight() > 1000) { System.out.println("Large image."); } // <b>try to guess the image's format. </b> final ImageFormat imageFormat = Imaging.guessFormat(imageBytes); imageFormat.equals(ImageFormats.PNG); // <b>get all metadata stored in EXIF format (ie. from JPEG or // TIFF). </b> final ImageMetadata metadata = Imaging.getMetadata(imageBytes); // <b>print a dump of information about an image to stdout. </b> Imaging.dumpImageFile(imageBytes); // <b>get a summary of format errors. </b> final FormatCompliance formatCompliance = Imaging.getFormatCompliance(imageBytes); } catch (final Exception e) { e.printStackTrace(); } }