From 27b762ef00dad0ba0dc617849bf20b13b3cc5a40 Mon Sep 17 00:00:00 2001 From: Mateusz Kowalczyk Date: Thu, 23 Jan 2020 17:29:47 +0100 Subject: [PATCH] update database, caching thumbnails and other few fixes --- .../ffm/app/DirectoryScanner.java | 31 ++++++--- .../ffm/app/cache/CacheService.java | 67 ++++++------------- .../ffm/app/cache/CacheStructureChecker.java | 17 ++--- .../ffm/app/cache/ThumbnailCacheService.java | 49 ++++++++++++++ .../ffm/data/DatabaseService.java | 4 +- .../ffm/data/database/photo/Photo.java | 12 +++- .../ffm/image/ThumbnailService.java | 51 ++++++++++++++ 7 files changed, 162 insertions(+), 69 deletions(-) create mode 100644 src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java create mode 100644 src/main/java/dev/mateuszkowalczyk/ffm/image/ThumbnailService.java diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/DirectoryScanner.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/DirectoryScanner.java index 0f71088..aa461ff 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/app/DirectoryScanner.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/DirectoryScanner.java @@ -1,15 +1,16 @@ package dev.mateuszkowalczyk.ffm.app; -import dev.mateuszkowalczyk.ffm.app.cache.CacheService; import dev.mateuszkowalczyk.ffm.data.database.photo.Photo; import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO; +import dev.mateuszkowalczyk.ffm.image.ThumbnailService; import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; import dev.mateuszkowalczyk.ffm.utils.Property; import javafx.application.Platform; -import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.image.PixelWriter; +import javafx.scene.image.WritableImage; -import java.io.File; +import java.awt.image.BufferedImage; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -18,6 +19,7 @@ import java.util.ArrayList; public class DirectoryScanner implements Runnable { private WorkspaceService workspaceService = WorkspaceService.getInstance(); private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance(); + private ThumbnailService thumbnailService = new ThumbnailService(); private PhotoDAO photoDAO = new PhotoDAO(); @Override @@ -33,19 +35,26 @@ public class DirectoryScanner implements Runnable { .filter(Files::isRegularFile) .forEach(path -> { String filename = path.getFileName().toString(); - if(this.isImagePath(filename)) { - ImageView imageView = new ImageView(); - String newImagePath = CacheService.getInstance().createCachedThumbnail(path.toString()); - + if(this.isImagePath(filename) && !path.toString().contains(".cache")) { Photo photo = new Photo(); photo.setPath(path.toString()); - photo.setCachedPath(newImagePath); + photo.setFileName(filename); + + BufferedImage image = this.thumbnailService.createThumbnail(photo); this.photoDAO.save(photo); -// Image image1 = new Image("file:" + path.toString(), 200, 200, false, true); - Image image1 = new Image("file:" + newImagePath); + WritableImage wr = null; + if (image != null) { + wr = new WritableImage(image.getWidth(), image.getHeight()); + PixelWriter pw = wr.getPixelWriter(); + for (int x = 0; x < image.getWidth(); x++) { + for (int y = 0; y < image.getHeight(); y++) { + pw.setArgb(x, y, image.getRGB(x, y)); + } + } + } - imageView.setImage(image1); + ImageView imageView = new ImageView(wr); imageViews.add(imageView); Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView)); diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java index 4ea27bc..042f806 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java @@ -1,68 +1,43 @@ package dev.mateuszkowalczyk.ffm.app.cache; -import com.sun.javafx.sg.prism.NGRectangle; import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; import dev.mateuszkowalczyk.ffm.utils.Property; -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; - public class CacheService { - private static final CacheService instance = new CacheService(); - private String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); + private static CacheService instance; + private String path = ""; private CacheService () { this.check(); } - public static CacheService getInstance() { + public synchronized static CacheService getInstance() { + if(instance == null) { + instance = new CacheService(); + } + return instance; } - public void check() { - CacheStructureChecker cacheStructureChecker = new CacheStructureChecker(); - cacheStructureChecker.check(); + public String getPath() { + return path; } - public String createCachedThumbnail(String path) { - this.path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); - BufferedImage bufferedImage = null; - File image = null; - File newImageFile = null; - String pathToNewFile = null; + public void check() { + this.updatePath(); + if (this.path != null) { + CacheStructureChecker cacheStructureChecker = new CacheStructureChecker(); + cacheStructureChecker.check(this.path); + } + } - try { - image = new File(path); - bufferedImage = ImageIO.read(image); + private String updatePath() { + String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); - float divider; - if (bufferedImage.getWidth() > bufferedImage.getHeight()) { - divider = bufferedImage.getWidth() / 200; - } else { - divider = bufferedImage.getHeight() / 200; - } - - int width = (int) (bufferedImage.getWidth() / divider); - int height = (int) (bufferedImage.getHeight() / divider); - - BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType()); - Graphics2D graphics2D = newImage.createGraphics(); - graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); - graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), null); - graphics2D.dispose(); - - pathToNewFile = this.path + "/.cache/thumbnails/" + path.substring(this.path.length()); - newImageFile = new File(pathToNewFile); - - ImageIO.write(newImage, "jpg", newImageFile); - - } catch (IOException e) { - e.printStackTrace(); + if (path != null) { + this.path = path + "/.cache"; } - return pathToNewFile; + return path; } } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheStructureChecker.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheStructureChecker.java index 30f3412..d7653cd 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheStructureChecker.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheStructureChecker.java @@ -1,10 +1,6 @@ package dev.mateuszkowalczyk.ffm.app.cache; -import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; -import dev.mateuszkowalczyk.ffm.utils.Property; - import java.io.File; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -12,19 +8,24 @@ public class CacheStructureChecker { private String path; private List listToCheck = new ArrayList(); + public CacheStructureChecker() {} + public void check() { - this.path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); + this.check(CacheService.getInstance().getPath()); + } + + public void check(String path) { + this.path = path; if (this.path != null) { - this.path += "/.cache"; this.setupPathsToCheck(); - this.listToCheck.forEach(s -> this.checkDirectory(s)); + this.listToCheck.forEach(this::checkDirectory); } } private void setupPathsToCheck() { this.listToCheck.add(this.path); - this.listToCheck.add(this.path + "/thumbnails"); + this.listToCheck.add(this.path + ThumbnailCacheService.DIRECTORY_NAME); } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java new file mode 100644 index 0000000..01819e4 --- /dev/null +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java @@ -0,0 +1,49 @@ +package dev.mateuszkowalczyk.ffm.app.cache; + +import dev.mateuszkowalczyk.ffm.data.database.photo.Photo; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class ThumbnailCacheService implements Runnable { + protected static final String DIRECTORY_NAME = "/thumbnails"; + private BufferedImage image; + private CacheService cacheService = CacheService.getInstance(); + private String path; + + public ThumbnailCacheService() { + + } + + public ThumbnailCacheService(BufferedImage bufferedImage) { + this.image = bufferedImage; + } + + @Override + public void run() { + this.createCachedThumbnail(); + } + + public void createPath(Photo photo) { + this.path = this.cacheService.getPath() + DIRECTORY_NAME + "/" + photo.getFileName(); + photo.setCachedPath(this.path); + } + + + private void createCachedThumbnail() { + if (image != null && this.path != null) { + File imageFile = new File(this.path); + try { + ImageIO.write(this.image, this.fileExtension(), imageFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private String fileExtension() { + return this.path.substring(this.path.length() - 3); + } +} diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java b/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java index 7d8dc07..f2dcaa9 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java @@ -32,10 +32,10 @@ public class DatabaseService { public void connect() { if (this.connection == null) { - String path = ResourceLoader.getInstance().getPath() + "app.db"; + String path = ResourceLoader.getInstance().getPath("app.db"); try { - this.connection = DriverManager.getConnection(path); + this.connection = DriverManager.getConnection("jdbc:sqlite:" + path); } catch (SQLException e) { System.out.println(e.getMessage()); } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/data/database/photo/Photo.java b/src/main/java/dev/mateuszkowalczyk/ffm/data/database/photo/Photo.java index 0efb2d0..c18abf9 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/data/database/photo/Photo.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/data/database/photo/Photo.java @@ -10,13 +10,21 @@ public class Photo { @Column(type = Column.Type.INT) @PrimaryKey private long id; - @Column private String path; - + @Column + private String fileName; @Column private String cachedPath; + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + public long getId() { return id; } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/image/ThumbnailService.java b/src/main/java/dev/mateuszkowalczyk/ffm/image/ThumbnailService.java new file mode 100644 index 0000000..119f8bc --- /dev/null +++ b/src/main/java/dev/mateuszkowalczyk/ffm/image/ThumbnailService.java @@ -0,0 +1,51 @@ +package dev.mateuszkowalczyk.ffm.image; + +import dev.mateuszkowalczyk.ffm.app.cache.ThumbnailCacheService; +import dev.mateuszkowalczyk.ffm.data.database.photo.Photo; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class ThumbnailService { + + public BufferedImage createThumbnail(Photo photo) { + BufferedImage originalImage; + BufferedImage image = null; + + try { + File originalImageFile = new File(photo.getPath()); + originalImage = ImageIO.read(originalImageFile); + + float divider; + if (originalImage.getWidth() > originalImage.getHeight()) { + divider = originalImage.getWidth() / 200; + } else { + divider = originalImage.getHeight() / 200; + } + + int width = (int) (originalImage.getWidth() / divider); + int height = (int) (originalImage.getHeight() / divider); + + image = new BufferedImage(width, height, originalImage.getType()); + Graphics2D graphics2D = image.createGraphics(); + graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + 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(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return image; + } + +}