java MySQL blob 到 Netbeans JLabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15426714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:32:38  来源:igfitidea点击:

MySQL blob to Netbeans JLabel

javaswingnetbeansblobjlabel

提问by kelvzy

I have a blob type field in my MySQL, I want to put the data in this field in JLabelas Icon. For example this JLabelwill be user's Profile Picture in my form.

我的 MySQL 中有一个 blob 类型的字段,我想将此字段中的数据JLabel作为图标。例如,这JLabel将是我表单中的用户个人资料图片。

I used this codes but nothing happens and also I want to fix to widthor fix any image size in my jlabel

我使用了这些代码,但没有任何反应,而且我想fix to width或修复我的 jlabel 中的任何图像大小

DefaultTableModel pic = MyDB.DataTable("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
     if (pic.getRowCount() > 0){
         Blob blob = pic.getBlob(1);
         byte[] image1 = blob.getBytes(1, ALLBITS);
         ImageIcon image = new ImageIcon(image1);
         picture.setIcon(image);
         getContentPane().add(picture);
         setVisible(true);
     }

pictureis the name of my jlabel

picture是我的 jlabel 的名字

回答by Alya'a Gamal

First : Return the Input stream from your database :

首先:从您的数据库返回输入流:

String query = "SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'";
stmt = (PreparedStatement) con.prepareStatement(query);
ResultSet result = stmt.executeQuery();

Returned image from Database

从数据库返回的图像

BufferedImage im = ImageIO.read(result.getBinaryStream(1));

Then make rezise to this image :

然后重新调整这张图片:

im =linearResizeBi(im, /*width*/, /*height*/);

linearResizeBiMethod :

linearResizeBi方法:

static public BufferedImage linearResizeBi(BufferedImage origin, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height ,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        float xScale = (float)width / origin.getWidth();
        float yScale = (float)height / origin.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
        g.drawRenderedImage(origin,at);
        g.dispose();
        return resizedImage;
    }

then make the image is an Icon:

然后使图像是一个图标:

ImageIcon image1 = new ImageIcon(im);

ImageIcon image1 = new ImageIcon(im);

then add the Icon to The Jlabel :

然后将图标添加到 Jlabel :

picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);

回答by kelvzy

I got my filename should be like this

我的文件名应该是这样的

  txtPicPath.setText(file.getAbsoluteFile().toString());

and i used these codes, and also it fits with the jlabel size

我使用了这些代码,它也符合 jlabel 的大小

 ResultSet rst = MyDB.rsFetch("SELECT `Picture` FROM `photo` WHERE `Employee ID` = '"+ Data.User.getText()+"'");
         while (rst.next()) {
         Blob filenameBlob = rst.getBlob("Picture");
         byte[] content = filenameBlob.getBytes(1L,(int)filenameBlob.length());
         ImageIcon ik = new ImageIcon(content);
         Image img = ik.getImage();
         Image newimg = img.getScaledInstance(Data.picture.getWidth(), Data.picture.getHeight(), java.awt.Image.SCALE_SMOOTH);
         ik = new ImageIcon(newimg);
         Data.picture.setIcon(ik);
         }

回答by AurA

Use a resultset

使用结果集

 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");

You may change from

你可以从

Blob blob = rs.getBlob(1);

to another altenative of

另一种选择

InputStream binaryStream = rs.getBinaryStream(1);

You can refer to the official guide of getting image from a blog here http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/blob.html

您可以在此处参考从博客获取图像的官方指南 http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/blob.html

回答by Biswajit

Blob has a getBinaryStream() which returns a stream of bytes containing the data stored in the blob.

Blob 有一个 getBinaryStream(),它返回一个字节流,其中包含存储在 Blob 中的数据。

ImageIcon, which implements Icon, has a constructor which takes a byte array as argument.

实现 Icon 的 ImageIcon 有一个构造函数,它接受一个字节数组作为参数。

JLabel has a setIcon(Icon) method.

JLabel 有一个 setIcon(Icon) 方法。

label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));

回答by Akhand pratap Singh

Try:

尝试:

picture.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));