我该如何从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[]?或者我应该考虑使用其他类来处理我的图像,或者是通过引用指向位置并使用路径/ URL进行处理?
Image.write
的文档说明如下:该方法在写入操作完成后不会关闭提供的 OutputStream;如果需要,调用方有责任关闭流。这表明必须关闭 ByteArrayOutputStream 或使用 try-with-ressource 块。 - Oliver Jan KrylowImageView
转换为字节数组而不是Image
? - Faizan Mubasher