From 5f150677af41c7c833cf49da072d06fe67546ceb Mon Sep 17 00:00:00 2001 From: hayashi Date: Sun, 1 Apr 2018 12:19:31 +0900 Subject: [PATCH] "Restamp.main()" --- .gitignore | 4 + importPicture/AdjustTime2.sh | 1 - importPicture/src/AdjustTime2.sh | 2 +- importPicture/src/osm/jp/gpx/ImportPicture.java | 1 - importPicture/src/osm/jp/gpx/Restamp.java | 248 ++++++++++++++++++++++++ importPicture/test/osm/jp/gpx/RestampTest.java | 56 ++++++ 6 files changed, 309 insertions(+), 3 deletions(-) delete mode 100755 importPicture/AdjustTime2.sh create mode 100644 importPicture/src/osm/jp/gpx/Restamp.java create mode 100644 importPicture/test/osm/jp/gpx/RestampTest.java diff --git a/.gitignore b/.gitignore index 58236c9..81d9b6d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ /importPicture/testdata/*~ /importPicture/testdata/cameradata/ /importPicture/testdata/output +/importPicture/nbproject/private/ +/importPicture/build/ +/importPicture/dist/ +/leaflet/nbproject/private/ \ No newline at end of file diff --git a/importPicture/AdjustTime2.sh b/importPicture/AdjustTime2.sh deleted file mode 100755 index cc5ff42..0000000 --- a/importPicture/AdjustTime2.sh +++ /dev/null @@ -1 +0,0 @@ -java -cp .:AdjustTime2.jar:commons-imaging-1.0-SNAPSHOT.jar osm.jp.gpx.matchtime.gui.AdjustTime diff --git a/importPicture/src/AdjustTime2.sh b/importPicture/src/AdjustTime2.sh index cc5ff42..4a4f609 100644 --- a/importPicture/src/AdjustTime2.sh +++ b/importPicture/src/AdjustTime2.sh @@ -1 +1 @@ -java -cp .:AdjustTime2.jar:commons-imaging-1.0-SNAPSHOT.jar osm.jp.gpx.matchtime.gui.AdjustTime +java -cp .:AdjustTime2.jar:commons-imaging-1.0-SNAPSHOT.jar:commons-compress-1.14.jar osm.jp.gpx.matchtime.gui.AdjustTime diff --git a/importPicture/src/osm/jp/gpx/ImportPicture.java b/importPicture/src/osm/jp/gpx/ImportPicture.java index f617918..0635e73 100644 --- a/importPicture/src/osm/jp/gpx/ImportPicture.java +++ b/importPicture/src/osm/jp/gpx/ImportPicture.java @@ -516,7 +516,6 @@ public class ImportPicture extends Thread { } if (outputSet == null) { - System.out.println("added : new tiff output set"); outputSet = new TiffOutputSet(); } diff --git a/importPicture/src/osm/jp/gpx/Restamp.java b/importPicture/src/osm/jp/gpx/Restamp.java new file mode 100644 index 0000000..140b249 --- /dev/null +++ b/importPicture/src/osm/jp/gpx/Restamp.java @@ -0,0 +1,248 @@ +package osm.jp.gpx; + +import java.io.*; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Comparator; +import java.util.Date; +import java.util.logging.LogManager; +import java.util.logging.Logger; +import org.apache.commons.imaging.ImageReadException; + +/** + * 動画から一定間隔で切り出したIMAGEのファイル更新日時を書き換える + * + * @author yuu + */ +public class Restamp extends Thread { + /** + * 実行中に発生したExceptionを保持する場所 + */ + public Exception ex = null; + + /** + * ログ設定プロパティファイルのファイル内容 + */ + protected static final String LOGGING_PROPERTIES_DATA + = "handlers=java.util.logging.ConsoleHandler\n" + + ".level=FINEST\n" + + "java.util.logging.ConsoleHandler.level=INFO\n" + + "java.util.logging.ConsoleHandler.formatter=osm.jp.gpx.YuuLogFormatter"; + + /** + * static initializer によるログ設定の初期化 + */ + public static final Logger logger = Logger.getLogger("CommandLogging"); + static { + InputStream inStream = null; + try { + inStream = new ByteArrayInputStream(LOGGING_PROPERTIES_DATA.getBytes("UTF-8")); + try { + LogManager.getLogManager().readConfiguration(inStream); + logger.config("ログ設定: LogManagerを設定しました。"); + } + catch (IOException e) { + logger.warning("ログ設定: LogManager設定の際に例外が発生しました。:" + e.toString()); + } + } + catch (UnsupportedEncodingException e) { + logger.severe("ログ設定: UTF-8エンコーディングがサポートされていません。:" + e.toString()); + } + finally { + try { + if (inStream != null) { + inStream.close(); + } + } catch (IOException e) { + logger.warning("ログ設定: ログ設定プロパティファイルのストリームクローズ時に例外が発生しました。:"+ e.toString()); + } + } + } + + /** + * メイン + * 動画から一定間隔で切り出したIMAGEのファイル更新日時を書き換える + * + * ・画像ファイルの更新日付を書き換えます。(Exi情報は無視します) + * ※ 指定されたディレクトリ内のすべての'*.jpg'ファイルを処理の対象とします + * ・画像は連番形式(名前順に並べられること)の名称となっていること + * + * パラメータ + * ・対象のフォルダ(ディレクトリ内のすべての'*.jpg'ファイルを処理の対象とします) + * ・基準となる画像 + * ・基準画像の正しい日時 + * ・画像ファイルの間隔(秒) + * + * exp) $ java -cp .:AdjustTime.jar:commons-imaging-1.0-SNAPSHOT.jar [AdjustTime.ini] + * exp) > java -cp .;AdjustTime.jar;commons-imaging-1.0-SNAPSHOT.jar [AdjustTime.ini] + * + * 1. 予め、動画から画像を切り出す + *   ソースファイル(mp4ファイル); 「-i 20160427_104154.mp4」 + * 出力先: 「-f image2 img/%06d.jpg」 imgフォルダに6桁の連番ファイルを差出力する + *   切り出し開始秒数→ 「-ss 0」 (ファイルの0秒から切り出し開始) + *   切り出し間隔; 「-r 30」 (1秒間隔=30fps間隔) + * ``` + * $ cd /home/yuu/Desktop/OSM/20180325_横浜新道 + * $ ffmpeg -ss 0 -i 20160427_104154.mp4 -f image2 -r 15 img/%06d.jpg + * ``` + * + * 2. ファイルの更新日付を書き換える + * ``` + * $ cd /home/yuu/Desktop/workspace/AdjustTime/importPicture/dist + * $ java -cp .:AdjustTime2.jar osm.jp.gpx.Restamp /home/yuu/Desktop/OSM/20180325_横浜新道/img 000033.jpg 2018-03-25_12:20:32 003600.jpg 2018-03-25_13:20:09 + * ``` + * + * @param argv + * argv[0] = 画像ファイルが格納されているディレクトリ --> imgDir + * argv[1] = 時刻補正の基準とする画像ファイル --> baseFile + * argv[2] = 基準画像ファイルの精確な撮影日時 "yyyy-MM-dd_HH:mm:ss" --> baseTime + * argv[3] = 時刻補正の基準とする画像ファイル --> baseFile + * argv[4] = 基準画像ファイルの精確な撮影日時 "yyyy-MM-dd_HH:mm:ss" --> baseTime + * + * @throws IOException + * @throws ImageReadException + */ + public static void main(String[] argv) throws Exception + { + if (argv.length < 5) { + System.out.println("java Restamp "); + return; + } + + File imgDir = new File(argv[0]); + if (!imgDir.exists()) { + System.out.println("[error] が存在しません。"); + return; + } + if (!imgDir.isDirectory()) { + System.out.println("[error] がフォルダじゃない"); + return; + } + + File baseFile1 = new File(imgDir, argv[1]); + if (!baseFile1.exists()) { + System.out.println("[error] が存在しません。"); + return; + } + if (!baseFile1.isFile()) { + System.out.println("[error] がファイルじゃない"); + return; + } + + DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'_'HH:mm:ss"); + Date baseTime1 = df1.parse(argv[2]); + + File baseFile2 = new File(imgDir, argv[3]); + if (!baseFile2.exists()) { + System.out.println("[error] が存在しません。"); + return; + } + if (!baseFile2.isFile()) { + System.out.println("[error] がファイルじゃない"); + return; + } + + DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'_'HH:mm:ss"); + Date baseTime2 = df2.parse(argv[4]); + + Restamp obj = new Restamp(); + obj.setUp(imgDir, baseFile1, baseTime1, baseFile2, baseTime2); + } + + public File imgDir; + public Date baseTime1; + public Date baseTime2; + public int bCount1 = 0; + public int bCount2 = 0; + public long span = 0; + public ArrayList jpgFiles = new ArrayList<>(); + + @SuppressWarnings("Convert2Lambda") + public void setUp(File imgDir, File baseFile1, Date baseTime1, File baseFile2, Date baseTime2) throws Exception { + // 指定されたディレクトリ内のGPXファイルすべてを対象とする + File[] files = imgDir.listFiles(); + java.util.Arrays.sort(files, new java.util.Comparator() { + @Override + public int compare(File file1, File file2){ + return file1.getName().compareTo(file2.getName()); + } + }); + bCount1 = 0; + bCount2 = 0; + boolean base1 = false; + boolean base2 = false; + for (File file : files) { + if (file.isFile()) { + String filename = file.getName().toUpperCase(); + if (filename.toUpperCase().endsWith(".JPG")) { + this.jpgFiles.add(file); + bCount1 += (base1 ? 0 : 1); + bCount2 += (base2 ? 0 : 1); + if (file.getName().equals(baseFile1.getName())) { + base1 = true; + } + if (file.getName().equals(baseFile2.getName())) { + base2 = true; + } + } + } + } + + try { + // imgDir内の画像ファイルを処理する + long span = baseTime2.getTime() - baseTime1.getTime(); + span = span / (bCount2 - bCount1); + int i = 0; + for (File jpgFile : this.jpgFiles) { + long deltaMsec = (i - bCount1) * span; + i++; + Calendar cal = Calendar.getInstance(); + cal.setTime(baseTime1); + cal.add(Calendar.MILLISECOND, (int) deltaMsec); + jpgFile.setLastModified(cal.getTimeInMillis()); + } + } + catch(Exception e) { + e.printStackTrace(); + this.ex = new Exception(e); + } + } + + /** + * 対象は '*.JPG' のみ対象とする + * @return + * @param name + */ + public static boolean checkFile(String name) { + return ((name != null) && name.toUpperCase().endsWith(".JPG")); + } + + /** + * ファイル名の順序に並び替えるためのソートクラス + * + * @author hayashi + */ + static class FileSort implements Comparator { + @Override + public int compare(File src, File target){ + int diff = src.getName().compareTo(target.getName()); + return diff; + } + } + + /** + * JPEGファイルフィルター + * @author yuu + */ + class JpegFileFilter implements FilenameFilter { + @Override + public boolean accept(File dir, String name) { + if (name.toUpperCase().matches(".*\\.JPG$")) { + return true; + } + return false; + } + } +} \ No newline at end of file diff --git a/importPicture/test/osm/jp/gpx/RestampTest.java b/importPicture/test/osm/jp/gpx/RestampTest.java new file mode 100644 index 0000000..caa8c3c --- /dev/null +++ b/importPicture/test/osm/jp/gpx/RestampTest.java @@ -0,0 +1,56 @@ +package osm.jp.gpx; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author yuu + */ +public class RestampTest { + + public RestampTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of main method, of class Restamp. + */ + @Test + public void testMain() { + try { + System.out.println("main"); + String[] argv = new String[]{ + "/home/yuu/Desktop/OSM/20180325_横浜新道/img", + "000033.jpg", + "2018-03-25_12:20:32", + "003752.jpg", + "2018-03-25_13:22:42" + }; + Restamp.main(argv); + } + catch (Exception e) { + fail(); + } + } + +} -- 2.11.0