List of usage examples for org.apache.commons.imaging.formats.tiff.constants GpsTagConstants GPS_TAG_GPS_IMG_DIRECTION
TagInfoRational GPS_TAG_GPS_IMG_DIRECTION
To view the source code for org.apache.commons.imaging.formats.tiff.constants GpsTagConstants GPS_TAG_GPS_IMG_DIRECTION.
Click Source Link
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;//from w ww. j a v a 2 s . c o m 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:net.tourbook.photo.Photo.java
/** * Creates metadata from image metadata//from w w w. j a v a 2s. co m * * @param imageMetadata * Can be <code>null</code> when not available * @return */ private PhotoImageMetadata createPhotoMetadata(final IImageMetadata imageMetadata) { final PhotoImageMetadata photoMetadata = new PhotoImageMetadata(); /* * read meta data for this photo */ if (imageMetadata instanceof TiffImageMetadata) { photoMetadata.isExifFromImage = true; final TiffImageMetadata tiffMetadata = (TiffImageMetadata) imageMetadata; photoMetadata.exifDateTime = getTiffValueDate(tiffMetadata); photoMetadata.orientation = 1; photoMetadata.imageWidth = getTiffValueInt(tiffMetadata, TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, Integer.MIN_VALUE); photoMetadata.imageHeight = getTiffValueInt(tiffMetadata, TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, Integer.MIN_VALUE); photoMetadata.model = getTiffValueString(tiffMetadata, TiffTagConstants.TIFF_TAG_MODEL); } else if (imageMetadata instanceof JpegImageMetadata) { photoMetadata.isExifFromImage = true; final JpegImageMetadata jpegMetadata = (JpegImageMetadata) imageMetadata; photoMetadata.exifDateTime = getExifValueDate(jpegMetadata); photoMetadata.orientation = getExifValueInt(jpegMetadata, TiffTagConstants.TIFF_TAG_ORIENTATION, 1); photoMetadata.imageWidth = getExifValueInt(jpegMetadata, ExifTagConstants.EXIF_TAG_EXIF_IMAGE_WIDTH, Integer.MIN_VALUE); photoMetadata.imageHeight = getExifValueInt(jpegMetadata, ExifTagConstants.EXIF_TAG_EXIF_IMAGE_LENGTH, Integer.MIN_VALUE); photoMetadata.imageDirection = getExifValueDouble(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); photoMetadata.altitude = getExifValueDouble(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_ALTITUDE); photoMetadata.model = getExifValueString(jpegMetadata, TiffTagConstants.TIFF_TAG_MODEL); /* * GPS */ final TiffImageMetadata exifMetadata = jpegMetadata.getExif(); if (exifMetadata != null) { try { final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS(); if (gpsInfo != null) { photoMetadata.latitude = gpsInfo.getLatitudeAsDegreesNorth(); photoMetadata.longitude = gpsInfo.getLongitudeAsDegreesEast(); } } catch (final Exception e) { // ignore } } photoMetadata.gpsAreaInfo = getExifValueGpsArea(jpegMetadata); /* * photoshop metadata */ final JpegPhotoshopMetadata pshopMetadata = jpegMetadata.getPhotoshop(); if (pshopMetadata != null) { final List<? extends IImageMetadataItem> pshopItems = pshopMetadata.getItems(); for (final IImageMetadataItem pshopItem : pshopItems) { if (pshopItem instanceof Item) { final Item item = (Item) pshopItem; final String keyword = item.getKeyword(); if (keyword.equals(IptcTypes.OBJECT_NAME.name)) { photoMetadata.objectName = item.getText(); } else if (keyword.equals(IptcTypes.CAPTION_ABSTRACT.name)) { photoMetadata.captionAbstract = item.getText(); } } } } } // set file date time photoMetadata.fileDateTime = _imageFileLastModified; //// this will log all available meta data // System.out.println(UI.timeStampNano()); // System.out.println(UI.timeStampNano() + " " + imageFileName); // System.out.println(UI.timeStampNano()); // System.out.println(imageMetadata.toString()); // System.out.println(UI.timeStampNano()); // System.out.println(photoMetadata); // System.out.println(UI.timeStampNano()); // // TODO remove SYSTEM.OUT.PRINTLN return photoMetadata; }
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 w w . j av a 2 s .c om*/ 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.UploadTest.java
/** * Tests the {@link UploadUtils#updateFile(MapillaryImportedImage)} method. *///from ww w. java 2 s . co m @Test public void updateFileTest() throws IOException { File image = new File("images/icon16.png"); MapillaryImportedImage img = ImageUtil.readImagesFrom(image, new LatLon(0, 0)).get(0); File updatedFile = null; try { updatedFile = UploadUtils.updateFile(img); ImageMetadata metadata = Imaging.getMetadata(updatedFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF) != null); assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE) != null); assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF) != null); assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE) != null); assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION) != null); assertTrue( jpegMetadata.findEXIFValueWithExactMatch(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL) != null); assertEquals(0, MapillaryUtils.degMinSecToDouble( (RationalNumber[]) jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE).getValue(), jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF) .getValue().toString()), 0.01); assertEquals(0, MapillaryUtils.degMinSecToDouble( (RationalNumber[]) jpegMetadata .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE).getValue(), jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF) .getValue().toString()), 0.01); } catch (ImageReadException | ImageWriteException | IOException e) { fail(); } finally { updatedFile.delete(); } }
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.//from w w w . ja v 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; }
From source file:org.openstreetmap.josm.plugins.mapillary.utils.ImageImportUtil.java
/** * @param is the input stream to read the metadata from * @param f the file that will be set as a field to the returned {@link MapillaryImportedImage} * @param defaultLL the coordinates that the image should get, if no coordinates are found in metadata * @return the {@link MapillaryImportedImage} with the read metadata and the given file set * @throws IOException if an IOException occurs while reading from the input stream *//*from ww w .ja va 2s . c o m*/ private static MapillaryImportedImage readImageFrom(final InputStream is, final File f, final LatLon defaultLL) throws IOException { Object latRef = null; Object lonRef = null; Object lat = null; Object lon = null; Object gpsDir = null; Object dateTime = null; final ImageMetadata meta; try { meta = Imaging.getMetadata(is, null); if (meta instanceof JpegImageMetadata) { final JpegImageMetadata jpegMeta = (JpegImageMetadata) meta; latRef = getTiffFieldValue(jpegMeta, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF); lonRef = getTiffFieldValue(jpegMeta, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF); lat = getTiffFieldValue(jpegMeta, GpsTagConstants.GPS_TAG_GPS_LATITUDE); lon = getTiffFieldValue(jpegMeta, GpsTagConstants.GPS_TAG_GPS_LONGITUDE); gpsDir = getTiffFieldValue(jpegMeta, GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); dateTime = getTiffFieldValue(jpegMeta, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); } } catch (ImageReadException e) { // Can't read metadata from image, use defaults instead } final LatLon latLon; if (lat instanceof RationalNumber[] && latRef != null && lon instanceof RationalNumber[] && lonRef != null) { latLon = new LatLon(MapillaryUtils.degMinSecToDouble((RationalNumber[]) lat, latRef.toString()), MapillaryUtils.degMinSecToDouble((RationalNumber[]) lon, lonRef.toString())); } else { latLon = defaultLL; } final double ca; if (gpsDir instanceof RationalNumber) { ca = ((RationalNumber) gpsDir).doubleValue(); } else { ca = 0; } if (dateTime == null) { return new MapillaryImportedImage(latLon, ca, f); } return new MapillaryImportedImage(latLon, ca, f, dateTime.toString()); }