从数据库显示字节作为图像在javafx imageview
问题描述:
请我一直坚持如何转换我的存储图像从我的 数据库,并显示它在javafx imageview中的图像。从数据库显示字节作为图像在javafx imageview
以前提出的所有问题都没有帮到我。 我使用objectdb作为我的数据库 我也用FXML建立我的GUI
for (Person p : person) {
name.setText(p.getName());
gender.setText(p.getGender());
byte[] byteArray = p.getImage();
image.setImage(new Image(new ByteArrayInputStream(byteArray)));
}
答
我将展示使用一个文件选择保存到数据库中,并在写作的图像文件的详细步骤目录(文件夹),并将其显示在fmxl GUI中的imageview中。
以下下面按钮事件触发时,或当你运行你发布的代码从控制器
FileChooser choose = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
choose.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = choose.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
byte[] b;
try (ByteArrayOutputStream out = new ByteArrayOutputStream(262144)) {
ImageIO.write(bufferedImage, "jpg", out);
out.flush();
b = out.toByteArray();
}
EntityService service = new EntityService();
Person p = new Person();
p.setId(UUID.randomUUID().toString());
p.setImage(b);
service.putPerson(p);
} catch (IOException e) {
e.printStackTrace();
}
Person p = service.getPerson();
byte[] byteArray = p.getImage();
ByteArrayInputStream in = new ByteArrayInputStream(byteArray);
BufferedImage read = ImageIO.read(in);
image.setImage(SwingFXUtils.toFXImage(read, null));
String output = "C:\\java\\images\\1.jpg";
try (FileOutputStream fos = new FileOutputStream(output)) {
fos.write(byteArray);
} catch (FileNotFoundException ex) {
System.out.println("FileNotFoundException : " + ex);
} catch (IOException ioe) {
System.out.println("IOException : " + ioe);
}
所以会发生什么初始化 ? – 2014-09-01 17:15:35
当我运行我的代码时,我得到从数据库返回的名称和性别,但imageview框保持空白。 – 2014-09-02 06:33:43