add caching thumbnails
This commit is contained in:
parent
aa17276aba
commit
9a5c104aa9
@ -1,5 +1,6 @@
|
||||
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.utils.PropertiesLoader;
|
||||
@ -8,6 +9,7 @@ import javafx.application.Platform;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
@ -33,19 +35,17 @@ public class DirectoryScanner implements Runnable {
|
||||
String filename = path.getFileName().toString();
|
||||
if(this.isImagePath(filename)) {
|
||||
ImageView imageView = new ImageView();
|
||||
String newImagePath = CacheService.getInstance().createCachedThumbnail(path.toString());
|
||||
|
||||
Photo photo = new Photo();
|
||||
photo.setPath(path.toString());
|
||||
|
||||
photo.setCachedPath(newImagePath);
|
||||
this.photoDAO.save(photo);
|
||||
|
||||
Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
|
||||
// Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
|
||||
Image image1 = new Image("file:" + newImagePath);
|
||||
|
||||
imageView.setImage(image1);
|
||||
imageView.setFitHeight(200);
|
||||
imageView.setFitWidth(200);
|
||||
imageView.setSmooth(true);
|
||||
imageView.setCache(true);
|
||||
imageViews.add(imageView);
|
||||
|
||||
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));
|
||||
|
@ -1,7 +1,18 @@
|
||||
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 CacheService () {
|
||||
this.check();
|
||||
@ -15,4 +26,43 @@ public class CacheService {
|
||||
CacheStructureChecker cacheStructureChecker = new CacheStructureChecker();
|
||||
cacheStructureChecker.check();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
try {
|
||||
image = new File(path);
|
||||
bufferedImage = ImageIO.read(image);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return pathToNewFile;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user