little cleaning, optimization and change way to load cached images
This commit is contained in:
parent
6045428f10
commit
b9320fc1f9
@ -21,6 +21,7 @@ import java.util.ArrayList;
|
|||||||
public class DirectoryScanner implements Runnable {
|
public class DirectoryScanner implements Runnable {
|
||||||
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
||||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||||
|
private FaceDetector faceDetector = FaceDetector.getInstance();
|
||||||
private ThumbnailService thumbnailService = new ThumbnailService();
|
private ThumbnailService thumbnailService = new ThumbnailService();
|
||||||
private PhotoDAO photoDAO = PhotoDAO.getInstance();
|
private PhotoDAO photoDAO = PhotoDAO.getInstance();
|
||||||
|
|
||||||
@ -64,20 +65,13 @@ public class DirectoryScanner implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// new Thread(
|
this.faceDetector.run(photo);
|
||||||
// new FaceDetector(photo)
|
|
||||||
// ).start();
|
|
||||||
|
|
||||||
new FaceDetector(photo).run();
|
|
||||||
|
|
||||||
imageView = new ImageView(wr);
|
imageView = new ImageView(wr);
|
||||||
imageViews.add(imageView);
|
imageViews.add(imageView);
|
||||||
|
|
||||||
} else {
|
Platform.runLater(() -> WorkspaceService.getInstance().addImage(imageView));
|
||||||
Image image = new Image("file:" + photo.getCachedPath());
|
|
||||||
imageView = new ImageView(image);
|
|
||||||
}
|
}
|
||||||
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -95,8 +89,4 @@ public class DirectoryScanner implements Runnable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearImages() {
|
|
||||||
this.workspaceService.getMainPageController().clearImages();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
package dev.mateuszkowalczyk.ffm.app;
|
package dev.mateuszkowalczyk.ffm.app;
|
||||||
|
|
||||||
import dev.mateuszkowalczyk.ffm.app.cache.CacheService;
|
import dev.mateuszkowalczyk.ffm.app.cache.CacheService;
|
||||||
import dev.mateuszkowalczyk.ffm.data.Chooser;
|
|
||||||
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
|
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
|
||||||
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
|
|
||||||
import dev.mateuszkowalczyk.ffm.utils.Property;
|
|
||||||
import dev.mateuszkowalczyk.ffm.view.SceneEnum;
|
|
||||||
import dev.mateuszkowalczyk.ffm.view.StageController;
|
|
||||||
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
|
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.scene.image.ImageView;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
public class WorkspaceService {
|
public class WorkspaceService {
|
||||||
private static final WorkspaceService instance = new WorkspaceService();
|
private static final WorkspaceService instance = new WorkspaceService();
|
||||||
private DatabaseService databaseService;
|
private DatabaseService databaseService;
|
||||||
@ -36,7 +31,11 @@ public class WorkspaceService {
|
|||||||
|
|
||||||
public void refreshWorkspace() {
|
public void refreshWorkspace() {
|
||||||
Platform.runLater(() -> {this.getMainPageController().clearImages();});
|
Platform.runLater(() -> {this.getMainPageController().clearImages();});
|
||||||
var t = new Thread(new DirectoryScanner());
|
var t = new Thread(new WorkspaceWrapper());
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addImage(ImageView element) {
|
||||||
|
Platform.runLater(() -> this.mainPageController.addImage(element));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package dev.mateuszkowalczyk.ffm.app;
|
||||||
|
|
||||||
|
import dev.mateuszkowalczyk.ffm.app.cache.ThumbnailCacheService;
|
||||||
|
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
||||||
|
import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO;
|
||||||
|
import javafx.scene.image.Image;
|
||||||
|
import javafx.scene.image.ImageView;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class WorkspaceWrapper implements Runnable {
|
||||||
|
private PhotoDAO photoDAO = PhotoDAO.getInstance();
|
||||||
|
private ThumbnailCacheService thumbnailCacheService = ThumbnailCacheService.getInstance();
|
||||||
|
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
this.setFromDatabase();
|
||||||
|
new DirectoryScanner().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setFromDatabase() {
|
||||||
|
List<Photo> photoList = this.photoDAO.getAll();
|
||||||
|
|
||||||
|
photoList.forEach(photo -> {
|
||||||
|
var element = this.createImageViewElement(photo);
|
||||||
|
|
||||||
|
workspaceService.addImage(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageView createImageViewElement(Photo photo) {
|
||||||
|
Image image = new Image("file:" + photo.getCachedPath());
|
||||||
|
|
||||||
|
return new ImageView(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -5,25 +5,25 @@ import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ThumbnailCacheService implements Runnable {
|
public class ThumbnailCacheService {
|
||||||
protected static final String DIRECTORY_NAME = "/thumbnails";
|
protected static final String DIRECTORY_NAME = "/thumbnails";
|
||||||
private BufferedImage image;
|
private static ThumbnailCacheService instance;
|
||||||
private CacheService cacheService = CacheService.getInstance();
|
private CacheService cacheService = CacheService.getInstance();
|
||||||
private String path;
|
private String path;
|
||||||
|
|
||||||
public ThumbnailCacheService() {
|
private ThumbnailCacheService() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThumbnailCacheService(BufferedImage bufferedImage) {
|
public static ThumbnailCacheService getInstance() {
|
||||||
this.image = bufferedImage;
|
if (instance == null) {
|
||||||
}
|
instance = new ThumbnailCacheService();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
return instance;
|
||||||
public void run() {
|
|
||||||
this.createCachedThumbnail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createPath(Photo photo) {
|
public void createPath(Photo photo) {
|
||||||
@ -31,12 +31,26 @@ public class ThumbnailCacheService implements Runnable {
|
|||||||
photo.setCachedPath(this.path);
|
photo.setCachedPath(this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void readThumbnail(Photo photo) throws FileNotFoundException {
|
||||||
|
var file = new File(photo.getCachedPath());
|
||||||
|
|
||||||
private void createCachedThumbnail() {
|
if(file.exists()) {
|
||||||
if (image != null && this.path != null) {
|
try {
|
||||||
|
BufferedImage image = ImageIO.read(file);
|
||||||
|
photo.setBufferedImage(image);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new FileNotFoundException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createCachedThumbnail(BufferedImage bufferedImage) {
|
||||||
|
if (bufferedImage != null && this.path != null) {
|
||||||
File imageFile = new File(this.path);
|
File imageFile = new File(this.path);
|
||||||
try {
|
try {
|
||||||
ImageIO.write(this.image, this.fileExtension(), imageFile);
|
ImageIO.write(bufferedImage, this.fileExtension(), imageFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import dev.mateuszkowalczyk.ffm.data.database.annotation.Column;
|
|||||||
import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey;
|
import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey;
|
||||||
import dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
|
import dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
@ -20,6 +21,8 @@ public class Photo {
|
|||||||
@Column
|
@Column
|
||||||
private String cachedPath;
|
private String cachedPath;
|
||||||
|
|
||||||
|
private BufferedImage bufferedImage;
|
||||||
|
|
||||||
public Photo() {
|
public Photo() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -31,6 +34,18 @@ public class Photo {
|
|||||||
this.cachedPath = resultSet.getString("cachedPath");
|
this.cachedPath = resultSet.getString("cachedPath");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BufferedImage getBufferedImage() {
|
||||||
|
if (bufferedImage == null) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return bufferedImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBufferedImage(BufferedImage bufferedImage) {
|
||||||
|
this.bufferedImage = bufferedImage;
|
||||||
|
}
|
||||||
|
|
||||||
public String getFileName() {
|
public String getFileName() {
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,12 @@ import dev.mateuszkowalczyk.ffm.data.database.Dao;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class PhotoDAO implements Dao<Photo> {
|
public class PhotoDAO implements Dao<Photo> {
|
||||||
|
private List<Photo> photoList = new ArrayList<>();
|
||||||
private static PhotoDAO instance;
|
private static PhotoDAO instance;
|
||||||
private DatabaseService databaseService = DatabaseService.getInstance();
|
private DatabaseService databaseService = DatabaseService.getInstance();
|
||||||
|
|
||||||
@ -31,12 +33,32 @@ public class PhotoDAO implements Dao<Photo> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Photo> getAll() {
|
public List<Photo> getAll() {
|
||||||
return null;
|
return this.getAll(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Photo> getAll(boolean refresh) {
|
public List<Photo> getAll(boolean refresh) {
|
||||||
return null;
|
if (this.photoList.size() == 0 || refresh) {
|
||||||
|
this.photoList.clear();
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM photos";
|
||||||
|
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
var photo = new Photo(resultSet);
|
||||||
|
|
||||||
|
this.photoList.add(photo);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.photoList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -91,6 +113,15 @@ public class PhotoDAO implements Dao<Photo> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(Photo photo) {
|
public void delete(Photo photo) {
|
||||||
|
String sql = "DELETE FROM photos WHERE id = ?";
|
||||||
|
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
|
||||||
|
preparedStatement.setLong(1, photo.getId());
|
||||||
|
|
||||||
|
preparedStatement.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,21 +21,29 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import static org.opencv.imgcodecs.Imgcodecs.imread;
|
import static org.opencv.imgcodecs.Imgcodecs.imread;
|
||||||
|
|
||||||
public class FaceDetector implements Runnable {
|
public class FaceDetector {
|
||||||
private FaceRecognition faceRecognition = new FaceRecognition();
|
private FaceRecognition faceRecognition = new FaceRecognition();
|
||||||
private FaceDAO faceDAO = FaceDAO.getInstance();
|
private FaceDAO faceDAO = FaceDAO.getInstance();
|
||||||
private final Photo photo;
|
private static FaceDetector instance;
|
||||||
|
private Photo photo;
|
||||||
|
|
||||||
public FaceDetector(Photo photo) {
|
private FaceDetector() {
|
||||||
this.photo = photo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static FaceDetector getInstance() {
|
||||||
public void run() {
|
if (instance == null) {
|
||||||
|
instance = new FaceDetector();
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(Photo photo) {
|
||||||
|
this.photo = photo;
|
||||||
this.detectFaces();
|
this.detectFaces();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ThumbnailService {
|
public class ThumbnailService {
|
||||||
|
ThumbnailCacheService thumbnailCacheService = ThumbnailCacheService.getInstance();
|
||||||
|
|
||||||
public BufferedImage createThumbnail(Photo photo) {
|
public BufferedImage createThumbnail(Photo photo) {
|
||||||
BufferedImage originalImage;
|
BufferedImage originalImage;
|
||||||
@ -35,12 +36,8 @@ public class ThumbnailService {
|
|||||||
graphics2D.drawImage(originalImage, 0, 0, width, height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
|
graphics2D.drawImage(originalImage, 0, 0, width, height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
|
||||||
graphics2D.dispose();
|
graphics2D.dispose();
|
||||||
|
|
||||||
ThumbnailCacheService thumbnailCacheService = new ThumbnailCacheService(image);
|
this.thumbnailCacheService.createPath(photo);
|
||||||
thumbnailCacheService.createPath(photo);
|
this.thumbnailCacheService.createCachedThumbnail(image);;
|
||||||
|
|
||||||
var t = new Thread(thumbnailCacheService);
|
|
||||||
t.start();
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user