原文:http://zetcode.com/articles/javaico/

在本文中,我们展示了如何在 Java 中读取和写入 ICO 图像。

ICO 是在 Microsoft Windows 上的图标中使用的图像文件格式。 ICO 文件包含一个或多个具有多种大小和颜色深度的小图像,因此可以适当缩放它们。 ICO 文件也经常用于网络上的收藏夹图标。

要使用 Java 读写 ICO 文件,我们使用 image4j 图片库。 可以从 image4j.sourceforge.net 下载用于 image4j 的 JAR。

将 PNG 转换为 ICO

在以下示例中,我们将 PNG 图像转换为 ICO 图像。

ConvertToIcoEx.java

  1. package com.zetcode;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. import net.sf.image4j.codec.ico.ICOEncoder;
  7. public class ConvertToIcoEx {
  8. public static void main(String[] args) throws IOException {
  9. BufferedImage bi = ImageIO.read(new File("laptop.png"));
  10. ICOEncoder.write(bi, new File("laptop.ico"));
  11. }
  12. }

我们使用ImageIO.read()方法将 PNG 文件读入BufferedImageBufferedImage是存储在内存中的像素矩形。 它是 Swing 中最重要的图像类型之一。

  1. ICOEncoder.write(bi, new File("laptop.ico"));

ICOEncoder.write()将 PNG 转换为 ICO 文件。

将 ICO 转换为 PNG

在第二个示例中,我们将 ICO 图像转换为 PNG 图像。

ConvertIcoToPngEx.java

  1. package com.zetcode;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.List;
  6. import javax.imageio.ImageIO;
  7. import net.sf.image4j.codec.ico.ICODecoder;
  8. public class ConvertIcoToPngEx {
  9. public static void main(String[] args) throws IOException {
  10. List<BufferedImage> images = ICODecoder.read(new File("favicon.ico"));
  11. ImageIO.write(images.get(0), "png", new File("favicon.png"));
  12. }
  13. }

我们使用ICODecoder.read()方法将 ICO 文件读入BufferedImage。 通过ImageIO.write()方法将BufferedImage写入 PNG 文件。

下载 ICO 文件

在下一个示例中,我们从网站下载 ICO 文件,将其转换为ImageIcon,并在JLabel组件中显示。

DownloadIcoEx.java

  1. package com.zetcode;
  2. import java.awt.Container;
  3. import java.awt.EventQueue;
  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.util.List;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.GroupLayout;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JComponent;
  15. import javax.swing.JFrame;
  16. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  17. import javax.swing.JLabel;
  18. import net.sf.image4j.codec.ico.ICODecoder;
  19. /**
  20. * The example downloads a favicon and displays it in a JLabel.
  21. *
  22. * @author Jan Bodnar
  23. * @website zetcode.com
  24. */
  25. public class DownloadIcoEx extends JFrame {
  26. public DownloadIcoEx() {
  27. initUI();
  28. }
  29. private void initUI() {
  30. List<BufferedImage> images = readImage();
  31. ImageIcon icon = new ImageIcon(images.get(0));
  32. JLabel lbl = new JLabel(icon);
  33. createLayout(lbl);
  34. setTitle("Ico image");
  35. setLocationRelativeTo(null);
  36. setDefaultCloseOperation(EXIT_ON_CLOSE);
  37. }
  38. private List<BufferedImage> readImage() {
  39. List<BufferedImage> images = null;
  40. try {
  41. String path = "http://stackoverflow.com/favicon.ico";
  42. InputStream istr = new URL(path).openStream();
  43. images = ICODecoder.read(istr);
  44. } catch (MalformedURLException ex) {
  45. Logger.getLogger(DownloadIcoEx.class.getName()).log(Level.SEVERE, null, ex);
  46. } catch (IOException ex) {
  47. Logger.getLogger(DownloadIcoEx.class.getName()).log(Level.SEVERE, null, ex);
  48. }
  49. return images;
  50. }
  51. private void createLayout(JComponent... arg) {
  52. Container pane = getContentPane();
  53. GroupLayout gl = new GroupLayout(pane);
  54. pane.setLayout(gl);
  55. gl.setAutoCreateContainerGaps(true);
  56. gl.setHorizontalGroup(gl.createSequentialGroup()
  57. .addComponent(arg[0])
  58. .addGap(150)
  59. );
  60. gl.setVerticalGroup(gl.createParallelGroup()
  61. .addComponent(arg[0])
  62. .addGap(100)
  63. );
  64. pack();
  65. }
  66. public static void main(String[] args) {
  67. EventQueue.invokeLater(() -> {
  68. DownloadIcoEx ex = new DownloadIcoEx();
  69. ex.setVisible(true);
  70. });
  71. }
  72. }

该示例从 StackOverflow 网站下载了一个图标。

  1. String path = "http://stackoverflow.com/favicon.ico";
  2. InputStream istr = new URL(path).openStream();

我们从 URL 创建一个InputStream

  1. images = ICODecoder.read(istr);

ICODecoder.read()InputStream读取到BufferedImage

  1. ImageIcon icon = new ImageIcon(images.get(0));

BufferedImage创建一个ImageIcon

  1. JLabel lbl = new JLabel(icon);

ImageIcon放入JLabel

用 Java 读写 ICO 图像 - 图1

图:Favicon

在本文中,我们已使用 image4j 库读取和写入 ICO 图像。

您可能也对以下相关教程感兴趣: Java 教程Java Swing 教程Android 教程