Salvar Imagem como jpg?

2 respostas
R

Bom dia;

O que está de errado com o meu código?
Ela salva na maquina, mas ao abrir o arquivo “.jpg” ele aparece todo preto.
Sendo que imagem aparece em um panel, perfeitamente.

JFileChooser fc = new JFileChooser(new File(""));
            fc.showSaveDialog(fc);

            Conexao rs = new Conexao();
            rs.conecta();

            rs.executeSQL("SELECT FOTO FROM FOTOS WHERE SAFRA='"+tblFotos.getValueAt(lin,1)+"' and "+
                                       " ANO="+tblFotos.getValueAt(lin,2)+" and "+
                                       " site='"+tblFotos.getValueAt(lin,3)+"' and "+
                                       " Material='"+tblFotos.getValueAt(lin,4)+"' and "+
                                       " caracteristica='"+tblFotos.getValueAt(lin,5)+"'");
            try
            {
                rs.resultset.next();
                Image imagem = Toolkit.getDefaultToolkit().createImage(rs.resultset.getBytes("Foto"));
//                ImageIcon img = new ImageIcon (imagem);

                BufferedImage bi = new BufferedImage(imagem.getWidth(null), imagem.getHeight(null), BufferedImage.TYPE_INT_RGB);

                try {
                    ImageIO.write(bi, "jpg", new File(fc.getSelectedFile().toString()));
                 } catch (IOException ex) {
                    Logger.getLogger(Consulta.class.getName()).log(Level.SEVERE, null, ex);
                 }

            } catch (SQLException e)
            {
                JOptionPane.showMessageDialog(null, "Erro na captura da foto.");
            }

            rs.desconecta();
            rs = null;

2 Respostas

R

Consegui.

Segue o resultado.

try
            {
                rs.resultset.next();
                Image imagem = Toolkit.getDefaultToolkit().createImage(rs.resultset.getBytes("Foto"));
                ImageIcon img = new ImageIcon (imagem);

                BufferedImage bi = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = bi.createGraphics();
                g2d.drawImage(imagem, 0, 0, null);
                g2d.dispose();

                try
                {
                    ImageIO.write(bi, "JPG", new File(fc.getSelectedFile().toString()));
                } catch (IOException ex) {
                   Logger.getLogger(Consulta.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (SQLException e)
            {
                JOptionPane.showMessageDialog(null, "Erro na captura da foto.");
            }
ViniGodoy

Pra que vc criou aquele ImageIcon? Ele não é necessário.

Além disso, procure fazer seu próprio TableModel, assim vc evita aqueles getValue() horríveis ali. Se você não notou, caso uma coluna da sua tabela mude de ordem, esse código quebra (uma péssima política). No mínimo você deveria ter criado constantes para os números das colunas ao invés de usa-los diretamente.

Finalmente, use PreparedStatement ao invés de concatenar valores diretamente na sua query.

Criado 3 de maio de 2012
Ultima resposta 3 de mai. de 2012
Respostas 2
Participantes 2