java读取gif

java在读取gif图片的时候,方式多种,最近项目中用到了applet,applet需要把图片都放在src目录下打包。

最先在尝试使用ImageIO.read(this.getClass().getResourceAsStream("t.gif"));  然后new ImageIcon(Image image);

gif图片只加载了第一侦,原因是上面的方式读取出来的是BuffferedImage形式,在赋予JLabel等组件时无法显示动画。

搜寻一阵,发现使用 this.getClass().getResource("t.gif")即可,这次返回的时URL。即可new ImageIcon(URL url)。



     

   JFrame frame = new JFrame();
        frame.setSize(800, 600);

        JPanel panel = new JPanel();

        JLabel jLabel = new JLabel();
        jLabel.setIcon(new ImageIcon(this.getClass().getResource("t.gif")));
        panel.add(jLabel);

        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

附上简短代码,希望对需要的人有帮助。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:   ImageIcon[] images; //用于动画的图标数组   Timer animationTimer;   int currentImage = 0; //当前图像编号   int delay = 500; //图像切换延迟   int width; //图像宽度   int height; //图像高度   public AnimatorIcon() //构造函数   {    setBackground(Color.white);    images = new ImageIcon[2]; //初始化数组    for (int i=0;i   images[i]=new ImageIcon(getClass().getResource("image" i ".gif")); //实例化图标    width = images[0].getIconWidth(); //初始化宽度值    height = images[0].getIconHeight(); //初始化高度值   }   public void paintComponent(Graphics g) { //重载组件绘制方法    super.paintComponent(g); //调用父类函数    images[currentImage].paintIcon(this,g,70,0); //绘制图标    currentImage=(currentImage 1)%2; //更改当前图像编号   }   public void actionPerformed(ActionEvent actionEvent) {    repaint();   }   public void startAnimation() { //开始动画    if (animationTimer==null) {    currentImage=0;    animationTimer=new Timer(delay, this); //实例化Timer对象    animationTimer.start(); //开始运行    } else if (!animationTimer.isRunning()) //如果没有运行    animationTimer.restart(); //重新运行   }   public void stopAnimation() {    animationTimer.stop(); //停止动画   }   public static void main(String args[]) {    AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标    JFrame frame = new JFrame("动画图标"); //实例化窗口对象    frame.getContentPane().add(animation); //增加组件到窗口上    frame.setSize(200, 100); //设置窗口尺寸    frame.setVisible(true); //设置窗口可视    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    animation.startAnimation(); //开始动画
要压缩Java中的GIF文件大小,可以使用GifSequenceWriter类和ImageIO类进行操作。 首先,需要导入JavaImageIO类和相关的输入输出流库。然后,将GIF文件读取Image对象序列。使用ImageReader类将GIF文件解码为图像帧序列。 接下来,创建一个新的GIF文件,并使用GifSequenceWriter类的实例将图像帧序列写入该文件。在创建GifSequenceWriter对象时,可以指定输出文件的压缩参数,例如颜色深度、帧率和压缩模式。 最后,关闭所有的输入输出流,并保存压缩后的GIF文件。可以使用try-with-resources语句块来确保流的正确关闭。 以下是一个示例代码: ```java import javax.imageio.*; import javax.imageio.metadata.*; import javax.imageio.stream.*; import java.awt.image.*; import java.io.*; import java.util.* public class GifCompressor { public static void main(String[] args) { try { // 读取GIF文件为图像序列 ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); File inputFile = new File("input.gif"); ImageInputStream inputStream = ImageIO.createImageInputStream(inputFile); reader.setInput(inputStream); int numFrames = reader.getNumImages(true); List<BufferedImage> frames = new ArrayList<>(); for (int i = 0; i < numFrames; i++) { BufferedImage frame = reader.read(i); frames.add(frame); } // 创建新的GIF文件并将图像序列压缩写入 File outputFile = new File("output.gif"); ImageOutputStream outputStream = new FileImageOutputStream(outputFile); GifSequenceWriter writer = new GifSequenceWriter(outputStream, frames.get(0).getType(), 0, false); for (BufferedImage frame : frames) { writer.writeToSequence(frame); } // 关闭流 writer.close(); outputStream.close(); reader.dispose(); inputStream.close(); System.out.println("压缩GIF文件成功!"); } catch (IOException e) { System.out.println("压缩GIF文件失败:" + e.getMessage()); } } } ``` 这样,通过ImageIO和GifSequenceWriter,就可以在Java中压缩GIF文件的大小。注意,可以根据需要调整压缩参数以获得最佳结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值