2015-02-17 53 views
1

我试图获取与特定文件关联的缩略图,然后调整其大小。我一直在Mac上进行测试,并且一直没有找到能够实现这一目标的解决方案。Java - 获取文件缩略图和调整大小

到目前为止的代码:

得到缩略图的
import com.apple.laf.AquaIcon; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 


public class TestSystemIcon extends JFrame 
{ 

JPanel panel; 
ImageIcon icon; 
public TestSystemIcon() 
{ 

    panel = new JPanel(); 
    JButton button = new JButton("Open..."); 
    final JLabel label = new JLabel(); 
    icon = null; 
    final JPanel en = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    label.setHorizontalAlignment(SwingConstants.CENTER); 
    button.addActionListener(new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      JFileChooser fileChooser = new JFileChooser(); 
      if(fileChooser.showOpenDialog(null) == JFileChooser.OPEN_DIALOG) 
      { 
       File file = fileChooser.getSelectedFile(); 
       icon = resizeIcon(getThumbnail1(file),200,200); 
//     icon = resizeIcon(getThumbnail2(file),200,200); 
       System.out.println(icon); 
       label.setIcon(icon); 
       en.add(label); 
       revalidate(); 
       repaint(); 

      } 
     } 
    }); 

    panel.add(button); 
    this.add(panel,BorderLayout.NORTH); 
    this.add(en,BorderLayout.CENTER); 

    this.setSize(400,400); 
    this.setLocationRelativeTo(null); 
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
} 


public ImageIcon getThumbnail1(File file) 
{ 
    JFileChooser f = new JFileChooser(); 
    Icon i = f.getIcon(file); 
    //Mac Conversion. 
    Image image = AquaIcon.getImageForIcon(i); 
    return new ImageIcon(image); 
} 

public ImageIcon getThumbnail2(File file) 
{ 
    return new ImageIcon(file.getPath()); 
} 

public ImageIcon resizeIcon(ImageIcon imageIcon,int width, int height) 
{ 
    return new ImageIcon(imageIcon.getImage().getScaledInstance(width,height,Image.SCALE_SMOOTH)); 
} 


public static void main(String[] args) 
{ 
    TestSystemIcon test = new TestSystemIcon(); 
    test.setVisible(true); 
} 
} 

版本1有以下行为:

  • 可以打开缩略图
  • 很小,比例不太合适。越来越缩略图

版本2有以下行为:

  • 不显示图像,尽管发现图像(System.out的证明了这一点)。 除pdf外,其中显示的是实际文件,而不是 缩略图
  • 当它工作时,即PDF,它可以很好地缩放。

我知道我可以使用sun.awt.shell.ShellFolder;,但我的目标是跨平台解决方案。

感谢所有帮助

回答

1

我看了一些代码,我在过去所做的那样,似乎当您使用的JLabel与和ImageIcon的这个工作正常,试试这个代码,调整大小大的图像为100x100,

ImageIcon icon = new ImageIcon("Penguins.jpg"); 
Image img = icon.getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH); 

// if the file is not an image, but a file on the system, 
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file); 
Image img = ((ImageIcon) icon).getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH); 

ImageIcon icon1 = new ImageIcon(img); 
JLabel image = new JLabel(icon1); 
+0

感谢您的回复,但其目的是获取任何文件的缩略图,而不是图像。即如果我打开一个docx文件,我会得到微软Word的缩略图图标 – 2015-02-18 08:21:18

+0

@ EII_18,我发现了一个重复的帖子,解决图标问题,但没有重新调整图像问题,看看http://stackoverflow.com/questions/1498506/java-howto-get-thumbnail-from-a-file我试过了它的工作逻辑,但是如果你需要重新设置图标,请使用我的答案 – faljbour 2015-02-18 21:09:53

+0

中的逻辑我更新了答案以显示信息从1498506提供。 – faljbour 2015-02-18 21:19:13