简体   繁体   English

图像未显示在 JavaFX ImageView

[英]Image is not shown in JavaFX ImageView

I want to show an image in an ImageView in JavaFX.我想在 JavaFX 中的 ImageView 中显示图像。 The image should be fetched from the SQLite Database and shown when the scene loads.图像应从 SQLite 数据库中获取,并在场景加载时显示。 I use the following code snippet to fetch the code from the database and to show it in previously created ImageView.我使用以下代码片段从数据库中获取代码并将其显示在先前创建的 ImageView 中。 But the Image is not shown as expected.但图像未按预期显示。 Am I doing it wrong?我做错了吗? ps I have tested changing the path in both FileOutputStream and when reading the file from the directory to src/image.jpg . ps 我已经测试了在FileOutputStream和将文件从目录读取到src/image.jpg时更改路径。 But nothing seems to work.但似乎没有任何效果。 But when I put an image file manually in the src directory and try to read it, it works like a charm.但是当我手动将图像文件放入 src 目录并尝试读取它时,它就像一个魅力。 But I want to show an image fetched from the database.但我想显示从数据库中获取的图像。

@FXML
private void loadEquipmentData() throws IOException {
    try{
        Connection conn = DatabaseConnection.getConnection();
        PreparedStatement checkStmt = conn.prepareStatement(selectEquipmentQuery);
        ResultSet rs = checkStmt.executeQuery();

        while(rs.next()){
            InputStream is = rs.getBinaryStream("Image");
            OutputStream os = new FileOutputStream(new File("image.jpg"));
            byte[] content = new byte[1024];
            int size = 0;
            while ((size = is.read(content)) != -1){
                os.write(content, 0, size);
            }
            os.close();
            is.close();

            File file = new File("file:image.jpg");
            image = new Image(file.toURI().toString());
            this.eq_img.setImage(image);
            this.eq_img.setPreserveRatio(true);
            this.eq_img.setFitWidth(318.0);
            this.eq_img.setFitHeight(253.0);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (FileNotFoundException fnf) {
        fnf.printStackTrace();
    }

}

When writing the image to database I use this code to choose image.将图像写入数据库时,我使用此代码选择图像。

private FileChooser fileChooser;
private File file;
private FileInputStream fis;

@FXML
void chooseFiles() throws FileNotFoundException {
    CommonStageSingleton stageSingleton = CommonStageSingleton.getInstance();
    Stage userStage = stageSingleton.getMainWindow();
    fileChooser = new FileChooser();
    fileChooser.getExtensionFilters().addAll(
            new FileChooser.ExtensionFilter("Image Files","*.jpg","*gif","*.jpeg","*.png")
    );

    file = fileChooser.showOpenDialog(userStage);
    if(file != null){
        fis = new FileInputStream(file);
        ta_imagePath.setText(file.getAbsolutePath());
    }
}

And these to enter it to database.并将这些输入到数据库中。

Connection conn = DatabaseConnection.getConnection();
PreparedStatement checkStmt = conn.prepareStatement(checkEquipmentQuery);
PreparedStatement st = conn.prepareStatement(insertEquipmentQuery);
st.setBinaryStream(12, (InputStream)fis, (int)file.length());

I might have done something to save this image not as a .jpg .我可能已经做了一些事情来将此图像保存为.jpg

@dan1st pointed out what I am missing here. @dan1st 指出了我在这里缺少的东西。 I have used webp image as the input image when writing into the database.我在写入数据库时使用了 webp 图像作为输入图像。 JavaFX does not support webp image type. JavaFX 不支持 webp 图像类型。 Now I use jpeg and that gives no any issue sofar.现在我使用jpeg,到目前为止没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM