+++ /dev/null
-package osm.jp.gpx;\r
-\r
-import java.io.*;\r
-import java.nio.channels.FileChannel;\r
-import java.text.DateFormat;\r
-import java.text.DecimalFormat;\r
-import java.text.ParseException;\r
-import java.text.SimpleDateFormat;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Calendar;\r
-import java.util.Comparator;\r
-import java.util.Date;\r
-import java.util.Map;\r
-import java.util.ResourceBundle;\r
-import java.util.TimeZone;\r
-import java.util.logging.LogManager;\r
-import java.util.logging.Logger;\r
-\r
-import javax.xml.parsers.*;\r
-import javax.xml.transform.TransformerException;\r
-\r
-import org.apache.commons.imaging.ImageReadException;\r
-import org.apache.commons.imaging.ImageWriteException;\r
-import org.apache.commons.imaging.Imaging;\r
-import org.apache.commons.imaging.common.ImageMetadata;\r
-import org.apache.commons.imaging.common.RationalNumber;\r
-import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;\r
-import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;\r
-import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;\r
-import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;\r
-import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;\r
-import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;\r
-import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;\r
-import org.w3c.dom.*;\r
-import org.xml.sax.SAXException;\r
-\r
-public class ImportPicture extends Thread {\r
- \r
- /**\r
- * 実行中に発生したExceptionを保持する場所\r
- */\r
- public Exception ex = null;\r
- \r
- /**\r
- * ログ設定プロパティファイルのファイル内容\r
- */\r
- protected static final String LOGGING_PROPERTIES_DATA\r
- = "handlers=java.util.logging.ConsoleHandler\n"\r
- + ".level=FINEST\n"\r
- + "java.util.logging.ConsoleHandler.level=INFO\n"\r
- + "java.util.logging.ConsoleHandler.formatter=osm.jp.gpx.YuuLogFormatter";\r
-\r
- /**\r
- * static initializer によるログ設定の初期化\r
- */\r
- public static final Logger LOGGER = Logger.getLogger("CommandLogging");\r
- static {\r
- InputStream inStream = null;\r
- try {\r
- inStream = new ByteArrayInputStream(LOGGING_PROPERTIES_DATA.getBytes("UTF-8"));\r
- try {\r
- LogManager.getLogManager().readConfiguration(inStream);\r
- // "ログ設定: LogManagerを設定しました。"\r
- LOGGER.config("LoggerSettings: LogManager setuped.");\r
- }\r
- catch (IOException e) {\r
- // LogManager設定の際に例外が発生しました.\r
- String str = "LoggerSettings: Exception occered:" + e.toString();\r
- LOGGER.warning(str);\r
- }\r
- }\r
- catch (UnsupportedEncodingException e) {\r
- String str = "LoggerSettings: Not supported 'UTF-8' encoding: " + e.toString();\r
- LOGGER.severe(str);\r
- }\r
- finally {\r
- try {\r
- if (inStream != null) {\r
- inStream.close();\r
- }\r
- } catch (IOException e) {\r
- String str = "LoggerSettings: Exception occored: "+ e.toString();\r
- LOGGER.warning(str);\r
- }\r
- }\r
- }\r
- \r
- /** メイン\r
- * 画像ファイルをGPXファイルに取り込みます。\r
- * \r
- * ・画像ファイルの更新日付をその画像の撮影日時とします。(Exi情報は無視します)\r
- * ※ 対象とするファイルは'*.jpg'のみ\r
- * ・精確な時刻との時差を入力することで、撮影日時を補正します。\r
- * ・画像ファイルの更新日付リストをCSV形式のファイルとして出力する。\r
- * ・・結果は、取り込み元のGPXファイルとは別に、元ファイル名にアンダーバー「_」を付加した.ファイルに出力します。\r
- * \r
- * exp) $ java -cp .:AdjustTime.jar:commons-imaging-1.0-SNAPSHOT.jar [AdjustTime.ini]\r
- * exp) > java -cp .;AdjustTime.jar;commons-imaging-1.0-SNAPSHOT.jar [AdjustTime.ini]\r
- *\r
- * @param argv\r
- * argv[0] = INIファイルのパス名\r
- * \r
- * argv[-] = dummy\r
- * argv[0] = 画像ファイルが格納されているディレクトリ --> imgDir\r
- * argv[1] = 時刻補正の基準とする画像ファイル --> baseFile\r
- * argv[2] = 基準画像ファイルの精確な撮影日時 "yyyy-MM-dd'T'HH:mm:ss" --> timeStr\r
- * argv[3] = 出力先フォルダ --> outDir\r
- * argv[4] = 撮影位置をロギングしたGPXファイル --> gpxDir\r
- * \r
- * @throws IOException\r
- * @throws ImageReadException \r
- */\r
- public static void main(String[] argv) throws Exception\r
- {\r
- ImportPicture obj = new ImportPicture();\r
- obj.setUp(((argv.length < 1) ? AppParameters.FILE_PATH : argv[0]));\r
- }\r
- \r
- public File gpxDir;\r
- public File imgDir;\r
- public File outDir;\r
- public long delta = 0;\r
- public boolean exif = false;\r
- public boolean exifBase = false;\r
- public ArrayList<File> gpxFiles = new ArrayList<>();\r
- public AppParameters params;\r
- public boolean param_GpxSplit = false;\r
- public static boolean param_GpxNoFirstNode = false;\r
- public boolean param_GpxReuse = false;\r
- public boolean param_GpxOutputWpt = true;\r
- public boolean param_ImgOutputAll = false;\r
- public String param_GpxSourceFolder = ".";\r
- \r
- public static final String TIME_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss'Z'";\r
- private static final String EXIF_DATE_TIME_FORMAT_STRING = "yyyy:MM:dd HH:mm:ss";\r
- public ResourceBundle i18n = ResourceBundle.getBundle("i18n");\r
- \r
- public void setUp(String paramFilePath) throws Exception {\r
- System.out.println("Param File = '"+ paramFilePath +"'");\r
- this.params = new AppParameters(paramFilePath);\r
-\r
- Date imgtime;\r
-\r
- System.out.println(" - param: "+ AppParameters.IMG_TIME +"="+ this.params.getProperty(AppParameters.IMG_TIME) );\r
- System.out.println(" - param: "+ AppParameters.IMG_BASE_FILE +"="+ this.params.getProperty(AppParameters.IMG_BASE_FILE) );\r
- System.out.println(" - param: "+ AppParameters.GPX_BASETIME +"="+ this.params.getProperty(AppParameters.GPX_BASETIME) );\r
- System.out.println(" - param: "+ AppParameters.IMG_SOURCE_FOLDER +"="+ this.params.getProperty(AppParameters.IMG_SOURCE_FOLDER) );\r
- System.out.println(" - param: "+ AppParameters.IMG_OUTPUT_FOLDER +"="+ this.params.getProperty(AppParameters.IMG_OUTPUT_FOLDER) );\r
- System.out.println(" - param: "+ AppParameters.IMG_OUTPUT +"="+ this.params.getProperty(AppParameters.IMG_OUTPUT)); \r
- System.out.println(" - param: "+ AppParameters.IMG_OUTPUT_ALL +"="+ this.param_ImgOutputAll);\r
- System.out.println(" - param: "+ AppParameters.IMG_OUTPUT_EXIF +"= "+ String.valueOf(this.exif));\r
- System.out.println(" - param: "+ AppParameters.GPX_SOURCE_FOLDER +"="+ this.param_GpxSourceFolder);\r
- System.out.println(" - param: "+ AppParameters.GPX_OUTPUT_WPT +"="+ this.param_GpxOutputWpt);\r
- System.out.println(" - param: "+ AppParameters.GPX_OVERWRITE_MAGVAR +"="+ Complementation.param_GpxOverwriteMagvar);\r
- System.out.println(" - param: "+ AppParameters.GPX_OUTPUT_SPEED +"="+ Complementation.param_GpxOutputSpeed);\r
- System.out.println(" - param: "+ AppParameters.GPX_GPXSPLIT +"="+ this.param_GpxSplit);\r
- System.out.println(" - param: "+ AppParameters.GPX_NO_FIRST_NODE +"="+ ImportPicture.param_GpxNoFirstNode); \r
- System.out.println(" - param: "+ AppParameters.GPX_REUSE +"="+ this.param_GpxReuse);\r
-\r
- this.ex = null;\r
- // argv[0] --> AppParameters.IMG_SOURCE_FOLDER に置き換え\r
- this.imgDir = new File(this.params.getProperty(AppParameters.IMG_SOURCE_FOLDER));\r
-\r
- // 基準時刻(ファイル更新日時 | EXIF撮影日時)\r
- this.exifBase = (this.params.getProperty(AppParameters.GPX_BASETIME).equals("EXIF_TIME"));\r
-\r
- // 基準時刻ファイルの「更新日時」を使って時刻合わせを行う。\r
- // argv[1] --> AppParameters.IMG_BASE_FILE に置き換え\r
- imgtime = this.adjustTime(new File(this.imgDir, this.params.getProperty(AppParameters.IMG_BASE_FILE)));\r
-\r
- // 出力ファイル\r
- // argv[3] --> AppParameters.IMG_OUTPUT に置き換え\r
- this.outDir = new File(this.params.getProperty(AppParameters.IMG_OUTPUT_FOLDER));\r
-\r
- // その他のパラメータを読み取る\r
- String paramStr = this.params.getProperty(AppParameters.GPX_GPXSPLIT);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- this.param_GpxSplit = true;\r
- }\r
- \r
- paramStr = this.params.getProperty(AppParameters.GPX_NO_FIRST_NODE);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- ImportPicture.param_GpxNoFirstNode = true;\r
- }\r
- \r
- paramStr = this.params.getProperty(AppParameters.GPX_REUSE);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- this.param_GpxReuse = true;\r
- }\r
- \r
- paramStr = this.params.getProperty(AppParameters.IMG_OUTPUT_ALL);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- this.param_ImgOutputAll = true;\r
- }\r
-\r
- paramStr = this.params.getProperty(AppParameters.GPX_OUTPUT_WPT);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- this.param_GpxOutputWpt = true;\r
- }\r
- \r
- paramStr = this.params.getProperty(AppParameters.GPX_OVERWRITE_MAGVAR);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- Complementation.param_GpxOverwriteMagvar = true;\r
- }\r
-\r
- paramStr = this.params.getProperty(AppParameters.GPX_OUTPUT_SPEED);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- Complementation.param_GpxOutputSpeed = true;\r
- }\r
-\r
- paramStr = this.params.getProperty(AppParameters.GPX_SOURCE_FOLDER);\r
- if (paramStr != null) {\r
- this.param_GpxSourceFolder = paramStr;\r
- this.gpxDir = new File(this.param_GpxSourceFolder);\r
- if (!this.gpxDir.exists()) {\r
- // GPXファイルまたはディレクトリが存在しません。('%s')\r
- System.out.println(\r
- String.format(i18n.getString("msg.100"), paramStr)\r
- );\r
- return;\r
- }\r
- }\r
- else {\r
- this.gpxDir = this.imgDir;\r
- }\r
-\r
- // 指定されたディレクトリ内のGPXファイルすべてを対象とする\r
- if (this.gpxDir.isDirectory()) {\r
- File[] files = this.gpxDir.listFiles();\r
- if (files == null) {\r
- // 対象となるGPXファイルがありませんでした。('%s')\r
- System.out.println(\r
- String.format(i18n.getString("msg.110"), this.gpxDir.getAbsolutePath())\r
- );\r
- return;\r
- }\r
- if (this.param_ImgOutputAll && (files.length > 1)) {\r
- // "複数のGPXファイルがあるときには、'IMG.OUTPUT_ALL'オプションは指定できません。"\r
- System.out.println(\r
- i18n.getString("msg.120")\r
- );\r
- return;\r
- }\r
- \r
- java.util.Arrays.sort(\r
- files, new java.util.Comparator<File>() {\r
- @Override\r
- public int compare(File file1, File file2){\r
- return file1.getName().compareTo(file2.getName());\r
- }\r
- }\r
- );\r
- for (File file : files) {\r
- if (file.isFile()) {\r
- String filename = file.getName().toUpperCase();\r
- if (filename.toUpperCase().endsWith(".GPX")) {\r
- if (!filename.toUpperCase().endsWith("_.GPX") || this.param_GpxReuse) {\r
- this.gpxFiles.add(file);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- else {\r
- this.gpxFiles.add(this.gpxDir);\r
- }\r
-\r
- paramStr = this.params.getProperty(AppParameters.IMG_OUTPUT_EXIF);\r
- if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) {\r
- this.exif = true;\r
- }\r
- \r
- String timeStr = this.params.getProperty(AppParameters.IMG_TIME);\r
- try {\r
- Date t = toUTCDate(timeStr);\r
- this.delta = t.getTime() - imgtime.getTime();\r
- }\r
- catch (ParseException e) {\r
- // "'%s'の書式が違います(%s)"\r
- System.out.println(\r
- String.format(\r
- i18n.getString("msg.130"),\r
- timeStr,\r
- TIME_FORMAT_STRING\r
- )\r
- );\r
- return;\r
- }\r
-\r
- this.start();\r
- try {\r
- this.join();\r
- } catch(InterruptedException end) {}\r
- if (this.ex != null) {\r
- throw this.ex;\r
- }\r
- }\r
- \r
- /**\r
- * @code{\r
- <wpt lat="35.25714922" lon="139.15490497">\r
- <ele>62.099998474121094</ele>\r
- <time>2012-06-11T00:44:38Z</time>\r
- <hdop>0.75</hdop>\r
- <name><![CDATA[写真]]></name>\r
- <cmt><![CDATA[精度: 3.0m]]></cmt>\r
- <link href="2012-06-11_09-44-38.jpg">\r
- <text>2012-06-11_09-44-38.jpg</text>\r
- </link>\r
- <sat>9</sat>\r
- </wpt>\r
- * }\r
- */\r
- @Override\r
- public void run() {\r
- try {\r
- if (params.getProperty(AppParameters.IMG_OUTPUT).equals(Boolean.toString(true))) {\r
- outDir = new File(outDir, imgDir.getName());\r
- }\r
- else {\r
- outDir = gpxDir;\r
- }\r
- for (File gpxFile : this.gpxFiles) {\r
- procGPXfile(new GpxFile(gpxFile));\r
- }\r
- }\r
- catch(ParserConfigurationException | DOMException | SAXException | IOException | ParseException | ImageReadException | ImageWriteException | IllegalArgumentException | TransformerException e) {\r
- e.printStackTrace();\r
- this.ex = new Exception(e);\r
- }\r
- }\r
- \r
- /**\r
- * 個別のGPXファイルを処理する\r
- * \r
- * @throws ParserConfigurationException \r
- * @throws IOException \r
- * @throws SAXException \r
- * @throws ParseException \r
- * @throws ImageWriteException \r
- * @throws ImageReadException \r
- * @throws TransformerException \r
- */\r
- void procGPXfile(GpxFile gpxFile) throws ParserConfigurationException, SAXException, IOException, ParseException, ImageReadException, ImageWriteException, TransformerException {\r
- System.gc();\r
-\r
- System.out.println("time difference: "+ (delta / 1000) +"(sec)");\r
- System.out.println(" Target GPX: ["+ gpxFile.getAbsolutePath() +"]");\r
- System.out.println(" EXIF: "+ (exif ? ("convert to '" + outDir.getAbsolutePath() +"'") : "off"));\r
- System.out.println();\r
-\r
- // imgDir内の画像ファイルを処理する\r
- System.out.println("|--------------------------------|--------------------|--------------------|--------------|--------------|--------|------|------|");\r
- System.out.println("| name | Camera Time | GPStime | Latitude | Longitude | ele |magvar| km/h |");\r
- System.out.println("|--------------------------------|--------------------|--------------------|--------------|--------------|--------|------|------|");\r
- proc(imgDir, delta, gpxFile.mapTRKSEG, exif, gpxFile);\r
- System.out.println("|--------------------------------|--------------------|--------------------|--------------|--------------|--------|------|------|");\r
-\r
- // GPX出力\r
- gpxFile.output(outDir);\r
- }\r
- \r
- \r
- /**\r
- * 再帰メソッド\r
- * @throws ParseException \r
- * @throws IOException \r
- * @throws ImageReadException \r
- * @throws ImageWriteException \r
- */\r
- boolean proc(File dir, long delta, ElementMapTRKSEG mapTRKSEG, boolean exifWrite, GpxFile gpxFile) throws ParseException, ImageReadException, IOException, ImageWriteException {\r
- boolean ret = false;\r
- File[] files = dir.listFiles(new JpegFileFilter());\r
- Arrays.sort(files, new FileSort());\r
- for (File image : files) {\r
- System.out.print(String.format("|%-32s|", image.getName()));\r
- if (image.isDirectory()) {\r
- ret = proc(image, delta, mapTRKSEG, exifWrite, gpxFile);\r
- continue;\r
- }\r
- \r
- String imageName = image.getName();\r
- if (!checkFile(imageName)) {\r
- System.out.println(String.format("%20s ", "it is not image file."));\r
- continue;\r
- }\r
- \r
- Discripter result = procImageFile(image, delta, mapTRKSEG, exifWrite, gpxFile);\r
- ret |= result.ret;\r
- switch (result.control) {\r
- case Discripter.CONTINUE:\r
- continue;\r
- case Discripter.BREAK:\r
- break;\r
- }\r
- }\r
- return ret;\r
- }\r
- \r
- class Discripter {\r
- static final int NEXT = 0;\r
- static final int CONTINUE = -1;\r
- static final int BREAK = 1;\r
- \r
- public boolean ret;\r
- public int control;\r
- public Discripter(boolean ret) {\r
- this.ret = ret;\r
- this.control = Discripter.NEXT;\r
- }\r
- }\r
- \r
- Discripter procImageFile(File imageFile, long delta, ElementMapTRKSEG mapTRKSEG, boolean exifWrite, GpxFile gpxFile) throws ParseException, ImageReadException, IOException, ImageWriteException {\r
- Discripter result = new Discripter(false);\r
- \r
- // itime <-- 画像ファイルの撮影時刻\r
- // ファイルの更新日時/EXIFの撮影日時\r
- Date itime = new Date(imageFile.lastModified());\r
- if (this.exifBase) {\r
- ImageMetadata meta = Imaging.getMetadata(imageFile);\r
- JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;\r
- if (jpegMetadata == null) {\r
- // "'%s'にEXIF情報がありません"\r
- System.out.println(\r
- String.format(\r
- i18n.getString("msg.140"), \r
- imageFile.getAbsolutePath()\r
- )\r
- );\r
- result.control = Discripter.CONTINUE;\r
- return result;\r
- }\r
- @SuppressWarnings("LocalVariableHidesMemberVariable")\r
- TiffImageMetadata exif = jpegMetadata.getExif();\r
- if (exif == null) {\r
- // "'%s'にEXIF情報がありません"\r
- System.out.println(\r
- String.format(\r
- i18n.getString("msg.140"), \r
- imageFile.getAbsolutePath()\r
- )\r
- );\r
- result.control = Discripter.CONTINUE;\r
- return result;\r
- }\r
- String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];\r
- itime = ImportPicture.toEXIFDate(dateTimeOriginal);\r
- }\r
- System.out.print(String.format("%20s|", toUTCString(itime)));\r
-\r
- // uktime <-- 画像撮影時刻に対応するGPX時刻(補正日時)\r
- Date correctedtime = new Date(itime.getTime() + delta);\r
- System.out.print(String.format("%20s|", toUTCString(correctedtime)));\r
-\r
- // 時刻uktimeにおける<magver>をtrkptに追加する\r
- String eleStr = "-";\r
- String magvarStr = "-";\r
- String speedStr = "-";\r
- double latitude = 90.5D;\r
- double longitude = 180.5D;\r
- TagTrkpt trkptT = null;\r
-\r
- for (Map.Entry<Date,ElementMapTRKPT> map : mapTRKSEG.entrySet()) {\r
- ElementMapTRKPT mapTRKPT = map.getValue();\r
- trkptT = mapTRKPT.getValue(correctedtime);\r
- if (trkptT != null) {\r
- break;\r
- }\r
- }\r
-\r
- if (trkptT == null) {\r
- System.out.print(String.format("%-14s|%-14s|", "", ""));\r
- System.out.println(String.format("%8s|%6s|%6s|", "", "", ""));\r
- if (!this.param_ImgOutputAll) {\r
- result.control = Discripter.CONTINUE;\r
- return result;\r
- }\r
- }\r
- else {\r
- latitude = trkptT.lat;\r
- longitude = trkptT.lon;\r
- \r
- if (trkptT.eleStr != null) {\r
- eleStr = trkptT.eleStr;\r
- }\r
- \r
- if (trkptT.magvarStr != null) {\r
- magvarStr = trkptT.magvarStr;\r
- }\r
- \r
- if (trkptT.speedStr != null) {\r
- speedStr = trkptT.speedStr;\r
- }\r
- //System.out.print(String.format("%-14s|%-14s|", (new Double(latitude)).toString(), (new Double(longitude)).toString()));\r
- System.out.print(String.format("%14.10f|%14.10f|", latitude, longitude));\r
- System.out.println(String.format("%8s|%6s|%6s|", eleStr, magvarStr, speedStr));\r
- }\r
-\r
- result.ret = true;\r
- outDir.mkdir();\r
-\r
- if (exifWrite) {\r
- exifWrite(imageFile, correctedtime, trkptT);\r
-\r
- if (Boolean.parseBoolean(params.getProperty(AppParameters.GPX_OUTPUT_WPT))) {\r
- if (trkptT != null) {\r
- Element temp = gpxFile.createWptTag(imageFile, imgDir, itime.getTime(), trkptT.trkpt);\r
- gpxFile.gpx.appendChild(temp);\r
- }\r
- }\r
- }\r
- else {\r
- if (this.param_ImgOutputAll) {\r
- // EXIFの変換を伴わない単純なファイルコピー\r
- FileInputStream sStream = new FileInputStream(imageFile);\r
- FileInputStream dStream = new FileInputStream(new File(outDir, imageFile.getName()));\r
- FileChannel srcChannel = sStream.getChannel();\r
- FileChannel destChannel = dStream.getChannel();\r
- try {\r
- srcChannel.transferTo(0, srcChannel.size(), destChannel);\r
- }\r
- finally {\r
- srcChannel.close();\r
- destChannel.close();\r
- sStream.close();\r
- dStream.close();\r
- }\r
- }\r
- }\r
- result.control = Discripter.NEXT;\r
- return result;\r
- }\r
- \r
- void exifWrite(File imageFile, Date correctedtime, TagTrkpt trkptT) throws ImageReadException, IOException, ImageWriteException {\r
- DecimalFormat yearFormatter = new DecimalFormat("0000");\r
- DecimalFormat monthFormatter = new DecimalFormat("00");\r
- DecimalFormat dayFormatter = new DecimalFormat("00");\r
- \r
- TiffOutputSet outputSet = null;\r
-\r
- ImageMetadata meta = Imaging.getMetadata(imageFile);\r
- JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;\r
- if (jpegMetadata != null) {\r
- @SuppressWarnings("LocalVariableHidesMemberVariable")\r
- TiffImageMetadata exif = jpegMetadata.getExif();\r
- if (exif != null) {\r
- outputSet = exif.getOutputSet();\r
- }\r
- }\r
-\r
- if (outputSet == null) {\r
- outputSet = new TiffOutputSet();\r
- }\r
-\r
- //---- EXIF_TAG_DATE_TIME_ORIGINAL / 「撮影日時/オリジナル画像の生成日時」----\r
- TiffOutputDirectory exifDir = outputSet.getOrCreateExifDirectory();\r
- {\r
- Calendar cal = Calendar.getInstance();\r
- cal.setTimeZone(TimeZone.getTimeZone("UTC"));\r
- cal.setTime(correctedtime);\r
- exifDir.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);\r
- exifDir.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, ImportPicture.toEXIFString(cal.getTime()));\r
- }\r
-\r
- //---- EXIF GPS_TIME_STAMP ----\r
- TiffOutputDirectory gpsDir = outputSet.getOrCreateGPSDirectory();\r
- {\r
- Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));\r
- cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));\r
- cal.setTime(correctedtime);\r
- final String yearStr = yearFormatter.format(cal.get(Calendar.YEAR));\r
- final String monthStr = monthFormatter.format(cal.get(Calendar.MONTH) + 1);\r
- final String dayStr = dayFormatter.format(cal.get(Calendar.DAY_OF_MONTH));\r
- final String dateStamp = yearStr +":"+ monthStr +":"+ dayStr;\r
-\r
- gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP);\r
- gpsDir.add(\r
- GpsTagConstants.GPS_TAG_GPS_TIME_STAMP,\r
- RationalNumber.valueOf(cal.get(Calendar.HOUR_OF_DAY)),\r
- RationalNumber.valueOf(cal.get(Calendar.MINUTE)),\r
- RationalNumber.valueOf(cal.get(Calendar.SECOND))\r
- );\r
- gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP);\r
- gpsDir.add(GpsTagConstants.GPS_TAG_GPS_DATE_STAMP, dateStamp);\r
- }\r
-\r
- if (trkptT != null) {\r
- //---- EXIF GPS elevation/ALTITUDE ----\r
- if (trkptT.eleStr != null) {\r
- final double altitude = Double.parseDouble(trkptT.eleStr);\r
- gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_ALTITUDE);\r
- gpsDir.add(GpsTagConstants.GPS_TAG_GPS_ALTITUDE, RationalNumber.valueOf(altitude));\r
- }\r
-\r
- //---- EXIF GPS magvar/IMG_DIRECTION ----\r
- if (trkptT.magvarStr != null) {\r
- final double magvar = Double.parseDouble(trkptT.magvarStr);\r
- gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION);\r
- gpsDir.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(magvar));\r
- }\r
-\r
- //---- EXIF GPS_ ----\r
- outputSet.setGPSInDegrees(trkptT.lon, trkptT.lat);\r
- }\r
-\r
- ExifRewriter rewriter = new ExifRewriter();\r
- try (FileOutputStream fos = new FileOutputStream(new File(outDir, imageFile.getName()))) {\r
- rewriter.updateExifMetadataLossy(imageFile, fos, outputSet);\r
- }\r
- }\r
- \r
- // 基準時刻ファイルの「更新日時」を使って時刻合わせを行う。\r
- // argv[1] --> AppParameters.IMG_BASE_FILE に置き換え\r
- // File baseFile = new File(this.imgDir, this.params.getProperty(AppParameters.IMG_BASE_FILE));\r
- private Date adjustTime(File baseFile) throws ImageReadException, IOException, ParseException {\r
- if (exifBase) {\r
- ImageMetadata meta = Imaging.getMetadata(baseFile);\r
- JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;\r
- if (jpegMetadata == null) {\r
- // "'%s'にEXIF情報がありません"\r
- System.out.println(\r
- String.format(\r
- i18n.getString("msg.140"), \r
- baseFile.getAbsolutePath()\r
- )\r
- );\r
- return null;\r
- }\r
- @SuppressWarnings("LocalVariableHidesMemberVariable")\r
- TiffImageMetadata exif = jpegMetadata.getExif();\r
- if (exif == null) {\r
- // "'%s'にEXIF情報がありません"\r
- System.out.println(\r
- String.format(\r
- i18n.getString("msg.140"), \r
- baseFile.getAbsolutePath()\r
- )\r
- );\r
- return null;\r
- }\r
- String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];\r
- return new Date(ImportPicture.toEXIFDate(dateTimeOriginal).getTime());\r
- }\r
- else {\r
- return new Date(baseFile.lastModified());\r
- }\r
- }\r
-\r
- /**\r
- * 対象は '*.JPG' のみ対象とする\r
- * @return \r
- * @param name\r
- */\r
- public static boolean checkFile(String name) {\r
- return ((name != null) && name.toUpperCase().endsWith(".JPG"));\r
- }\r
-\r
- /**\r
- * DateをEXIFの文字列に変換する。\r
- * 注意:EXiFの撮影時刻はUTC時間ではない\r
- * @param localdate\r
- * @return\r
- */\r
- public static String toEXIFString(Date localdate) {\r
- DateFormat dfUTC = new SimpleDateFormat(EXIF_DATE_TIME_FORMAT_STRING);\r
- return dfUTC.format(localdate);\r
- }\r
- \r
- /**\r
- * EXIFの文字列をDateに変換する。\r
- * 注意:EXiFの撮影時刻はUTC時間ではない\r
- * @param timeStr\r
- * @return\r
- * @throws ParseException\r
- */\r
- public static Date toEXIFDate(String timeStr) throws ParseException {\r
- DateFormat dfUTC = new SimpleDateFormat(EXIF_DATE_TIME_FORMAT_STRING);\r
- //dfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));\r
- return dfUTC.parse(timeStr);\r
- }\r
- \r
- public static String toUTCString(Date localdate) {\r
- DateFormat dfUTC = new SimpleDateFormat(TIME_FORMAT_STRING);\r
- dfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));\r
- return dfUTC.format(localdate);\r
- }\r
- \r
- public static Date toUTCDate(String timeStr) throws ParseException {\r
- DateFormat dfUTC = new SimpleDateFormat(TIME_FORMAT_STRING);\r
- dfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));\r
- return dfUTC.parse(timeStr);\r
- }\r
- \r
- static String getShortPathName(File dir, File iFile) {\r
- String dirPath = dir.getAbsolutePath();\r
- String filePath = iFile.getAbsolutePath();\r
- if (filePath.startsWith(dirPath)) {\r
- return filePath.substring(dirPath.length()+1);\r
- }\r
- else {\r
- return filePath;\r
- }\r
- }\r
- \r
- /**\r
- * ファイル名の順序に並び替えるためのソートクラス\r
- * \r
- * @author hayashi\r
- */\r
- static class FileSort implements Comparator<File> {\r
- @Override\r
- public int compare(File src, File target){\r
- int diff = src.getName().compareTo(target.getName());\r
- return diff;\r
- }\r
- }\r
-\r
- /**\r
- * JPEGファイルフィルター\r
- * @author yuu\r
- */\r
- class JpegFileFilter implements FilenameFilter {\r
- @Override\r
- public boolean accept(File dir, String name) {\r
- return name.toUpperCase().matches(".*\\.JPG$");\r
- }\r
- }\r
-}
\ No newline at end of file