Changeset 30738 in osm for applications/editors/josm/plugins/photo_geotagging/src
- Timestamp:
- 2014-10-19T01:27:04+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java
r28398 r30738 8 8 import java.io.FileOutputStream; 9 9 import java.io.IOException; 10 import java.io.OutputStream;11 10 import java.text.DecimalFormat; 12 11 import java.util.Calendar; … … 49 48 } 50 49 51 public static void setExifGPSTagWorker(File jpegImageFile, File dst, double lat, double lon, long gpsTime, Double ele) throws IOException, 52 ImageReadException, ImageWriteException 53 { 54 OutputStream os = null; 55 try { 56 TiffOutputSet outputSet = null; 50 public static void setExifGPSTagWorker(File jpegImageFile, File dst, double lat, double lon, long gpsTime, Double ele) 51 throws IOException, ImageReadException, ImageWriteException { 52 TiffOutputSet outputSet = null; 57 53 58 59 60 61 54 IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile); 55 JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; 56 if (null != jpegMetadata) { 57 TiffImageMetadata exif = jpegMetadata.getExif(); 62 58 63 if (null != exif) { 64 outputSet = exif.getOutputSet(); 65 } 59 if (null != exif) { 60 outputSet = exif.getOutputSet(); 66 61 } 62 } 67 63 68 69 70 64 if (null == outputSet) { 65 outputSet = new TiffOutputSet(); 66 } 71 67 72 68 Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 73 69 74 70 calendar.setTimeInMillis(gpsTime); 75 71 76 77 78 79 80 81 72 final int year = calendar.get(Calendar.YEAR); 73 final int month = calendar.get(Calendar.MONTH) + 1; 74 final int day = calendar.get(Calendar.DAY_OF_MONTH); 75 final int hour = calendar.get(Calendar.HOUR_OF_DAY); 76 final int minute = calendar.get(Calendar.MINUTE); 77 final int second = calendar.get(Calendar.SECOND); 82 78 83 84 85 79 DecimalFormat yearFormatter = new DecimalFormat("0000"); 80 DecimalFormat monthFormatter = new DecimalFormat("00"); 81 DecimalFormat dayFormatter = new DecimalFormat("00"); 86 82 87 88 89 90 91 83 final String yearStr = yearFormatter.format(year); 84 final String monthStr = monthFormatter.format(month); 85 final String dayStr = dayFormatter.format(day); 86 final String dateStamp = yearStr+":"+monthStr+":"+dayStr; 87 //System.err.println("date: "+dateStamp+" h/m/s: "+hour+"/"+minute+"/"+second); 92 88 93 94 95 96 97 98 99 100 101 89 Double[] timeStamp = {new Double(hour), new Double(minute), new Double(second)}; 90 TiffOutputField gpsTimeStamp = TiffOutputField.create( 91 GPSTagConstants.GPS_TAG_GPS_TIME_STAMP, 92 outputSet.byteOrder, timeStamp); 93 TiffOutputDirectory exifDirectory = outputSet.getOrCreateGPSDirectory(); 94 // make sure to remove old value if present (this method will 95 // not fail if the tag does not exist). 96 exifDirectory.removeField(GPSTagConstants.GPS_TAG_GPS_TIME_STAMP); 97 exifDirectory.add(gpsTimeStamp); 102 98 103 104 105 106 107 108 109 99 TiffOutputField gpsDateStamp = SanselanFixes.create( 100 GPSTagConstants.GPS_TAG_GPS_DATE_STAMP, 101 outputSet.byteOrder, dateStamp); 102 // make sure to remove old value if present (this method will 103 // not fail if the tag does not exist). 104 exifDirectory.removeField(GPSTagConstants.GPS_TAG_GPS_DATE_STAMP); 105 exifDirectory.add(gpsDateStamp); 110 106 111 107 SanselanFixes.setGPSInDegrees(outputSet, lon, lat); 112 108 113 114 115 116 117 118 119 109 if (ele != null) { 110 byte eleRef = ele >= 0 ? (byte) 0 : (byte) 1; 111 TiffOutputField gpsAltitudeRef = new TiffOutputField( 112 GPSTagConstants.GPS_TAG_GPS_ALTITUDE_REF, 113 FieldType.FIELD_TYPE_BYTE, 1, new byte[] { eleRef }); 114 exifDirectory.removeField(GPSTagConstants.GPS_TAG_GPS_ALTITUDE_REF); 115 exifDirectory.add(gpsAltitudeRef); 120 116 121 Number[] val = new Number[] { Math.abs(ele) }; 122 byte[] bytes = FieldType.FIELD_TYPE_RATIONAL.writeData(val, outputSet.byteOrder); 123 TiffOutputField gpsAltitude = new TiffOutputField( 124 GPSTagConstants.GPS_TAG_GPS_ALTITUDE, 125 FieldType.FIELD_TYPE_RATIONAL, 1, bytes); 126 exifDirectory.removeField(GPSTagConstants.GPS_TAG_GPS_ALTITUDE); 127 exifDirectory.add(gpsAltitude); 128 } 129 130 os = new FileOutputStream(dst); 131 os = new BufferedOutputStream(os); 132 133 new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, 134 outputSet); 135 136 os.close(); 137 os = null; 138 } finally { 139 if (os != null) { 140 try { 141 os.close(); 142 } catch (IOException e) {} 143 } 117 Number[] val = new Number[] { Math.abs(ele) }; 118 byte[] bytes = FieldType.FIELD_TYPE_RATIONAL.writeData(val, outputSet.byteOrder); 119 TiffOutputField gpsAltitude = new TiffOutputField( 120 GPSTagConstants.GPS_TAG_GPS_ALTITUDE, 121 FieldType.FIELD_TYPE_RATIONAL, 1, bytes); 122 exifDirectory.removeField(GPSTagConstants.GPS_TAG_GPS_ALTITUDE); 123 exifDirectory.add(gpsAltitude); 124 } 125 try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dst))) { 126 new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); 144 127 } 145 128 }
Note:
See TracChangeset
for help on using the changeset viewer.