Last active
December 5, 2015 00:21
-
-
Save tatesuke/e582b181aaef8d83c147 to your computer and use it in GitHub Desktop.
ContorGPSで撮影した動画ファイルからnmeaを取り出す ref: http://qiita.com/tatesuke/items/9936a82a3c464eb3e12d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java NmeaExporter [入力ファイル] > [出力ファイル] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.PushbackInputStream; | |
public class NmeaExporter { | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
if (args.length == 0) { | |
usage(); | |
System.exit(1); | |
} | |
try (PushbackInputStream is = new PushbackInputStream(new FileInputStream(args[0]), 256)) { | |
export(is); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
private static void export(PushbackInputStream is) throws IOException { | |
while (true) { | |
byte[] buf = new byte[6]; | |
// ファイル終端に達したら終了 | |
int bytes = is.read(buf); | |
if (bytes < buf.length) { | |
break; | |
} | |
// 欲しいデータじゃなければスキップ | |
if (!equals(buf, "$GPGGA") && !equals(buf, "$GPRMC")) { | |
is.unread(buf, 1, buf.length - 1); | |
continue; | |
} | |
// ここまで来ると欲しいデータなので改行文字(\r\n)まで出力 | |
System.out.print(new String(buf)); | |
while (true) { | |
buf = new byte[2]; | |
is.read(buf); | |
if (equals(buf, "\r\n")) { | |
break; | |
} | |
System.out.print((char) buf[0]); | |
is.unread(buf, 1, 1); | |
} | |
System.out.println(""); | |
} | |
} | |
private static boolean equals(byte[] buf, String str) { | |
if (buf.length != str.length()) { | |
return false; | |
} | |
for (int i = 0; i < buf.length; i++) { | |
if ((char) buf[i] != str.charAt(i)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private static void usage() { | |
System.err.println("ファイルパスを渡してください。"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment