java如何读取gif文件 用ImageIO.read()可以读入JPG格式的图片。好象不能读入GIF。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Gif4J http://www.gif4j.com/index.htm 你的JDK是哪个版本?使用 ImageIO.getReaderFormatNames(),ImageIO.getWriterFormatNames() 看看 可以读取ImageIO.read(new File("/images/001.gif")); BufferedImage image = ImageIO.read(new URL(url)); ImageIO.read(new File(filepath+filename)); 读gif文件那是必须的记得以前自己好玩实现过一个用流直接读取。 通过ImageIO.read读出来返回是一个BufferedImage对象。而不是一个动画的GIF import java.awt.*;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.BorderLayout;import javax.swing.JLabel;import javax.swing.ImageIcon;public class MainFrame extends JFrame { JPanel contentPane; JLabel imageLabel = new JLabel(); JLabel headerLabel = new JLabel(); public MainFrame() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); contentPane = (JPanel) getContentPane(); contentPane.setLayout(new BorderLayout()); setSize(new Dimension(400, 300)); setTitle("Your Job Crashed!"); // add the header label headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16)); headerLabel.setText(" Your job crashed during the save process!"); contentPane.add(headerLabel, java.awt.BorderLayout.NORTH); // add the image label ImageIcon ii = new ImageIcon(this.getClass().getResource( "snoopy_dancing.gif")); imageLabel.setIcon(ii); contentPane.add(imageLabel, java.awt.BorderLayout.CENTER); // show it this.setLocationRelativeTo(null); this.setVisible(true); } catch (Exception exception) { exception.printStackTrace(); } } public static void main(String[] args) { new MainFrame(); }} 楼主可以看一下《Java 核心技术》下卷“读取和写入带有多个图像的文件”(第七版为 7.10.2 节,pp.446~454),里面有讲到如何读到 GIF 文件。 InputStream imageIn = context.getResourceAsStream(imagePath);// 文件流BufferedInputStream bis = new BufferedInputStream(imageIn);// 输入缓冲流BufferedOutputStream bos = new BufferedOutputStream(output);// 输出缓冲流 怎样才能将jar文件转换成exe文件 求高手!!!!!!!!!!!!!! J2SE遇到瓶颈了,求大侠帮助 jdk7.0新特性。。。。。 方法调用 java问题 为什么会抛出java.io.FileNotFoundException哪,路径是对的啊,各位高人帮帮忙啊 有项目经验的大哥们说说项目中的命名问题 j2sdk的配置问题 小弟发现一个很基础,但是不好玩的问题,关于switch控制结构的。 哪里去下载Together for eclipse 6.2? 各位,,,,还是没有解决啊 今天去一家公司的面试题 第一个JAVA程序求高手解答
使用 ImageIO.getReaderFormatNames(),ImageIO.getWriterFormatNames()
看看
ImageIO.read(new File("/images/001.gif"));
记得以前自己好玩实现过一个用流直接读取。
而不是一个动画的GIF
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.ImageIcon;public class MainFrame extends JFrame {
JPanel contentPane;
JLabel imageLabel = new JLabel();
JLabel headerLabel = new JLabel(); public MainFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
setSize(new Dimension(400, 300));
setTitle("Your Job Crashed!");
// add the header label
headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
headerLabel.setText(" Your job crashed during the save process!");
contentPane.add(headerLabel, java.awt.BorderLayout.NORTH);
// add the image label
ImageIcon ii = new ImageIcon(this.getClass().getResource(
"snoopy_dancing.gif"));
imageLabel.setIcon(ii);
contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
// show it
this.setLocationRelativeTo(null);
this.setVisible(true);
} catch (Exception exception) {
exception.printStackTrace();
}
} public static void main(String[] args) {
new MainFrame();
}}
BufferedInputStream bis = new BufferedInputStream(imageIn);// 输入缓冲流
BufferedOutputStream bos = new BufferedOutputStream(output);// 输出缓冲流