diff --git a/importPicture/src/osm/jp/gpx/ImportPicture.java b/importPicture/src/osm/jp/gpx/ImportPicture.java index e05b155..901ca0c 100644 --- a/importPicture/src/osm/jp/gpx/ImportPicture.java +++ b/importPicture/src/osm/jp/gpx/ImportPicture.java @@ -408,10 +408,6 @@ * @throws ImageWriteException */ boolean proc(File dir, long delta, ElementMapTRKSEG mapTRKSEG, boolean exifWrite, Node gpx) throws ParseException, ImageReadException, IOException, ImageWriteException { - DecimalFormat yearFormatter = new DecimalFormat("0000"); - DecimalFormat monthFormatter = new DecimalFormat("00"); - DecimalFormat dayFormatter = new DecimalFormat("00"); - boolean ret = false; File[] files = dir.listFiles(new JpegFileFilter()); Arrays.sort(files, new FileSort()); @@ -428,185 +424,220 @@ continue; } - // itime <-- 画像ファイルの撮影時刻 - // ファイルの更新日時/EXIFの撮影日時 - //Date itime = changeLocalTime2GMT(new Date(image.lastModified())); - Date itime = new Date(image.lastModified()); - if (this.exifBase) { - ImageMetadata meta = Imaging.getMetadata(image); - JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta; - if (jpegMetadata == null) { - System.out.println("'"+ image.getAbsolutePath() +"' にEXIF情報がありません"); - continue; - } - TiffImageMetadata exif = jpegMetadata.getExif(); - if (exif == null) { - System.out.println("'"+ image.getAbsolutePath() +"' にEXIF情報がありません"); - continue; - } - String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0]; - itime = ImportPicture.toEXIFDate(dateTimeOriginal); - } - System.out.print(String.format("%20s|", toUTCString(itime))); - - // uktime <-- 画像撮影時刻に対応するGPX時刻(補正日時) - Date correctedtime = new Date(itime.getTime() + delta); - System.out.print(String.format("%20s|", toUTCString(correctedtime))); - - // 時刻uktimeにおけるをtrkptに追加する - String eleStr = "-"; - String magvarStr = "-"; - String speedStr = "-"; - double latitude = 90.5D; - double longitude = 180.5D; - //Element trkptE = null; - TagTrkpt trkptT = null; - - for (Map.Entry map : mapTRKSEG.entrySet()) { - ElementMapTRKPT mapTRKPT = map.getValue(); - trkptT = mapTRKPT.getValue(correctedtime); - if (trkptT != null) { - break; - } - } - if (trkptT == null) { - System.out.print(String.format("%-14s|%-14s|", "", "")); - System.out.println(String.format("%8s|%6s|%6s|", "", "", "")); - if (!this.param_ImgOutputAll) { - continue; - } - } - else { - latitude = trkptT.lat; - longitude = trkptT.lon; - - if (trkptT.eleStr != null) { - eleStr = new String(trkptT.eleStr); - } - - if (trkptT.magvarStr != null) { - magvarStr = new String(trkptT.magvarStr); - } - - if (trkptT.speedStr != null) { - speedStr = new String(trkptT.speedStr); - } - //System.out.print(String.format("%-14s|%-14s|", (new Double(latitude)).toString(), (new Double(longitude)).toString())); - System.out.print(String.format("%14.10f|%14.10f|", latitude, longitude)); - System.out.println(String.format("%8s|%6s|%6s|", eleStr, magvarStr, speedStr)); - } - - ret = true; - FileOutputStream fos = null; - outDir.mkdir(); - - if (exifWrite) { - TiffOutputSet outputSet = null; - - ImageMetadata meta = Imaging.getMetadata(image); - JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta; - if (jpegMetadata != null) { - TiffImageMetadata exif = jpegMetadata.getExif(); - if (exif != null) { - outputSet = exif.getOutputSet(); - } - } - - if (outputSet == null) { - System.out.println("added : new tiff output set"); - outputSet = new TiffOutputSet(); - } - - //---- EXIF_TAG_DATE_TIME_ORIGINAL / 「撮影日時/オリジナル画像の生成日時」---- - TiffOutputDirectory exifDir = outputSet.getOrCreateExifDirectory(); - { - Calendar cal = GregorianCalendar.getInstance(); - cal.setTimeZone(TimeZone.getTimeZone("UTC")); - cal.setTime(correctedtime); - exifDir.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); - exifDir.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, ImportPicture.toEXIFString(cal.getTime())); - } - - //---- EXIF GPS_TIME_STAMP ---- - TiffOutputDirectory gpsDir = outputSet.getOrCreateGPSDirectory(); - { - Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); - cal.setTimeZone(TimeZone.getTimeZone("GMT+00")); - cal.setTime(correctedtime); - final String yearStr = yearFormatter.format(cal.get(Calendar.YEAR)); - final String monthStr = monthFormatter.format(cal.get(Calendar.MONTH) + 1); - final String dayStr = dayFormatter.format(cal.get(Calendar.DAY_OF_MONTH)); - final String dateStamp = yearStr +":"+ monthStr +":"+ dayStr; - - gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP); - gpsDir.add(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP, - RationalNumber.valueOf(cal.get(Calendar.HOUR_OF_DAY)), - RationalNumber.valueOf(cal.get(Calendar.MINUTE)), - RationalNumber.valueOf(cal.get(Calendar.SECOND))); - gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP); - gpsDir.add(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP, dateStamp); - } - - if (trkptT != null) { - //---- EXIF GPS elevation/ALTITUDE ---- - if (eleStr.equals("-") == false) { - final double altitude = Double.parseDouble(eleStr); - gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_ALTITUDE); - gpsDir.add(GpsTagConstants.GPS_TAG_GPS_ALTITUDE, RationalNumber.valueOf(altitude)); - } - - //---- EXIF GPS magvar/IMG_DIRECTION ---- - if (magvarStr.equals("-") == false) { - final double magvar = Double.parseDouble(magvarStr); - gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); - gpsDir.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(magvar)); - } - - //---- EXIF GPS_ ---- - longitude = Math.abs(longitude); - latitude = Math.abs(latitude); - outputSet.setGPSInDegrees(longitude, latitude); - } - - ExifRewriter rewriter = new ExifRewriter(); - try { - fos = new FileOutputStream(new File(outDir, imageName)); - rewriter.updateExifMetadataLossy(image, fos, outputSet); - } - finally { - if (fos != null) { - fos.close(); - } - } - - if (Boolean.parseBoolean(params.getProperty(AppParameters.GPX_OUTPUT_WPT))) { - if (trkptT != null) { - Element temp = createWptTag(image, itime.getTime(), trkptT.trkpt); - gpx.appendChild(temp); - } - } - } - else { - if (this.param_ImgOutputAll) { - // EXIFの変換を伴わない単純なファイルコピー - FileInputStream sStream = new FileInputStream(image); - FileInputStream dStream = new FileInputStream(new File(outDir, imageName)); - FileChannel srcChannel = sStream.getChannel(); - FileChannel destChannel = dStream.getChannel(); - try { - srcChannel.transferTo(0, srcChannel.size(), destChannel); - } - finally { - srcChannel.close(); - destChannel.close(); - sStream.close(); - dStream.close(); - } - } + Discripter result = procImageFile(image, delta, mapTRKSEG, exifWrite, gpx); + ret |= result.ret; + switch (result.control) { + case Discripter.CONTINUE: + continue; + case Discripter.BREAK: + break; } } return ret; } + + class Discripter { + static final int NEXT = 0; + static final int CONTINUE = -1; + static final int BREAK = 1; + + public boolean ret; + public int control; + public Discripter(boolean ret) { + this.ret = ret; + this.control = Discripter.NEXT; + } + } + + Discripter procImageFile(File image, long delta, ElementMapTRKSEG mapTRKSEG, boolean exifWrite, Node gpx) throws ParseException, ImageReadException, IOException, ImageWriteException { + DecimalFormat yearFormatter = new DecimalFormat("0000"); + DecimalFormat monthFormatter = new DecimalFormat("00"); + DecimalFormat dayFormatter = new DecimalFormat("00"); + + Discripter result = new Discripter(false); + + // itime <-- 画像ファイルの撮影時刻 + // ファイルの更新日時/EXIFの撮影日時 + Date itime = new Date(image.lastModified()); + if (this.exifBase) { + ImageMetadata meta = Imaging.getMetadata(image); + JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta; + if (jpegMetadata == null) { + System.out.println("'"+ image.getAbsolutePath() +"' にEXIF情報がありません"); + result.control = Discripter.CONTINUE; + return result; + } + TiffImageMetadata exif = jpegMetadata.getExif(); + if (exif == null) { + System.out.println("'"+ image.getAbsolutePath() +"' にEXIF情報がありません"); + result.control = Discripter.CONTINUE; + return result; + } + String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0]; + itime = ImportPicture.toEXIFDate(dateTimeOriginal); + } + System.out.print(String.format("%20s|", toUTCString(itime))); + + // uktime <-- 画像撮影時刻に対応するGPX時刻(補正日時) + Date correctedtime = new Date(itime.getTime() + delta); + System.out.print(String.format("%20s|", toUTCString(correctedtime))); + + // 時刻uktimeにおけるをtrkptに追加する + String eleStr = "-"; + String magvarStr = "-"; + String speedStr = "-"; + double latitude = 90.5D; + double longitude = 180.5D; + //Element trkptE = null; + TagTrkpt trkptT = null; + + for (Map.Entry map : mapTRKSEG.entrySet()) { + ElementMapTRKPT mapTRKPT = map.getValue(); + trkptT = mapTRKPT.getValue(correctedtime); + if (trkptT != null) { + break; + } + } + + if (trkptT == null) { + System.out.print(String.format("%-14s|%-14s|", "", "")); + System.out.println(String.format("%8s|%6s|%6s|", "", "", "")); + if (!this.param_ImgOutputAll) { + result.control = Discripter.CONTINUE; + return result; + } + } + else { + latitude = trkptT.lat; + longitude = trkptT.lon; + + if (trkptT.eleStr != null) { + eleStr = new String(trkptT.eleStr); + } + + if (trkptT.magvarStr != null) { + magvarStr = new String(trkptT.magvarStr); + } + + if (trkptT.speedStr != null) { + speedStr = new String(trkptT.speedStr); + } + //System.out.print(String.format("%-14s|%-14s|", (new Double(latitude)).toString(), (new Double(longitude)).toString())); + System.out.print(String.format("%14.10f|%14.10f|", latitude, longitude)); + System.out.println(String.format("%8s|%6s|%6s|", eleStr, magvarStr, speedStr)); + } + + result.ret = true; + FileOutputStream fos = null; + outDir.mkdir(); + + if (exifWrite) { + TiffOutputSet outputSet = null; + + ImageMetadata meta = Imaging.getMetadata(image); + JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta; + if (jpegMetadata != null) { + TiffImageMetadata exif = jpegMetadata.getExif(); + if (exif != null) { + outputSet = exif.getOutputSet(); + } + } + + if (outputSet == null) { + System.out.println("added : new tiff output set"); + outputSet = new TiffOutputSet(); + } + + //---- EXIF_TAG_DATE_TIME_ORIGINAL / 「撮影日時/オリジナル画像の生成日時」---- + TiffOutputDirectory exifDir = outputSet.getOrCreateExifDirectory(); + { + Calendar cal = GregorianCalendar.getInstance(); + cal.setTimeZone(TimeZone.getTimeZone("UTC")); + cal.setTime(correctedtime); + exifDir.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); + exifDir.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, ImportPicture.toEXIFString(cal.getTime())); + } + + //---- EXIF GPS_TIME_STAMP ---- + TiffOutputDirectory gpsDir = outputSet.getOrCreateGPSDirectory(); + { + Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); + cal.setTimeZone(TimeZone.getTimeZone("GMT+00")); + cal.setTime(correctedtime); + final String yearStr = yearFormatter.format(cal.get(Calendar.YEAR)); + final String monthStr = monthFormatter.format(cal.get(Calendar.MONTH) + 1); + final String dayStr = dayFormatter.format(cal.get(Calendar.DAY_OF_MONTH)); + final String dateStamp = yearStr +":"+ monthStr +":"+ dayStr; + + gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP); + gpsDir.add(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP, + RationalNumber.valueOf(cal.get(Calendar.HOUR_OF_DAY)), + RationalNumber.valueOf(cal.get(Calendar.MINUTE)), + RationalNumber.valueOf(cal.get(Calendar.SECOND))); + gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP); + gpsDir.add(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP, dateStamp); + } + + if (trkptT != null) { + //---- EXIF GPS elevation/ALTITUDE ---- + if (eleStr.equals("-") == false) { + final double altitude = Double.parseDouble(eleStr); + gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_ALTITUDE); + gpsDir.add(GpsTagConstants.GPS_TAG_GPS_ALTITUDE, RationalNumber.valueOf(altitude)); + } + + //---- EXIF GPS magvar/IMG_DIRECTION ---- + if (magvarStr.equals("-") == false) { + final double magvar = Double.parseDouble(magvarStr); + gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION); + gpsDir.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(magvar)); + } + + //---- EXIF GPS_ ---- + longitude = Math.abs(longitude); + latitude = Math.abs(latitude); + outputSet.setGPSInDegrees(longitude, latitude); + } + + ExifRewriter rewriter = new ExifRewriter(); + try { + fos = new FileOutputStream(new File(outDir, image.getName())); + rewriter.updateExifMetadataLossy(image, fos, outputSet); + } + finally { + if (fos != null) { + fos.close(); + } + } + + if (Boolean.parseBoolean(params.getProperty(AppParameters.GPX_OUTPUT_WPT))) { + if (trkptT != null) { + Element temp = createWptTag(image, itime.getTime(), trkptT.trkpt); + gpx.appendChild(temp); + } + } + } + else { + if (this.param_ImgOutputAll) { + // EXIFの変換を伴わない単純なファイルコピー + FileInputStream sStream = new FileInputStream(image); + FileInputStream dStream = new FileInputStream(new File(outDir, image.getName())); + FileChannel srcChannel = sStream.getChannel(); + FileChannel destChannel = dStream.getChannel(); + try { + srcChannel.transferTo(0, srcChannel.size(), destChannel); + } + finally { + srcChannel.close(); + destChannel.close(); + sStream.close(); + dStream.close(); + } + } + } + result.control = Discripter.NEXT; + return result; + } /** * 対象は '*.JPG' のみ対象とする