如何获取文件的所有不同大小的系统图标

时间:2012-02-06 18:26:14

标签: java swing icons

我在java中做了一段代码,我需要获得不同大小的文件系统图标。 我知道要获取文件系统图标,我应该使用它:

File file = new File("Whatever.txt");
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);

但此代码返回最小的图标大小。 我为了获得其他尺码而采取了什么措施?

4 个答案:

答案 0 :(得分:1)

对于32x32图标,您可以使用ShellFolder。

sun.awt.shell.ShellFolder.getShellFolder( file ).getIcon( true ) 

或使用JNI Api。两个"解决方案"已经讨论过here

答案 1 :(得分:0)

我遇到了需要高分辨率图标的问题。经过一个小时的挖掘,我认为答案很清楚,虽然非常不幸。

  1. 目前没有本地库支持。
  2. Java版本12预计会提供原生支持。
  3. 即使经过大量搜索,我也找不到任何可靠的第三方库。
  4. 此问题的故障单已在JDK错误系统上提交here,但它们的优先级列表似乎相当低。

    但是,似乎确实有解决方法:

    我假设您使用的是Windows。在Linux上查找图标文件非常简单,因为图标单独存储并在桌面条目中链接。

    1. 使用第三方命令行exe工具(如this onethis one)将图标提取到临时目录。您可以使用Runtime.exec()
    2. 使用像the ones suggested here这样的.ico阅读器库,将其中包含的所有图像文件读入BufferedImage中的List
    3. 遍历列表以查找分辨率最高的图像。
    4. 删除临时文件。
    5. <强>更新

      刚刚实施了我的解决方法,它运作良好。这是代码段。我在实施中使用了iconsext.exeimage4j

          // Extract icons and wait for completion.
          File iconDir = new File("tempIcons");
          String[] iconsextCmd = { "iconsext.exe", "/save", exeFile.getPath(), iconDir.getPath(), "-icons" };
          Process iconsextProc = Runtime.getRuntime().exec(iconsextCmd);
          iconsextProc.waitFor();
      
          // Get all icons, sort by ascending name, and pick first one.
          File[] icons = iconDir.listFiles();
          Arrays.sort(icons, (f1, f2) -> f1.getName().compareTo(f2.getName()));
          File defaultIcon = icons[0];
      
          // Read images from icon.
          List<ICOImage> iconImgs = ICODecoder.readExt(defaultIcon);
      
          // Sort images by descending size and color depth, and pick first one.
          iconImgs.sort((i1, i2) -> (i2.getWidth() * i2.getHeight() * 33 + i2.getColourDepth())
                  - (i1.getWidth() * i1.getHeight() * 33 + i1.getColourDepth()));
          BufferedImage awtImg = iconImgs.get(0).getImage();
      
          // Delete temporary icons.
          for (File i : icons)
          {
              i.delete();
          }
          iconDir.delete();
      

      抛出一些JavaFX代码:

      Nice and big icons

      漂亮的大图标!

      我知道,解决方法是混乱和复杂的。它确实否定了Java的跨平台优势,但说实话,这是我能想到的最好的。希望它有所帮助。

答案 2 :(得分:0)

我制作了一个可以提取所有图标尺寸的库。在这里JIconExtract 只需输入:

struct setFragment_functor
{
    const int n;

    setFragment_functor(int _n) : n(_n) {}

    __host__ __device__
    void operator() (Fragment *frag) {
        frag->index[0] = n;
        frag->index[1] = n+1;
        frag->index[2] = n+2;       
    }
};

void setFragment( vector< Atom * > &vStruct, vector< Fragment * > &vFragment ) {
    int n = vStruct.size();
    thrust::device_vector<Fragment *> d_vFragment(n-2);

    thrust::transform( d_vFragment.begin(), d_vFragment.end(), setFragment_functor( thrust::counting_iterator<int>(0) ) );

    thrust::copy(d_vFragment.begin(), d_vFragment.end(), vFragment.begin());        
}

答案 3 :(得分:0)

如果您正在Mac上寻找操作方法,则可以使用第三方库ERROR_CODE_UNSUPPORTED_OPERATION

LibMan