List of usage examples for org.apache.commons.imaging.formats.tiff.write TiffOutputSet TiffOutputSet
public TiffOutputSet()
From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyWriter.java
public static void setExifGPSTag(File jpegImageFile, File jpegImageOutputFile, Coordinates coordinates) throws IOException, ImageReadException, ImageWriteException { OutputStream os = null;// ww w. ja va 2 s. com boolean canThrow = false; try { TiffOutputSet outputSet = null; final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (jpegMetadata != null) { final TiffImageMetadata exif = jpegMetadata.getExif(); if (exif != null) { outputSet = exif.getOutputSet(); } } if (outputSet == null) { outputSet = new TiffOutputSet(); } outputSet.setGPSInDegrees(coordinates.getLongitude(), coordinates.getLatitude()); outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF); outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF, GpsTagConstants.GPS_TAG_GPS_DEST_BEARING_REF_VALUE_TRUE_NORTH); outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, new RationalNumber[] { RationalNumber.valueOf(coordinates.getDirection()) }); //outputSet.getRootDirectory().removeField(305); os = new FileOutputStream(jpegImageOutputFile); os = new BufferedOutputStream(os); new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); canThrow = true; } finally { IoUtils.closeQuietly(canThrow, os); } }
From source file:com.hygenics.imaging.ImageCleanup.java
public byte[] writeMetaData(String data, byte[] ibytes) { // write metadata based on the metadata columns list TiffOutputSet outset = null;/*ww w. j a v a 2s . c o m*/ BufferedImage bi; ImageMetadata metadata; ByteArrayOutputStream bos = null; try { // get the buffered image to write to bi = Imaging.getBufferedImage(ibytes); metadata = Imaging.getMetadata(ibytes); JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // get the image exif data TiffImageMetadata exif = jpegMetadata.getExif(); outset = exif.getOutputSet(); } if (outset == null) { // get a new set (directory structured to write to) outset = new TiffOutputSet(); } TiffOutputDirectory exdir = outset.getOrCreateExifDirectory(); exdir.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT); exdir.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, data.trim()); bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ibytes); ExifRewriter exrw = new ExifRewriter(); // read to a byte stream exrw.updateExifMetadataLossy(bis, bos, outset); // read the input from the byte buffer ibytes = bos.toByteArray(); bis.close(); bos.close(); } catch (ImageReadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ImageWriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bos != null) { try { bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return ibytes; }
From source file:com.ubb.imaging.ExifMetadataWriter.java
/** * This method illustrates how to add/update EXIF metadata in a JPEG file. * //from w ww . j a v a 2 s . co m * @param srcFile * A source image file. * @param imageId * @param dstFile * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void changeExifMetadata(File srcFile, File dstFile, String imageId) 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 = (ImageMetadata) Imaging.getMetadata(srcFile); 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. // // see // org.apache.commons.imaging.formats.tiff.constants.AllTagConstants // final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory(); updateImageUniqueId(exifDirectory, imageId); updateDocumentName(exifDirectory, imageId); updateUserComments(exifDirectory); updateImageDescription(exifDirectory, LINK_TO_RESOURCE.concat(imageId)); // AllTagConstants.TIFF_TAG_IMAGE_DESCRIPTION // AllTagConstants.TIFF_TAG_DOCUMENT_NAME // AllTagConstants.TIFF_TAG_COPYRIGHT } os = new BufferedOutputStream(new FileOutputStream(dstFile)); new ExifRewriter().updateExifMetadataLossless(srcFile, os, outputSet); canThrow = true; } finally { IoUtils.closeQuietly(canThrow, os); //delete the source file srcFile.delete(); //move the new file with new metadata to the source file path (it's like copy and delete). FileUtils.moveFile(dstFile, srcFile); } }
From source file:at.ac.tuwien.qse.sepm.dao.repo.impl.JpegSerializer.java
@Override public void update(InputStream is, OutputStream os, PhotoMetadata metadata) throws DAOException { if (is == null) throw new IllegalArgumentException(); if (os == null) throw new IllegalArgumentException(); if (metadata == null) throw new IllegalArgumentException(); LOGGER.debug("updating photo metadata {}", metadata); String tags = "travelimg"; for (Tag element : metadata.getTags()) { tags += "/" + element.getName(); }/*from w w w .j a va2 s. c om*/ Rating rating = metadata.getRating(); tags += "/rating|" + rating; Place place = metadata.getPlace(); if (place != null) { tags += "/place|" + place.getCity() + "|" + place.getCountry() + "|" + place.getLatitude() + "|" + place.getLongitude(); } Journey journey = metadata.getJourney(); if (journey != null) { tags += "/journey|" + journey.getName() + "|" + journey.getStartDate().format(DATE_FORMATTER) + "|" + journey.getEndDate().format(DATE_FORMATTER); } Photographer photographer = metadata.getPhotographer(); if (photographer != null) { tags += "/photographer|" + photographer.getName(); } try { is.mark(Integer.MAX_VALUE); ImageMetadata imageData = Imaging.getMetadata(is, null); if (imageData == null) { LOGGER.debug("could not find image metadata"); throw new DAOException("No metadata found."); } if (!(imageData instanceof JpegImageMetadata)) { LOGGER.debug("metadata is of unknown type"); throw new DAOException("Metadata is of unknown type."); } JpegImageMetadata jpegData = (JpegImageMetadata) imageData; TiffOutputSet outputSet = new TiffOutputSet(); TiffImageMetadata exifData = jpegData.getExif(); if (exifData != null) { outputSet = exifData.getOutputSet(); } TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory(); outputSet.setGPSInDegrees(metadata.getLongitude(), metadata.getLatitude()); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, DATE_FORMATTER.format(metadata.getDatetime())); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT); exifDirectory.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, tags); is.reset(); new ExifRewriter().updateExifMetadataLossless(is, os, outputSet); } catch (IOException | ImageReadException | ImageWriteException ex) { LOGGER.warn("failed updating metadata"); throw new DAOException(ex); } LOGGER.debug("updated photo metadata"); }
From source file:joachimeichborn.geotag.io.jpeg.PictureMetadataWriter.java
private TiffOutputSet getOutputSet(final Path aFile) throws ImageReadException, IOException, ImageWriteException { final ImageMetadata metadata = Imaging.getMetadata(aFile.toFile()); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { final TiffImageMetadata exif = jpegMetadata.getExif(); if (exif != null) { return exif.getOutputSet(); }// w w w . ja va 2 s .c om } return new TiffOutputSet(); }
From source file:com.ubb.imaging.ExifMetadataWriter.java
/** * This method illustrates how to set the GPS values in JPEG EXIF metadata. * // w w w . ja v a 2 s.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:net.tourbook.tour.photo.TourPhotoManager.java
/** * This example illustrates how to set the GPS values in JPEG EXIF metadata. * /*from www . j a v a 2 s.com*/ * @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.WriteExifMetadataExample.java
/** * This example illustrates how to add/update EXIF metadata in a JPEG file. * /*from w w w.j a v a 2 s. c om*/ * @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 set the GPS values in JPEG EXIF metadata. * //w w w . java 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 ww w . j av a 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); } }