我正在尝试从数据库中检索Blob(Image)文件,并希望在JLabel中显示它。但它并没有出现在JLabel中。为什么?如果知道我的Byte数组是否包含有效数据,还有一个查询吗?这是我的代码
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();
while(rs.next()){
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);
}
}
这是插入图片的代码
import java.awt.EventQueue;
public class ImgC1 {
private JFrame frame;
private JLabel image;
private File images;
DataBufferByte data;
String s;
WritableRaster raster;
ImageIcon photo;
private static final int IMG_WIDTH = 120;
private static final int IMG_HEIGHT = 120;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImgC1 window = new ImgC1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ImgC1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
Connection con=null;
private JTextField id;
public ImageIcon ResizeImage(String imgPath){
ImageIcon MyImage = new ImageIcon(imgPath);
Image img = MyImage.getImage();
Image newImage = img.getScaledInstance(image.getWidth(), image.getHeight(),Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(newImage);
return image;
}
private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
public Image toImage(BufferedImage bufferedImage) {
return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}
private void initialize() {
con=dB.Connect();
frame = new JFrame();
frame.setBounds(100, 100, 643, 444);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton browse = new JButton("Browse");
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
chooser.setCurrentDirectory(images);
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
int i = chooser.showSaveDialog(null);
if (i == JFileChooser.APPROVE_OPTION) {
images = chooser.getSelectedFile();
image.setText(images.getAbsolutePath());
try {
BufferedImage originalImage = ImageIO.read(images);
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
photo=new ImageIcon();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
photo = new ImageIcon(toImage(resizeImageJpg));
raster = resizeImageJpg.getRaster();
data = (DataBufferByte) raster.getDataBuffer();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
});
browse.setBounds(270, 282, 89, 23);
frame.getContentPane().add(browse);
JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
PreparedStatement ps = con.prepareStatement("insert into showme(id,image) values (?,?)");
//InputStream is = new FileInputStream(new File(s));
byte[] bt=data.getData();
ps.setString(1, id.getText());
ps.setBytes(2,bt);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Inserted");
ps.close();
}catch(Exception c)
{
c.printStackTrace();
}
}
});
save.setBounds(270, 316, 89, 23);
frame.getContentPane().add(save);
image = new JLabel("New label");
image.setBounds(236, 141, 168, 130);
frame.getContentPane().add(image);
id = new JTextField();
id.setBounds(206, 60, 232, 23);
frame.getContentPane().add(id);
id.setColumns(10);
}
}