如何检索保存为斑点的图像

时间:2019-05-15 21:34:58

标签: java mysql swing netbeans-8

请帮助我检索保存为#blob数据库#mysql中的图像

这是我用于保存图片的代码

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    filename =f.getAbsolutePath();
    ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(lbl_img.getWidth(), lbl_img.getHeight(), Image.SCALE_DEFAULT));
    lbl_img.setIcon(imageIcon);
  try {

        File image = new File(filename);
        FileInputStream fis = new FileInputStream (image);
        ByteArrayOutputStream bos= new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for(int readNum; (readNum=fis.read(buf))!=-1; ){

            bos.write(buf,0,readNum);
        }
        person_image=bos.toByteArray();
    }

    catch(Exception e){
        JOptionPane.showMessageDialog(null,e.getMessage());

    }

但是当我尝试使用

          byte[] img = rs.getBytes("IMAGE");
          ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage().getScaledInstance(lbl_image.getWidth(), lbl_image.getHeight(), Image.SCALE_SMOOTH));
          lbl_image.setIcon(imageIcon);

它没有给出任何错误,但没有检索图像。请帮助我找出问题所在

1 个答案:

答案 0 :(得分:0)

使用ImageIO.read将这些字节读取到BufferedImage