initial commit

This commit is contained in:
Hendrik Schutter 2019-02-21 14:57:47 +01:00
parent 5d488ec56f
commit 6b4c3dbc5d
7 changed files with 933 additions and 0 deletions

201
src/DBController.java Normal file
View File

@ -0,0 +1,201 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
class DBController
{
private Connection connection;
private String DB_PATH_Linux;
public String dbname = "tlcIndex";
public void main()
{
try {
connection = DriverManager
.getConnection("jdbc:sqlite:" + DB_PATH_Linux + dbname + ".db");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DBController(String projectPath)
{
this.DB_PATH_Linux = projectPath;
}
public void connectDatabase()
{ // connect to database
System.out.println("Verbinde ... DB name: " + dbname);
try {
if (connection != null)
return;
connection = DriverManager
.getConnection("jdbc:sqlite:" + DB_PATH_Linux + dbname + ".db");
if (!connection.isClosed())
System.out.println("DB Datei-Verbindung erstellt");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run()
{
try {
if (!connection.isClosed() && connection != null) {
connection.close();
if (connection.isClosed())
System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
public void createTableImages()
{ // create table position
System.out.println("Erstelle Tabelle images");
try {
Statement stmt = connection.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS images;");
stmt.executeUpdate(
"CREATE TABLE images (imageCounter, path, day, month, year, hour, minute, size, brightness);");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
}
}
public void fillImages(int imageCounter, String path, int day, int month,
int year, int hour, int minute, int size, int brightness)
{ // create new data in table
// System.out.println("Erstelle neuen images eintrag");
try {
PreparedStatement ps = connection.prepareStatement(
"INSERT INTO images VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
ps.setInt(1, imageCounter); // primary
ps.setString(2, path);
ps.setInt(3, day);
ps.setInt(4, month);
ps.setInt(5, year);
ps.setInt(6, hour);
ps.setInt(7, minute);
ps.setInt(8, size);
ps.setInt(9, brightness);
ps.addBatch();
connection.setAutoCommit(false);
ps.executeBatch(); // SQL execution
connection.setAutoCommit(true);
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
}
}
public List<Image> query(String pQuery)
{
List<Image> list = new ArrayList<Image>();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(pQuery);
while (rs.next()) {
list.add(new Image(rs.getInt("imageCounter"), rs.getString("path"),
rs.getInt("day"), rs.getInt("month"), rs.getInt("year"),
rs.getInt("hour"), rs.getInt("minute"), rs.getInt("size"),
rs.getInt("brightness")));
} // end while
return list;
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return null;
}
}
public String getPathFromIndex(int pCounter)
{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT path FROM images WHERE imageCounter = "
+ pCounter + " ;");
return rs.getString("path");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return "Error 404";
}
}
public int getHourFromIndex(int pCounter)
{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT hour FROM images WHERE imageCounter = "
+ pCounter + " ;");
return rs.getInt("hour");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return 404;
}
}
public int getMinuteFromIndex(int pCounter)
{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT minute FROM images WHERE imageCounter = "
+ pCounter + " ;");
return rs.getInt("minute");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return 404;
}
}
public int getDayFromIndex(int pCounter)
{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT day FROM images WHERE imageCounter = "
+ pCounter + " ;");
return rs.getInt("day");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return 404;
}
}
public int getSizeFromIndex(int pCounter)
{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT size FROM images WHERE imageCounter = "
+ pCounter + " ;");
return rs.getInt("size");
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
return 404;
}
}
}

39
src/Exif.java Normal file
View File

@ -0,0 +1,39 @@
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import java.io.File;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
public class Exif
{
public Calendar getDate(String path)
{
File file = new File(path);
Date date = null;
Calendar calendar = Calendar.getInstance();
try {
Metadata metadata = ImageMetadataReader.readMetadata(file);
ExifSubIFDDirectory directory = metadata
.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
ZonedDateTime cetTimeZoned = ZonedDateTime.of(
new java.sql.Timestamp(date.getTime()).toLocalDateTime(),
ZoneId.of("CET"));
date = java.sql.Timestamp.valueOf(cetTimeZoned
.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
calendar.setTime(date);
return calendar;
} catch (ImageProcessingException e) {
} catch (IOException e) {
}
return calendar;
}
}

82
src/Image.java Normal file
View File

@ -0,0 +1,82 @@
public class Image
{
private int imageCounter;
private String path;
private int day;
private int month;
private int year;
private int hour;
private int minute;
private int size;
private int brightness;
public Image(int pImageCounter, String pPath, int pDay, int pMonth,
int pYear, int pHour, int pMinute, int pSize, int pBrightnes)
{
this.imageCounter = pImageCounter;
this.path = pPath;
this.day = pDay;
this.month = pMonth;
this.year = pYear;
this.hour = pHour;
this.minute = pMinute;
this.size = pSize;
this.brightness = pBrightnes;
}
public int getImageCounter()
{
return imageCounter;
}
public String getPath()
{
return path;
}
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSize()
{
return size;
}
public int getBrightness()
{
return brightness;
}
}

156
src/ImageFinder.java Normal file
View File

@ -0,0 +1,156 @@
import java.io.File;
import java.io.FileFilter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class ImageFinder
{
short maxYears = 5;
int lengthRootFolder;
String rootFolder;
int currentYear = 0;
int currentMonth = 0;
String[] monthNames = { "01_January", "02_February", "03_March",
"04_April", "05_May", "06_June", "07_July", "08_August",
"09_September", "10_October", "11_November", "12_December" };
Boolean[][] month = new Boolean[maxYears][12];
String[] years = new String[maxYears];
public ImageFinder(String rootFolder)
{
this.rootFolder = rootFolder;
lengthRootFolder = rootFolder.length();
}
String findStartyear()
{
boolean next = true;
int i = 0; // start year
while (next) {
int currentyear = i;
if (Files.isDirectory(Paths.get(rootFolder + currentyear + "/"))) {
next = false; // When a year folder found
} else {
i = i + 1;
next = true;
}
}
return Integer.toString(i); // return first year folder name
}
public void findYears(int yearCount, String startyear)
{
for (int i = 0; i <= years.length; i++) {
int currentyear = Integer.parseInt(startyear) + (i * 1);
if (Files.isDirectory(Paths.get(rootFolder + currentyear + "/"))) {
// System.out.println("Jahr " + currentyear + " gefunden!");
years[i] = Integer.toString(currentyear);
yearCount = i + 1;
} else {
// System.out.println("Jahr " + currentyear + " nicht gefunden!");
}
}
}
public void findMonths(int yearCount)
{
for (int i = 0; i < years.length; i++) {
// System.out.println(i + " Year-Array: " + years[i]);
if (years[i] != null) {
for (int j = 0; j < monthNames.length; j++) {
if (Files.isDirectory(Paths
.get(rootFolder + years[i] + "/" + monthNames[j] + "/"))) {
month[i][j] = true;
} else {
month[i][j] = false;
}
}
} else {
break;
}
}
}
public void printFoundDirectories()
{
for (int i = 0; i < years.length; i++) {
if (years[i] != null) {
System.out.println("Jahr: " + years[i]);
for (int j = 0; j < 12; j++) {
if (month[i][j]) {
System.out.print(monthNames[j] + ", ");
}
}
System.out.println("");
}
}
}
public String getImagePath(int imageCounter)
{
String path = null;
for (int i = currentYear; i < years.length; i++) {
if (years[i] != null) {
for (int j = currentMonth; j < month[i].length; j++) {
if(month[i][j]) {
//System.out.println("Patth: " + rootFolder + years[i] + "/" + monthNames[j] + "/" );
File dir = new File(rootFolder + years[i] + "/" + monthNames[j] + "/");
FileFilter fileFilter = new WildcardFileFilter(
"*" + convertImageCounter(imageCounter) + "*.jpg");
File[] files = dir.listFiles(fileFilter);
path = Arrays.toString(files);
path = path.replace("[", "");
path = path.replace("]", "");
if ((path == "null") || (files.length == 0)) {
//Nicht gefunden
//System.out.println("error404");
}else {
//Gefunden
currentMonth = j;
currentYear = i;
return path.substring(lengthRootFolder);
}
}
}
currentMonth = 0;
}
}
return "404";
}
private String convertImageCounter(int imageCounter)
{
switch ((int) (Math.log10(imageCounter) + 1)) {
case 1:
return "00000" + Integer.toString(imageCounter);
case 2:
return "0000" + Integer.toString(imageCounter);
case 3:
return "000" + Integer.toString(imageCounter);
case 4:
return "00" + Integer.toString(imageCounter);
case 5:
return "0" + Integer.toString(imageCounter);
case 6:
return Integer.toString(imageCounter);
default:
return "000000";
}
}
}

168
src/Main.java Normal file
View File

@ -0,0 +1,168 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;
public class Main
{
// read from UI later
static String rootFolderPath; // = "XXX/home/hendrik/FTP/camera01/";
// read from UI later
static String projectPath; // = "XXX/home/hendrik/Schreibtisch/";
// read from UI later
static String outputPath; // = "XXX/home/hendrik/Schreibtisch/testeritis/";
// read from UI later
static int maxImgageCounter; // = 83584; // read from UI later 16819
static boolean log = false; // Don´t output image counter
static boolean modi = true; // Create Database // False -> Copy/Link images
// java -jar TLC_PostSW.jar -log -m createDB -r /home/hendrik/FTP/camera01/
// -p /home/hendrik/Schreibtisch/ -o /home/hendrik/Schreibtisch/testeritis/
// -n 83584
public static void main(String[] args)
{
for (int i = 0; i < args.length; i++) {
//System.out.println(i + ": " +args[i]);
String value = args[i];
if (value.equals("-log")) {
System.out.println("Logging will be used");
log = true;
}
if (value.equals("-m")) {
i++;
if (args[i].equals("createDB")) {
modi = true;
System.out.println("[Modi] Index images and create DB");
} else {
modi = false;
System.out.println("[Modi] Copy or Link images");
}
}
if (value.equals("-r")) {
i++;
rootFolderPath = args[i];
System.out.println("[rootFolderPath] " + rootFolderPath);
}
if (value.equals("-p")) {
i++;
projectPath = args[i];
System.out.println("[projectPath] " + projectPath);
}
if (value.equals("-o")) {
i++;
outputPath = args[i];
System.out.println("[outputPath] " + outputPath);
}
if (value.equals("-n")) {
i++;
maxImgageCounter = Integer.parseInt(args[i]);
System.out.println("[maxImgageCounter] " + maxImgageCounter);
}
if (value.equals("-help")) {
System.out.println(
"-log -m createDB -r /home/hendrik/FTP/camera01/ -p /home/hendrik/Schreibtisch/ -o /home/hendrik/Schreibtisch/testeritis/ -n 83584");
System.exit(1);
}
}
//System.exit(1);
if (modi) {
long timeStart;
ImageFinder imgF = new ImageFinder(rootFolderPath);
DBController db = new DBController(projectPath);
Exif exif = new Exif();
db.connectDatabase();
db.createTableImages();
String startyear = imgF.findStartyear();
int yearCount = Integer.parseInt(startyear);
imgF.findYears(yearCount, startyear);
imgF.findMonths(yearCount);
if(log = true) {
imgF.printFoundDirectories();
}
timeStart = System.currentTimeMillis();
System.out.println("Suche ...");
try {
PrintWriter missingTxt = new PrintWriter(
projectPath + "missingImages.txt");
missingTxt.println("Nicht gefundene Bilder: \n");
float multiplicator = (float) (100.0 / maxImgageCounter);
System.out.println("Multiplicator: " + multiplicator);
for (int i = 0; i <= maxImgageCounter; i++) {
if(log == true) {
float percent = multiplicator * i;
System.out.print(i + "/" + maxImgageCounter + " --> ");
System.out.printf("%.2f", percent);
System.out.println("%");
}
String path = imgF.getImagePath(i);
// System.out.println(i + " Path: " + path);
if (path != "404") {
// gefunden
File f = new File(rootFolderPath + path);
int size = (int) f.length();
Calendar calendar = exif.getDate(rootFolderPath + path);
db.fillImages(i, path, calendar.get(Calendar.DAY_OF_MONTH),
(calendar.get(Calendar.MONTH) + 1),
calendar.get(Calendar.YEAR),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), size, 1234);
} else {
// nicht gefunden
System.out.println("Nicht gefunden: " + i);
missingTxt.println(i);
}
}
missingTxt.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double processingTime = ((System.currentTimeMillis() - timeStart)
* 0.001);
System.out.println("Process time: " + processingTime + " seconds.");
} else {
///////////////////////////////////////////////////////////////////
DBController db = new DBController(projectPath);
db.connectDatabase();
QueryImages qi = new QueryImages(db, outputPath, rootFolderPath);
//qi.outputImagesFromHour(0, 18, "copy", true);
//qi.outputImagesFromHourToHour(0, 8, 19, "copy", false);
qi.outputImagesEveryHour(0, 8, 19, "copy", false);
//qi.outputImagesFromHourAndMinute(40177, 8, 30, "copy", true);
// qi.outputImagesFromStartToEnd(16185, 16282, 10, "copy", false,
// 2000000);
}
}
}

79
src/Output.java Normal file
View File

@ -0,0 +1,79 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
public class Output
{
private String outputpath;
private String foldername;
public Output(String path)
{
outputpath = path;
}
public void createFolder()
{
Calendar calendar = Calendar.getInstance();
foldername = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)) + "."
+ Integer.toString((calendar.get(Calendar.MONTH) + 1)) + "."
+ Integer.toString(calendar.get(Calendar.YEAR)) + "_"
+ Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)) + ":"
+ Integer.toString(calendar.get(Calendar.MINUTE)) + "/";
// System.out.println("ordner: " + outputpath+foldername);
File dir = new File(outputpath + foldername);
dir.mkdir();
}
public void copyImage(String rootpath, String imagePath)
{
String imagename = imagePath.substring(imagePath.lastIndexOf("/") + 1);
File src = new File(rootpath + imagePath);
File dst = new File(outputpath + foldername + imagename);
try {
FileUtils.copyFile(src, dst);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Konnte Datei nicht kopieren!");
e.printStackTrace();
}
}
public void linkImage(String rootpath, String imagePath)
{
String imagename = imagePath.substring(imagePath.lastIndexOf("/") + 1);
Path newLink = Paths.get(outputpath + foldername + imagename);
Path target = Paths.get(rootpath + imagePath);
try {
Files.createSymbolicLink(newLink, target);
} catch (IOException x) {
System.err.println(x);
} catch (UnsupportedOperationException x) {
// Some file systems do not support symbolic links.
System.err.println(x);
}
}
public void outputMethod(String method, String rootpath, String imagePath)
{
if (method == "copy") {
copyImage(rootpath, imagePath);
}
if (method == "link") {
linkImage(rootpath, imagePath);
}
}
}

208
src/QueryImages.java Normal file
View File

@ -0,0 +1,208 @@
import java.util.Calendar;
import java.util.List;
public class QueryImages
{
private DBController db;
private Output op;
private String outputPath;
private String rootFolderPath;
public QueryImages(DBController pDB, String pOutputPath,
String pRootFolderPath)
{
this.db = pDB;
this.outputPath = pOutputPath;
this.rootFolderPath = pRootFolderPath;
op = new Output(outputPath);
op.createFolder();
}
public boolean checkIfSunday(int day, int month, int year)
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, (month - 1));
cal.set(Calendar.DAY_OF_MONTH, day);
// System.out.println("CAL: " + cal.getTime());
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
// System.out.println("Sunday! " + day + "." + month + "." + year);
return false;
}
return true;
}
private boolean timeCheck(int pH1, int pM1, int pH2, int pM2, int pInterval)
{
int time1 = (pH1 * 60) + pM1;
int time2 = (pH2 * 60) + pM2;
if ((time2 - time1) >= pInterval) {
return true;
}
return false;
}
public void outputImagesFromHour(int startcounter, int hour, String method, boolean sunday)
{
List<Image> resultList = db.query(
"SELECT imageCounter, path, day, month, year, hour, minute, size, brightness FROM images WHERE hour = "
+ hour + " AND minute <= 5 AND minute > 0 ;");
for (int i = 0; i < resultList.size(); i++) {
Image image = resultList.get(i);
if(image.getImageCounter() < startcounter) {
continue;
}
if (!sunday) {
if (checkIfSunday(image.getDay(), image.getMonth(),
image.getYear())) {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
} else {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
}
resultList = null; //free
}
public void outputImagesFromHourToHour(int startcounter, int starthour, int endhour, String method, boolean sunday)
{
List<Image> resultList = db.query(
"SELECT imageCounter, path, day, month, year, hour, minute, size, brightness FROM images WHERE hour >= "
+ starthour + " AND hour < " + endhour + ";");
for (int i = 0; i < resultList.size(); i++) {
Image image = resultList.get(i);
if(image.getImageCounter() < startcounter) {
continue;
}
if (!sunday) {
if (checkIfSunday(image.getDay(), image.getMonth(),
image.getYear())) {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
} else {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
}
resultList = null; //free
}
public void outputImagesFromHourAndMinute(int startcounter, int hour, int minute, String method, boolean sunday)
{
// List<Image> resultList = db.query(
// "SELECT imageCounter, path, day, month, year, hour, minute, size, brightness FROM images WHERE hour = "
// + hour + " AND minute >= ("+ (minute-3) +") AND minute <= ( "+ (minute+3) + ") ;");
List<Image> resultList = db.query(
"SELECT imageCounter, path, day, month, year, hour, minute, size, brightness FROM images WHERE hour = "
+ hour + " AND minute = " + minute + ";");
for (int i = 0; i < resultList.size(); i++) {
Image image = resultList.get(i);
if(image.getImageCounter() < startcounter) {
//System.out.println(image.getImageCounter());
continue;
}
if (!sunday) {
if (checkIfSunday(image.getDay(), image.getMonth(),
image.getYear())) {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
} else {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
}
resultList = null; //free
}
public void outputImagesFromStartToEnd(int pStart, int pEnd, int pMinute,
String method, boolean sunday, int pSize)
{
op.createFolder();
int hour = db.getHourFromIndex(pStart);
int minute = db.getMinuteFromIndex(pStart);
int day = db.getDayFromIndex(pStart);
for (int i = pStart; i < pEnd; i++) {
String path = db.getPathFromIndex(i);
if ((path == "Error 404") || (path == null)) {
continue;
}
if (day != db.getDayFromIndex(i)) {
// neuer Tag
day = db.getDayFromIndex(i);
hour = db.getHourFromIndex(i);
minute = db.getMinuteFromIndex(i);
}
if (timeCheck(hour, minute, db.getHourFromIndex(i),
db.getMinuteFromIndex(i), pMinute)) {
if (db.getSizeFromIndex(i) > pSize) {
System.out.println("Pfad: " + db.getPathFromIndex(i));
// System.out.println(db.getSizeFromIndex(i));
hour = db.getHourFromIndex(i);
minute = db.getMinuteFromIndex(i);
op.outputMethod(method, rootFolderPath, path);
}
} else {
// System.out.println("Nein: Pfad: " + db.getPathFromIndex(i));
}
}
}
public void outputImagesEveryHour(int startcounter, int starthour, int endhour, String method, boolean sunday)
{
List<Image> resultList = db.query(
"SELECT imageCounter, path, day, month, year, hour, minute, size, brightness FROM images WHERE (hour >= "
+ starthour + " AND hour <= " + endhour + ") AND minute <= 5 AND minute > 0 ;");
for (int i = 0; i < resultList.size(); i++) {
Image image = resultList.get(i);
if(image.getImageCounter() < startcounter) {
continue;
}
if (!sunday) {
if (checkIfSunday(image.getDay(), image.getMonth(),
image.getYear())) {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
} else {
System.out.println("Path = " + image.getPath());
op.outputMethod(method, rootFolderPath, image.getPath());
}
}
resultList = null; //free
}
}