List of usage examples for org.apache.commons.imaging.formats.jpeg JpegImageMetadata getExif
public TiffImageMetadata getExif()
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 ww .jav a 2s .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 . ja va 2 s .c om*/ * @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.openstreetmap.josm.plugins.mapillary.io.export.MapillaryExportWriterThread.java
@Override public void run() { this.monitor.setCustomText("Downloaded 0/" + this.amount); BufferedImage img;/*from w ww.ja va 2 s . co m*/ MapillaryAbstractImage mimg; String finalPath = ""; for (int i = 0; i < this.amount; i++) { try { img = this.queue.take(); mimg = this.queueImages.take(); if (img == null || mimg == null) throw new IllegalStateException("Null image"); if (this.path == null && mimg instanceof MapillaryImportedImage) { String path = ((MapillaryImportedImage) mimg).getFile().getPath(); finalPath = path.substring(0, path.lastIndexOf('.')); } else if (mimg instanceof MapillaryImage) { finalPath = this.path + '/' + ((MapillaryImage) mimg).getKey(); } else if (mimg instanceof MapillaryImportedImage) { finalPath = this.path + '/' + ((MapillaryImportedImage) mimg).getFile().getName(); } // Transforms the image into a byte array. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", outputStream); byte[] imageBytes = outputStream.toByteArray(); // Write EXIF tags TiffOutputSet outputSet = null; TiffOutputDirectory exifDirectory; TiffOutputDirectory gpsDirectory; // If the image is imported, loads the rest of the EXIF data. if (mimg instanceof MapillaryImportedImage) { final ImageMetadata metadata = Imaging.getMetadata(((MapillaryImportedImage) mimg).getFile()); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { outputSet = exif.getOutputSet(); } } } if (null == outputSet) { outputSet = new TiffOutputSet(); } exifDirectory = outputSet.getOrCreateExifDirectory(); gpsDirectory = outputSet.getOrCreateGPSDirectory(); gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF); gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF, GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF_VALUE_TRUE_NORTH); gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(mimg.getMovingCa())); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); if (mimg instanceof MapillaryImportedImage) { exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, mimg.getDate("yyyy/MM/dd HH:mm:ss")); } else if (mimg instanceof MapillaryImage) { exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, mimg.getDate("yyyy/MM/dd HH/mm/ss")); } outputSet.setGPSInDegrees(mimg.getMovingLatLon().lon(), mimg.getMovingLatLon().lat()); OutputStream os = new BufferedOutputStream(new FileOutputStream(finalPath + ".jpg")); new ExifRewriter().updateExifMetadataLossless(imageBytes, os, outputSet); os.close(); } catch (InterruptedException e) { Main.info("Mapillary export cancelled"); return; } catch (IOException | ImageReadException | ImageWriteException e) { Main.error(e); } // Increases the progress bar. this.monitor.worked(PleaseWaitProgressMonitor.PROGRESS_BAR_MAX / this.amount); this.monitor.setCustomText("Downloaded " + (i + 1) + "/" + this.amount); } }
From source file:org.openstreetmap.josm.plugins.mapillary.oauth.UploadUtils.java
/** * Returns a file containing the picture and an updated version of the EXIF * tags.// ww w .j av a 2 s.c o m * * @param image The image to be uploaded * @return A File object containing the picture and an updated version of the * EXIF tags. * @throws ImageReadException if there are errors reading the image from the file. * @throws IOException if there are errors getting the metadata from the file or writing * the output. * @throws ImageWriteException if there are errors writing the image in the file. */ static File updateFile(MapillaryImportedImage image) throws ImageReadException, IOException, ImageWriteException { TiffOutputSet outputSet = null; TiffOutputDirectory exifDirectory; TiffOutputDirectory gpsDirectory; TiffOutputDirectory rootDirectory; // If the image is imported, loads the rest of the EXIF data. JpegImageMetadata jpegMetadata = null; try { ImageMetadata metadata = Imaging.getMetadata(image.getFile()); jpegMetadata = (JpegImageMetadata) metadata; } catch (Exception e) { Main.warn(e); } if (null != jpegMetadata) { final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { outputSet = exif.getOutputSet(); } } if (null == outputSet) { outputSet = new TiffOutputSet(); } gpsDirectory = outputSet.getOrCreateGPSDirectory(); exifDirectory = outputSet.getOrCreateExifDirectory(); rootDirectory = outputSet.getOrCreateRootDirectory(); gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF); gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF, GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF_VALUE_TRUE_NORTH); gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(image.getMovingCa())); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, image.getDate("yyyy/MM/dd HH:mm:ss")); // Removes the ImageDescription tag, that causes problems in the upload. rootDirectory.removeField(TiffTagConstants.TIFF_TAG_IMAGE_DESCRIPTION); outputSet.setGPSInDegrees(image.getMovingLatLon().lon(), image.getMovingLatLon().lat()); File tempFile = File.createTempFile("imagetoupload_" + c, ".tmp"); c++; OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile)); // Transforms the image into a byte array. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image.getImage(), "jpg", outputStream); byte[] imageBytes = outputStream.toByteArray(); new ExifRewriter().updateExifMetadataLossless(imageBytes, os, outputSet); os.close(); return tempFile; }