From 5867b4b19037b2cd51819a26a7976ae3393dde42 Mon Sep 17 00:00:00 2001 From: Michael Sims Date: Fri, 16 Apr 2021 11:49:43 -0700 Subject: [PATCH] Cross OS Additions Added some methods that will work on any OS, offering both a JavaFX Image and a BufferedImage. --- src/me/marnic/jiconextract2/JIconExtract.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/me/marnic/jiconextract2/JIconExtract.java b/src/me/marnic/jiconextract2/JIconExtract.java index b9c8858..acf2695 100644 --- a/src/me/marnic/jiconextract2/JIconExtract.java +++ b/src/me/marnic/jiconextract2/JIconExtract.java @@ -6,6 +6,8 @@ import com.sun.jna.platform.win32.COM.COMUtils; import com.sun.jna.platform.win32.*; import com.sun.jna.ptr.PointerByReference; +import javafx.embed.swing.SwingFXUtils; +import javafx.scene.image.Image; import java.awt.image.BufferedImage; import java.io.File; @@ -131,4 +133,57 @@ public static WinDef.HBITMAP getHBITMAPForFile(int width,int height,String fileN return null; } + + public static BufferedImage getIconForFile(File file) { + return getBufferedImage(file); + } + + public static BufferedImage getIconForFile(Path filePath) { + return getBufferedImage(filePath.toFile()); + } + + public static BufferedImage getIconForFile(String filePath) { + File file = new File(filePath); + return getBufferedImage(file); + } + + public static Image getIconImageForFile(String filePath) { + File file = new File(filePath); + return getImage(file); + } + + public static Image getIconImageForFile(File file) { + return getImage(file); + } + + public static Image getIconImageForFile(Path filePath) { + return getImage(filePath.toFile()); + } + + private static Image getImage(File file) { + final javax.swing.JFileChooser fc = new javax.swing.JFileChooser(); + javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file); + int width = icon.getIconWidth(); + int height = icon.getIconHeight(); + BufferedImage bufferedImage = new BufferedImage( + width, + height, + BufferedImage.TYPE_INT_ARGB + ); + icon.paintIcon(null, bufferedImage.getGraphics(), 0, 0); + return SwingFXUtils.toFXImage(bufferedImage, null); + } + + private static BufferedImage getBufferedImage(File file) { + final javax.swing.JFileChooser fc = new javax.swing.JFileChooser(); + javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file); + int width = icon.getIconWidth(); + int height = icon.getIconHeight(); + return new BufferedImage( + width, + height, + BufferedImage.TYPE_INT_ARGB + ); + } + }