我试图借助OS默认图标从特定文件中提取图标,我已经搜索并找到了两种方法来实现它:
public Image getImage(File file) throws FileNotFoundException {
sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
return new ImageIcon(sf.getIcon(true)).getImage();
}
和
FileSystemView.getFileSystemView().getSystemIcon(file);
两者都可以,但是获得了切片图标
我认为容器(Swing中的JLabel和FX中的ImageView)有问题,因此我尝试将其导出到桌面,但是得到了相同的切片形状:
public static void writePNG(File f, BufferedImage img) throws Exception {
ImageWriter writer = null;
f.createNewFile();
try(FileImageOutputStream output = new FileImageOutputStream(f.getCanonicalFile())) {
writer = ImageIO.getImageWritersByFormatName("png").next();
writer.setOutput(output);
IIOImage iioImage = new IIOImage(img, null, null);
writer.write(null, iioImage, null);
} finally {
if (writer != null)
writer.dispose();
}
}
public static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage)
return (BufferedImage) img;
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
是否有其他解决方案? 预先感谢。