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 {
|
||||
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||
private FaceDetector faceDetector = FaceDetector.getInstance();
|
||||
private ThumbnailService thumbnailService = new ThumbnailService();
|
||||
private PhotoDAO photoDAO = PhotoDAO.getInstance();
|
||||
|
||||
@ -64,20 +65,13 @@ public class DirectoryScanner implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
// new Thread(
|
||||
// new FaceDetector(photo)
|
||||
// ).start();
|
||||
|
||||
new FaceDetector(photo).run();
|
||||
this.faceDetector.run(photo);
|
||||
|
||||
imageView = new ImageView(wr);
|
||||
imageViews.add(imageView);
|
||||
|
||||
} else {
|
||||
Image image = new Image("file:" + photo.getCachedPath());
|
||||
imageView = new ImageView(image);
|
||||
Platform.runLater(() -> WorkspaceService.getInstance().addImage(imageView));
|
||||
}
|
||||
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
@ -95,8 +89,4 @@ public class DirectoryScanner implements Runnable {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void clearImages() {
|
||||
this.workspaceService.getMainPageController().clearImages();
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,11 @@
|
||||
package dev.mateuszkowalczyk.ffm.app;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.app.cache.CacheService;
|
||||
import dev.mateuszkowalczyk.ffm.data.Chooser;
|
||||
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 javafx.application.Platform;
|
||||
import javafx.scene.image.ImageView;
|
||||
|
||||
import java.io.*;
|
||||
public class WorkspaceService {
|
||||
private static final WorkspaceService instance = new WorkspaceService();
|
||||
private DatabaseService databaseService;
|
||||
@ -36,7 +31,11 @@ public class WorkspaceService {
|
||||
|
||||
public void refreshWorkspace() {
|
||||
Platform.runLater(() -> {this.getMainPageController().clearImages();});
|
||||
var t = new Thread(new DirectoryScanner());
|
||||
var t = new Thread(new WorkspaceWrapper());
|
||||
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 java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ThumbnailCacheService implements Runnable {
|
||||
public class ThumbnailCacheService {
|
||||
protected static final String DIRECTORY_NAME = "/thumbnails";
|
||||
private BufferedImage image;
|
||||
private static ThumbnailCacheService instance;
|
||||
private CacheService cacheService = CacheService.getInstance();
|
||||
private String path;
|
||||
|
||||
public ThumbnailCacheService() {
|
||||
private ThumbnailCacheService() {
|
||||
|
||||
}
|
||||
|
||||
public ThumbnailCacheService(BufferedImage bufferedImage) {
|
||||
this.image = bufferedImage;
|
||||
}
|
||||
public static ThumbnailCacheService getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ThumbnailCacheService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.createCachedThumbnail();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void createPath(Photo photo) {
|
||||
@ -31,12 +31,26 @@ public class ThumbnailCacheService implements Runnable {
|
||||
photo.setCachedPath(this.path);
|
||||
}
|
||||
|
||||
public void readThumbnail(Photo photo) throws FileNotFoundException {
|
||||
var file = new File(photo.getCachedPath());
|
||||
|
||||
private void createCachedThumbnail() {
|
||||
if (image != null && this.path != null) {
|
||||
if(file.exists()) {
|
||||
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);
|
||||
try {
|
||||
ImageIO.write(this.image, this.fileExtension(), imageFile);
|
||||
ImageIO.write(bufferedImage, this.fileExtension(), imageFile);
|
||||
} catch (IOException e) {
|
||||
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.Table;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@ -20,6 +21,8 @@ public class Photo {
|
||||
@Column
|
||||
private String cachedPath;
|
||||
|
||||
private BufferedImage bufferedImage;
|
||||
|
||||
public Photo() {
|
||||
|
||||
}
|
||||
@ -31,6 +34,18 @@ public class Photo {
|
||||
this.cachedPath = resultSet.getString("cachedPath");
|
||||
}
|
||||
|
||||
public BufferedImage getBufferedImage() {
|
||||
if (bufferedImage == null) {
|
||||
|
||||
}
|
||||
|
||||
return bufferedImage;
|
||||
}
|
||||
|
||||
public void setBufferedImage(BufferedImage bufferedImage) {
|
||||
this.bufferedImage = bufferedImage;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
@ -6,10 +6,12 @@ import dev.mateuszkowalczyk.ffm.data.database.Dao;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PhotoDAO implements Dao<Photo> {
|
||||
private List<Photo> photoList = new ArrayList<>();
|
||||
private static PhotoDAO instance;
|
||||
private DatabaseService databaseService = DatabaseService.getInstance();
|
||||
|
||||
@ -31,12 +33,32 @@ public class PhotoDAO implements Dao<Photo> {
|
||||
|
||||
@Override
|
||||
public List<Photo> getAll() {
|
||||
return null;
|
||||
return this.getAll(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -91,6 +113,15 @@ public class PhotoDAO implements Dao<Photo> {
|
||||
|
||||
@Override
|
||||
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;
|
||||
|
||||
public class FaceDetector implements Runnable {
|
||||
public class FaceDetector {
|
||||
private FaceRecognition faceRecognition = new FaceRecognition();
|
||||
private FaceDAO faceDAO = FaceDAO.getInstance();
|
||||
private final Photo photo;
|
||||
private static FaceDetector instance;
|
||||
private Photo photo;
|
||||
|
||||
public FaceDetector(Photo photo) {
|
||||
this.photo = photo;
|
||||
private FaceDetector() {
|
||||
}
|
||||
|
||||
static {
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public static FaceDetector getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new FaceDetector();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void run(Photo photo) {
|
||||
this.photo = photo;
|
||||
this.detectFaces();
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ThumbnailService {
|
||||
ThumbnailCacheService thumbnailCacheService = ThumbnailCacheService.getInstance();
|
||||
|
||||
public BufferedImage createThumbnail(Photo photo) {
|
||||
BufferedImage originalImage;
|
||||
@ -35,12 +36,8 @@ public class ThumbnailService {
|
||||
graphics2D.drawImage(originalImage, 0, 0, width, height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
|
||||
graphics2D.dispose();
|
||||
|
||||
ThumbnailCacheService thumbnailCacheService = new ThumbnailCacheService(image);
|
||||
thumbnailCacheService.createPath(photo);
|
||||
|
||||
var t = new Thread(thumbnailCacheService);
|
||||
t.start();
|
||||
|
||||
this.thumbnailCacheService.createPath(photo);
|
||||
this.thumbnailCacheService.createCachedThumbnail(image);;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user