现在的位置: 首页 > 综合 > 正文

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

2018年01月30日 ⁄ 综合 ⁄ 共 3239字 ⁄ 字号 评论关闭


// 保存动画按钮监听事件
        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;
    }

抱歉!评论已关闭.