Tom*_*iak 13 java processing byte javafx image
我如何从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;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从我当前的对象(imageview),图像转换为byte [] ?,或者shoud我开始考虑使用其他类作为我的图像/或者指向参考位置并使用路径/网址?
Lor*_*cia 15
试试这个:
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.
Run Code Online (Sandbox Code Playgroud)
应该根据徽标类工作
你需要在写和读时指定一种格式,据我所知,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);
Run Code Online (Sandbox Code Playgroud)