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

swing生成图片之保存图片

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


//保存按钮监听事件       

saveBut.addActionListener(new ActionListener() {

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

            }
        });

 // 保存图片
    private void saveImage() {
        String strFilename = "";
        FileChooserModel fileChoose;
        int row = filenametable.getSelectedRow();
        if (row >= 0) {
            WDBImageRadar wDBImage = contenes.get(row);
            if (wDBImage != null) {
                strFilename = wDBImage.getFname().trim() + ".jpg";
            }
            fileChoose = new FileChooserModel();
            fileChoose.setDialogType(JFileChooser.SAVE_DIALOG);
            fileChoose.setSelectedFile(new File(strFilename));
            fileChoose.setAcceptAllFileFilterUsed(true);
            fileChoose
                    .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
                        public boolean accept(File f) {
                            String fname = f.getName().toLowerCase();
                            return fname.endsWith(".bmp")
                                    || fname.endsWith(".jpg")
                                    || fname.endsWith(".png")
                                    || fname.endsWith(".gif");
                        }

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

            if (ret == JFileChooser.APPROVE_OPTION) {
                String imageType = "";
                if (fileChoose.getSelectedFile().getName().endsWith(".bmp")) {
                    imageType = "bmp";
                } else if (fileChoose.getSelectedFile().getName()
                        .endsWith(".jpg")) {
                    imageType = "jpg";
                } else if (fileChoose.getSelectedFile().getName()
                        .endsWith(".png")) {
                    imageType = "png";
                } else if (fileChoose.getSelectedFile().getName()
                        .endsWith(".gif")) {
                    imageType = "gif";
                }
                if (imageType.equals("")) {
                    JOptionPane.showMessageDialog(this, "文件名格式不正确。", "告警",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
                try {
                    byte[] congtent = wDBImage.getContent();
                    Image image = null;
                    image = imagePane.getImage();
                    BufferedImage bufferedImage = convertImageToBuffer(image);
                    ImageIO.write(bufferedImage, imageType,
                            fileChoose.getSelectedFile());
                    JOptionPane.showMessageDialog(this, "保存图片成功。", "消息",
                            JOptionPane.INFORMATION_MESSAGE);
                    return;
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(this, "保存图片失败。", "错误",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
            if (ret == JFileChooser.CANCEL_OPTION) {
                return;
            }
        }
    }

抱歉!评论已关闭.