samedi 1 août 2015

No output in JLabel when trying to retrieve blob image from JLabel

I am trying to retrieve a Blob(Image) file from database and want to show it in a JLabel . But it doesnt show up in JLabel . Why ? one More query if there any to know if my Byte array Contains valid data ? Here is my code

import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Show extends JFrame {

private JPanel contentPane;
private JTextField id;
//BufferedImage bufImg = null;
JLabel img=null;
//   InputStream in=null;
ImageIcon imgs=null;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Show frame = new Show();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
Connection con=null;
public Show() {
    con=dB.Connect();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 664, 499);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    id = new JTextField();
    id.setBounds(158, 23, 86, 20);
    contentPane.add(id);
    id.setColumns(10);

    JButton show = new JButton("New button");
    show.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{

                String q="select * from showme where id='"+id.getText()+"'";
                PreparedStatement ps=con.prepareStatement(q);
                ResultSet rs=ps.executeQuery(); 

                InputStream in = rs.getBinaryStream("image");

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead = in.read(buffer);
                while (bytesRead > -1)
                {
                  out.write(buffer);
                  bytesRead = in.read(buffer);
                }
                byte[] picture = out.toByteArray();
                ImageIcon mage = new ImageIcon(picture);
                Image im = mage.getImage();
                ImageIcon newImage = new ImageIcon(im);
                //Image myImg = im.getScaledInstance(img.getWidth(),img.getWidth(),ImageObserver.WIDTH);
                //ImageIcon newImage = new ImageIcon(myImg);
                img.setIcon(newImage);
                JOptionPane.showMessageDialog(null, "Done");

                /*//I Also used this method . Still No Result in Jlabel 
                 while(rs.next()){
                    byte[] imgss = rs.getBytes("image");
                    //Resize The ImageIcon
                    ImageIcon mage = new ImageIcon(imgss);
                    Image im = mage.getImage();
                    //Image myImg = im.getScaledInstance(imgss.length, imgss.length,imgss.length);
                    //ImageIcon newImage = new ImageIcon(im);
                    img.setIcon(new ImageIcon(im));
                    JOptionPane.showMessageDialog(null, "Done");
                 }
                    */

                in.close();
                rs.close();
                ps.close();

                }catch(Exception c)
                {
                    c.printStackTrace();
                }
        }
    });
    show.setBounds(302, 22, 89, 23);
    contentPane.add(show);

    img = new JLabel("");
    img.setBounds(114, 54, 269, 171);
    contentPane.add(img);
}

}

Aucun commentaire:

Enregistrer un commentaire