swing生成图片之保存动态gif图片



// 保存动画按钮监听事件
        saveFlashBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                saveFlash();

            }
        });


/**
     * 1,获取列表的Image对象 2,动画显示
     */

    public void saveFlash() {
        if (filenametable.getRowCount() == 0) {
            return;
        }
        // 保存窗口
        String defaultFilename = "RadarFlash.gif";
        String fileName = "";
        FileChooserModel fileChoose = new FileChooserModel();
        fileChoose.setDialogType(JFileChooser.SAVE_DIALOG);
        fileChoose.setSelectedFile(new File(defaultFilename));
        fileChoose.setAcceptAllFileFilterUsed(true);
        fileChoose
                .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
                    public boolean accept(File f) {
                        String fname = f.getName().toLowerCase();
                        return fname.endsWith(".gif");
                    }

                    public String getDescription() {
                        /** 文件描述 */
                        return " *.gif";
                    }
                });
        fileChoose.setCurrentDirectory(new File("."));
        int ret = fileChoose.showSaveDialog(this);

        if (ret == JFileChooser.APPROVE_OPTION) {
            fileName = fileChoose.getSelectedFile().getName().trim();
            if (!fileName.endsWith(".gif")) {
                JOptionPane.showMessageDialog(this, "文件名格式不正确。", "告警",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                // 图片动画
                AnimatedGifEncoder e = new AnimatedGifEncoder();//AnimatedGifEncoder、LZWEncoder、NeuQuant为开源代码,上网搜即可
                e.setRepeat(0);
                e.start(fileChoose.getSelectedFile().toString());

                // 播放速度
                int times = (int) (Double.parseDouble(palyTimeComboBox
                        .getSelectedItem().toString()) * 1000);
                int[] tableRows = filenametable.getSelectedRows();
                Image image = null;
                for (int i = 0; i < tableRows.length; i++) {
                    WDBImageRadar wdbStatel = contenes.get(tableRows[i]);
                    byte[] imageContent = wdbStatel.getContent();
                    if (imageContent != null) {
                        e.setDelay(times);
                        image = Toolkit.getDefaultToolkit().createImage(
                                imageContent);
                        ImageIcon icon = new ImageIcon(image);
                        e.addFrame(this.convertImageToBuffer(image));
                    }
                }
                e.finish();
                JOptionPane.showMessageDialog(this, "动态图片保存成功!", "消息",
                        JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                // TODO: handle exception
                JOptionPane.showMessageDialog(this, "动态图片保失败!", "错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
        if (ret == JFileChooser.CANCEL_OPTION) {
            return;
        }
    }

    // resetPicSize
    private BufferedImage convertImageToBuffer(Image pic) {
        BufferedImage bufferedImage = new BufferedImage(pic.getWidth(null),
                pic.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();
        g.drawImage(pic, 0, 0, null);
        g.dispose();
        return bufferedImage;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值