我的TableView显示图片的地址,而不是实际的图片

时间:2020-01-17 12:09:35

标签: java hibernate javafx java-io

嘿,我正在尝试保存一个名为“阿凡达”的对象,然后检索该对象并显示其图片“图像”。我得到这个结果: enter image description here

我的头像类:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- displays site properly based on user's device -->
  <link href="https://fonts.googleapis.com/css?family=Poppins:200,400,600&display=swap" rel="stylesheet">
  <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
  <link rel="stylesheet" href="style.css">
  <title>Frontend Mentor | Four card feature section</title>

  <!-- Feel free to remove these styles or customise in your own stylesheet ? -->
  <style>
    .attribution { font-size: 11px; text-align: center; }
    .attribution a { color: hsl(228, 45%, 44%); }
  </style>
</head>
<body>
  <div class="container">
    <div class="part1">
      <p>Reliable, efficient delivery</p>
      <h2>Powered by Technology</h2>
      <p> Our Artificial Intelligence powered tools use millions of project data points to ensure that your project is successful.</p>
    </div>
    <div class="part2">
    <div class="supervisor">
      <h3>Supervisor</h3>
      <p>Monitors activity to identify project roadblock.</p>
    </div>
    <div class="wrapper">
    <div class="teambuilder">
      <h3>Team Builder</h3>
      <p>Scans our talent network to create the optimal team for your project.</p>
    </div>
    <div class="karma">
      <h3>Karma</h3>
      <p>Regularly evaluates our talent to ensure quality.</p>
    </div>
    </div>
    <div class="calculator">
      <h3>Calculator</h3>
      <p> Uses data from past projects to provide better delivery estimates.</p>
    </div>

  </div>
  </div>
  <footer>
    <p class="attribution">
      Challenge by <a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a>. 
      Coded by <a href="#">Your Name Here</a>.
    </p>
  </footer>
</body>
</html>

我如何保存对象:

body {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
   background-color: hsl(0, 0%, 98%);
   text-rendering: optimizeLegibility;
}

.container {
   width: 60%;
   height: 60%;
} 

从头像表中检索数据:

@Entity
@Table(name="avatar")
public class Avatar implements java.io.Serializable {

    @Id
    private Integer avatarId;
    @Column(name = "image")
    private byte[] image;
//getters and setters

控制器:

session.getTransaction().begin();

        File file = new File("PicturePath");
        byte[] bFile = new byte[(int) file.length()];

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Avatar avatar = new Avatar();
        avatar.setAvatarId(1);
        avatar.setImage(bFile);
        session.save(avatar);
        session.getTransaction().commit();

我的FXML文件:

public List<Avatar> avatarList(){
        session.getTransaction().begin();
        List<Avatar> avatar = session.createQuery("from Avatar").list();
        session.getTransaction().commit();
        return avatar;
    }

我希望实际的图片显示在表格视图中。

1 个答案:

答案 0 :(得分:3)

之所以看不到图像,是因为尽管您正确地向TableView提供了图像的字节,但是您没有告诉它如何呈现图像。您需要的是自定义TableCell,它将字节数组呈现为ImageView。因此,我将执行以下操作:

首先在TableColumn的fxml文件中添加一个id属性:

<TableColumn fx:id="imageColumn" text="Picture" style="-fx-backround-color:#33d9b2">
    <cellValueFactory>
        <PropertyValueFactory property="image"/>
    </cellValueFactory>
</TableColumn>

然后在Controller内引用它,并在其中添加CellFactory

@FXML
public TableView<Avatar> produits;

@FXML
public TableColumn<Avatar, byte[]> imageColumn;

@FXML
public void initialize() {
    ...
    imageColumn.setCellFactory(param -> new TableCell<Avatar, byte[]>() {

        private ImageView imageView = new ImageView();

        @Override
        protected void updateItem(byte[] item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setText(null);
                setGraphic(null);
            } else {
                imageView.setImage(getImageFromBytes(item));
                setGraphic(imageView);
            }
            this.setItem(item);
        }
    });
    ...
}

CellFactory为您的每个Avatar项目创建一个表格单元格,该单元格的updateItem()方法将字节转换为Image,然后将其渲染为{{ 1}}(如果单元格不为空)。 这是将字节数组转换为JavaFX Image的一种方法:

ImageView

重要说明::我将避免从private Image getImageFromBytes(byte[] imgBytes) { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(imgBytes); BufferedImage bufferedImage = ImageIO.read(inputStream); return SwingFXUtils.toFXImage(bufferedImage, null); } catch (IOException e) { e.printStackTrace(); } return null; } 内部调用getImageFromBytes()方法,尤其是对于具有大量记录的TableView。我只是为了简单起见。您可能需要预先转换图像,然后将其存储在地图或其他内容中。

编辑:更改了updateItem()方法,使其不会在每次调用时创建新的updateItem()

相关问题