如何从javafx imageView获取byte []? [英] How to get byte[] from javafx imageView?

查看:142
本文介绍了如何从javafx imageView获取byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从javafx image / imageview类获取byte []?我想将我的图像作为Blob存储到我的数据库中。这是我用它的方法

  public PreparedStatement prepareQuery( HSQLDBConnector连接器){
try {
Blob logoBlob = connector.connection.createBlob();
logoBlob.setBytes(0,logo.getImage()); //卡在这里
for(int i = 0,a = 1; i< data.length; i ++,a ++){

connector.prepStatCreateProfile.setString(a,data [i]);

}
//存储LOB

connector.prepStatCreateProfile.setBlob(11,logoBlob);

} catch(SQLException ex){
ex.printStackTrace();
}
return connector.prepStatCreateProfile;
}

有没有办法从我当前的对象转换(imageview) ,图像)到byte [] ?,或者shoud我开始考虑使用其他类作为我的图像/或者指向带引用的位置并使用路径/网址?


<尝试这个:

  BufferedImage bImage = SwingFXUtils.fromFXImage (logo.getImage(),null); 
ByteArrayOutputStream s = new ByteArrayOutputStream();
ImageIO.write(bImage,png,s);
byte [] res = s.toByteArray();
s.close(); //尤其是在使用不同的输出流时。

应该根据徽标类别工作



你需要在写和读时指定一种格式,并且据我记得bmp不受支持,所以你最终会在数据库上找到一个png字节数组


How do i get byte[] from javafx image/imageview class? I want to store my image as a Blob into my database.This is the method that i use for it

 public PreparedStatement prepareQuery(HSQLDBConnector connector) {
        try {
            Blob logoBlob = connector.connection.createBlob();
            logoBlob.setBytes(0,logo.getImage());//stuck  here
            for (int i = 0, a = 1; i < data.length; i++, a++) {

                connector.prepStatCreateProfile.setString(a, data[i]);

            }
            //store LOB

            connector.prepStatCreateProfile.setBlob(11, logoBlob);

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return connector.prepStatCreateProfile;
    }

Is there a way to convert from my current object (imageview),image) into byte[]?, or shoud i start to think about using other class for my image/ alternatively point to the location with reference and work with paths/urls?

解决方案

try this one:

BufferedImage bImage = SwingFXUtils.fromFXImage(logo.getImage(), null);
ByteArrayOutputStream s = new ByteArrayOutputStream();
ImageIO.write(bImage, "png", s);
byte[] res  = s.toByteArray();
s.close(); //especially if you are using a different output stream.

should work depending on the logo class

you need to specify a format while writing and reading, and as far as I remember bmp is not supported so you will end up with a png byte array on the database

这篇关于如何从javafx imageView获取byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆