栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使jfilechooser显示图像缩略图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使jfilechooser显示图像缩略图

实际上,令我感到惊讶的是,尽管在Windows中使用了本机外观,但文件选择器确实没有缩略图视图。我尝试了您的示例,但您的做法正确无误,但我发现带有很多大图像的文件夹的速度很慢。当然,开销是由于读取文件内容然后解释图像时的I
/ O,这是不可避免的。

更糟糕的是,我发现这

FileView.getIcon(File)
被称为 很多
-在显示文件列表之前,将鼠标悬停在图标上以及更改选择内容时。如果我们在加载图像后不缓存图像,那么我们将一直无意义地重新加载图像。

显而易见的解决方案是将所有图像加载推到另一个线程或线程池上,一旦获得缩小的结果,就将其放入临时缓存中,以便可以再次对其进行检索。

我打得四处

Image
ImageIcon
了很多,我发现了一个
ImageIcon
的图像可以在任何时候通过调用来改变
setImage(Image)
。对我们而言,这意味着在内
getIcon(File)
,我们可以立即返回一个空白或默认图标,但保留对其的引用,并将其传递给工作线程,该线程将在后台加载图像,并在完成后设置图标的图像(唯一的不足是我们必须致电
repaint()
才能看到更改)。

对于此示例,我使用

ExecutorService
缓存的线程池(这是获取所有图像的最快方法,但是使用大量I / O)来处理图像加载任务。我还使用a
WeakHashMap
作为缓存,以确保我们仅在需要它们时才保留它们。您可以使用另一种Map,但是必须管理持有的图标数量,以避免内存不足。

package guitest;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.util.Map;import java.util.WeakHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.regex.Pattern;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFileChooser;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.filechooser.FileView;public class ThumbnailFileChooser extends JFileChooser {        private static final int ICON_SIZE = 16;        private static final Image LOADING_IMAGE = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_INT_ARGB);        private final Pattern imageFilePattern = Pattern.compile(".+?\.(png|jpe?g|gif|tiff?)$", Pattern.CASE_INSENSITIVE);        private final Map imageCache = new WeakHashMap();    public static void main(String[] args) throws Exception {        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        JFileChooser chooser = new ThumbnailFileChooser();        chooser.showOpenDialog(null);        System.exit(1);    }    public ThumbnailFileChooser() {        super();    }    // --- Override the other constructors as needed ---    {        // This initializer block is always executed after any constructor call.        setFileView(new ThumbnailView());    }    private class ThumbnailView extends FileView {                private final ExecutorService executor = Executors.newCachedThreadPool();        public Icon getIcon(File file) { if (!imageFilePattern.matcher(file.getName()).matches()) {     return null; } // Our cache makes browsing back and forth lightning-fast! :D synchronized (imageCache) {     ImageIcon icon = imageCache.get(file);     if (icon == null) {         // Create a new icon with the default image         icon = new ImageIcon(LOADING_IMAGE);         // Add to the cache         imageCache.put(file, icon);         // Submit a new task to load the image and update the icon         executor.submit(new ThumbnailIconLoader(icon, file));     }     return icon; }        }    }    private class ThumbnailIconLoader implements Runnable {        private final ImageIcon icon;        private final File file;        public ThumbnailIconLoader(ImageIcon i, File f) { icon = i; file = f;        }        public void run() { System.out.println("Loading image: " + file); // Load and scale the image down, then replace the icon's old image with the new one. ImageIcon newIcon = new ImageIcon(file.getAbsolutePath()); Image img = newIcon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH); icon.setImage(img); // Repaint the dialog so we see the new icon. SwingUtilities.invokeLater(new Runnable() {public void run() {repaint();}});        }    }}

已知的问题:

1)缩放时,我们不保持图像的纵横比。这样做可能会导致图标的尺寸异常,从而破坏列表视图的对齐方式。解决方案可能是创建一个

BufferedImage
16x16
的新图像,并将缩放后的图像居中显示在其顶部。如果您愿意,可以实现它!

2)如果文件不是图像或已损坏,则完全不会显示任何图标。看起来程序仅在渲染图像时检测到此错误,而在加载或缩放图像时却未检测到,因此我们无法提前检测到此错误。但是,如果解决问题1,我们可能会检测到它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号