Changeset 35738 in osm for applications/editors/josm
- Timestamp:
- 2021-04-25T18:09:26+02:00 (4 years ago)
- Location:
- applications/editors/josm/plugins/photo_geotagging
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java
r35715 r35738 40 40 * @param ele elevation - can be null if not available 41 41 * @param imgDir image direction in degrees (0..360) - can be null if not available 42 * @param lossy whether to use lossy approach when writing metadata (overwriting unknown tags) 42 43 * @throws IOException in case of I/O error 43 44 */ 44 public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir ) throws IOException {45 public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir, boolean lossy) throws IOException { 45 46 try { 46 setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir );47 setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir, lossy); 47 48 } catch (ImageReadException ire) { 48 49 throw new IOException(tr("Read error: "+ire), ire); … … 52 53 } 53 54 54 public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir )55 public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir, boolean lossy) 55 56 throws IOException, ImageReadException, ImageWriteException { 56 57 … … 148 149 try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dst))) { 149 150 if (metadata instanceof JpegImageMetadata) { 150 new ExifRewriter().updateExifMetadataLossless(imageFile, os, outputSet); 151 if (lossy) { 152 new ExifRewriter().updateExifMetadataLossy(imageFile, os, outputSet); 153 } else { 154 new ExifRewriter().updateExifMetadataLossless(imageFile, os, outputSet); 155 } 151 156 } else if (metadata instanceof TiffImageMetadata) { 152 157 new TiffImageWriterLossy().write(os, outputSet); -
applications/editors/josm/plugins/photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingAction.java
r35726 r35738 35 35 import javax.swing.UIManager; 36 36 37 import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter; 37 38 import org.apache.commons.io.FilenameUtils; 38 39 import org.openstreetmap.josm.gui.ExtendedDialog; … … 210 211 private boolean canceled = false; 211 212 private Boolean override_backup = null; 213 private boolean lossy = false; 212 214 213 215 private File fileFrom; … … 258 260 if (canceled) return; 259 261 ExifGPSTagger.setExifGPSTag(fileFrom, fileTo, e.getPos().lat(), e.getPos().lon(), 260 e.getGpsTime(), e.getSpeed(), e.getElevation(), e.getExifImgDir()); 262 e.getGpsTime(), e.getSpeed(), e.getElevation(), e.getExifImgDir(), lossy); 263 lossy = false; 261 264 262 265 if (mTime != null) { … … 287 290 } catch (final IOException ioe) { 288 291 ioe.printStackTrace(); 292 restoreFile(); 289 293 try { 290 294 SwingUtilities.invokeAndWait(() -> { 291 295 ExtendedDialog dlg = new ExtendedDialog(progressMonitor.getWindowParent(), tr("Error"), new String[] {tr("Abort"), tr("Retry"), tr("Ignore")}); 292 dlg.setIcon(JOptionPane.ERROR_MESSAGE);293 296 dlg.setButtonIcons("cancel", "dialogs/refresh", "dialogs/next"); 294 297 String msg; … … 298 301 msg = ioe.toString(); 299 302 } 300 dlg.setContent(tr("Unable to process file ''{0}'':", e.getFile().toString()) + "<br/>" + msg); 303 boolean tmpLossy = false; 304 if (!lossy && ioe.getCause() instanceof ExifRewriter.ExifOverflowException) { 305 tmpLossy = true; 306 dlg.setIcon(JOptionPane.WARNING_MESSAGE); 307 dlg.setContent(tr( 308 "The GPS tag could not be added to the file \"{0}\" because there is not enough free space in the EXIF section.<br>" 309 + "This can likely be fixed by rewriting the entire EXIF section, however some metadata may get lost in the process.<br><br>" 310 + "Would you like to try again using the lossy approach?", 311 e.getFile().getName())); 312 dlg.setDefaultButton(2); 313 } else { 314 dlg.setIcon(JOptionPane.ERROR_MESSAGE); 315 dlg.setContent(tr("Unable to process file ''{0}'':", e.getFile().toString()) + "<br/>" + msg); 316 dlg.setDefaultButton(3); 317 } 301 318 dlg.showDialog(); 302 319 switch (dlg.getValue()) { 303 320 case 2: // retry 304 321 currentIndex--; 322 lossy = tmpLossy; 305 323 break; 306 324 case 3: // continue … … 417 435 } 418 436 437 private void restoreFile() { 438 if (fileFrom != null && fileFrom.exists()) { 439 if (fileTo != null && fileTo.exists()) { 440 fileTo.delete(); 441 } 442 fileFrom.renameTo(fileTo); 443 } 444 } 445 419 446 private void cleanupFiles() throws IOException { 420 447 if (fileDelete != null) { -
applications/editors/josm/plugins/photo_geotagging/test/unit/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTaggerTest.java
r32392 r35738 25 25 26 26 @Test 27 @Ignore("To enable after https://issues.apache.org/jira/browse/IMAGING-179 is fixed")28 27 public void testTicket11757() throws Exception { 29 28 final File in = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg"); 30 ExifGPSTagger.setExifGPSTag(in, tempFolder.newFile(), 12, 34, new Date(), 12.34, Math.E, Math.PI );29 ExifGPSTagger.setExifGPSTag(in, tempFolder.newFile(), 12, 34, new Date(), 12.34, Math.E, Math.PI, true); 31 30 } 32 31 … … 44 43 final File in = new File(TestUtils.getTestDataRoot(), "IMG_7250_small.JPG"); 45 44 final File out = tempFolder.newFile(); 46 ExifGPSTagger.setExifGPSTag(in, out, 12, 34, new Date(), 12.34, Math.E, Math.PI );45 ExifGPSTagger.setExifGPSTag(in, out, 12, 34, new Date(), 12.34, Math.E, Math.PI, false); 47 46 final Process jhead = Runtime.getRuntime().exec(new String[]{"jhead", out.getAbsolutePath()}); 48 47 final String stdout = new Scanner(jhead.getErrorStream()).useDelimiter("\\A").next();
Note:
See TracChangeset
for help on using the changeset viewer.