![](/img/trans.png)
[英]How to retreive image (byte type) from database to tableview (imageView)/ JavaFx
[英]How to get byte[] from javafx imageView?
我如何从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),图像转换为byte [] ?,或者shoud我开始考虑使用其他类作为我的图像/或者指向参考位置并使用路径/网址?
试试这个:
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.
应该根据徽标类工作
你需要在写和读时指定一种格式,据我所知,bmp不受支持,所以你最终会在数据库上输入一个png字节数组
纯java fx解决方案跟踪 (==你将不得不填补缺失点:)
Image i = logo.getImage();
PixelReader pr = i.getPixelReader();
PixelFormat f = pr.getPixelFormat();
WriteablePixelFromat wf = f.getIntArgbInstance(); //???
int[] buffer = new int[size as desumed from the format f, should be i.width*i.height*4];
pr.getPixels(int 0, int 0, int i.width, i.height, wf, buffer, 0, 0);
Lorenzo的回答是正确的,这个答案只是考察了效率和可移植性方面。
根据图像类型和存储要求,将图像转换为压缩格式进行存储可能会有效,例如:
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ImageIO.write(SwingFXUtils.fromFXImage(fxImage, null), "png", byteOutput);
Blob logoBlob = connector.connection.createBlob();
logoBlob.setBytes(0, byteOutput.toByteArray());
在保持映像之前转换为常用格式(如png)的另一个优点是,处理数据库的其他程序将能够读取映像而无需尝试将其从JavaFX特定字节数组存储格式转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.