获取 Java 中文件图标的特定版本
Get a specific version of the icon of a file in Java
我在看这个 question 我在看第一个答案。
所以我尝试使用此代码:
public static Image getIcon(String fileName) throws Exception {
File file = new File(fileName);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
ImageIcon imageIcon = (ImageIcon) icon;
Image image = imageIcon.getImage();
return image;
}
return 一个 Image
(或抛出一个 Error
)但是 Image
的分辨率非常低。
我假设这是因为 16x16 Image
是 returned。
有什么方法可以说明 Image
我想 returned 吗?
我用过这个方法:
protected ImageIcon getImageIcon() {
File f = new File((iconPath!=null)?iconPath:"");
if (!f.isFile() || !f.canRead()) {
iconPath = Constants.getDefaultPreviewIconPath();
}
ImageIcon icon = new ImageIcon(iconPath, titolo);
return new ImageIcon(Utils.getScaledImage(
icon.getImage(),
Constants.getICON_WIDTH(),
Constants.getICON_HEIGTH()));
}
其中 getScaledImage 是:
public static Image getScaledImage(Image srcImg, int w, int h) {
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
Java 为您提供两种检索文件图标的可能性。
你已经知道第一个了:
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));
这会给你一个 16x16 像素的结果。
另一个使用 ShellFolder
Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));
将根据 getIcon
方法中的布尔标志 getLargeIcon
取回较大的 (32x32)。
很抱歉,但 java 默认库无法(目前)提供更多功能。兴趣存在,如您所见 in this JDK bugreport。
但是到目前为止什么都没做。
如果您真的想要更大的版本,您将需要使用 OS 取回它们,具体取决于本机调用或将它们手动存储为本地应用程序资源。
注意: 如果您在访问 ShellFolder
时遇到问题,您应该阅读 this question。
我在看这个 question 我在看第一个答案。
所以我尝试使用此代码:
public static Image getIcon(String fileName) throws Exception {
File file = new File(fileName);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
ImageIcon imageIcon = (ImageIcon) icon;
Image image = imageIcon.getImage();
return image;
}
return 一个 Image
(或抛出一个 Error
)但是 Image
的分辨率非常低。
我假设这是因为 16x16 Image
是 returned。
有什么方法可以说明 Image
我想 returned 吗?
我用过这个方法:
protected ImageIcon getImageIcon() {
File f = new File((iconPath!=null)?iconPath:"");
if (!f.isFile() || !f.canRead()) {
iconPath = Constants.getDefaultPreviewIconPath();
}
ImageIcon icon = new ImageIcon(iconPath, titolo);
return new ImageIcon(Utils.getScaledImage(
icon.getImage(),
Constants.getICON_WIDTH(),
Constants.getICON_HEIGTH()));
}
其中 getScaledImage 是:
public static Image getScaledImage(Image srcImg, int w, int h) {
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
Java 为您提供两种检索文件图标的可能性。
你已经知道第一个了:
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));
这会给你一个 16x16 像素的结果。
另一个使用 ShellFolder
Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));
将根据 getIcon
方法中的布尔标志 getLargeIcon
取回较大的 (32x32)。
很抱歉,但 java 默认库无法(目前)提供更多功能。兴趣存在,如您所见 in this JDK bugreport。
但是到目前为止什么都没做。
如果您真的想要更大的版本,您将需要使用 OS 取回它们,具体取决于本机调用或将它们手动存储为本地应用程序资源。
注意: 如果您在访问 ShellFolder
时遇到问题,您应该阅读 this question。