gpt4 book ai didi

java - 如何从 javafx imageView 获取 byte[]?

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:10 24 4
gpt4 key购买 nike

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

 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;
}

有没有一种方法可以将我当前的对象 (imageview),image) 转换为 byte[]?或者我应该开始考虑为我的图像使用其他类/或者指向具有引用和使用路径/网址?

最佳答案

试试这个:

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.

应该根据 Logo 类别工作

你需要在写入和读取时指定一种格式,据我所知不支持 bmp,所以你最终会在数据库中得到一个 png 字节数组

关于java - 如何从 javafx imageView 获取 byte[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com