使用 JavaFX 上传和显示图像

Uploading and displaing an image using JavaFX

我正在使用 JavaFX,我在其中编写了包含 display Image 的代码,我想在我的场景中显示图像,但它不起作用。图片不显示。
当我使用 getAbsolutePath() 时,它也会显示错误。虽然我的代码已经完成了 70%,但我只是坚持在现场显示图像(没有上传)。
这是我正在使用的代码:

package application;
    
import java.util.HashMap;

import java.io.File;

import javafx.application.Application;
import javafx.stage.Stage; 
import javafx.stage.FileChooser;
import javafx.scene.Scene;
import javafx.scene.text.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;
import javafx.scene.control.Button;
import javafx.scene.control.Label;


public class Main extends Application {
    
    public ImageView iv;
    private GridPane gridpane;
 
    
//Image
    Image img = new Image("application/PA_Image.jpg");
    


    
    @Override
    public void start(Stage primaryStage) {
        try {
            

//Upload Image Button
            final Button button  = new Button("Display Image");  
            
//FileChooser
            final FileChooser fileChooser = new FileChooser();


            button.setOnAction(e -> {
                File selectedFile  = fileChooser.showOpenDialog(primaryStage);
                 if (selectedFile != null) { 
                     System.out.println(selectedFile.getAbsolutePath());
                      
                     Image image = new Image(selectedFile.getAbsolutePath()); 
                     iv = new ImageView(image);
                     
                     //Image uploadedImage = new Image(selectedFile.getAbsolutePath());
                     
                 }
            });
            
            final StackPane stac = new StackPane();
            stac.getChildren().add(button);

            Scene scene = new Scene(stac,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

所以,我需要的是通过单击按钮并选择图像来在我的场景中显示图像。

Image() 构造函数需要 URI 而不是绝对路径。 有两种方法可以解决这个问题。

  1. 通过添加所需的前缀 file://

    将您的 absolutePath 变成 URI
  2. 使用InputStream保存Image数据,创建Path后就不用管了。我比较喜欢这种方式

示例:

button.setOnAction(e -> {
    File selectedFile  = fileChooser.showOpenDialog(primaryStage);
    if (selectedFile != null) {
        System.out.println(selectedFile.getAbsolutePath());

        final InputStream targetStream; // Creating the InputStream
        try
        {
            targetStream = new DataInputStream(new FileInputStream(selectedFile));
            Image image = new Image(targetStream);
            iv = new ImageView(image);

        } catch (FileNotFoundException fileNotFoundException)
        {
            fileNotFoundException.printStackTrace();
        }


    }
});

您的代码还有一些其他问题。

  1. 创建您的 ImageView 并初始化它。不是在单击按钮时而是在开始时
  2. ImageView 添加到您的 Scene
  3. 不要在每次单击按钮时创建新的 ImageView,只需更改图像即可。

这是我基于你的代码:

public ImageView iv = new ImageView();
private GridPane gridpane;

@Override
public void start(Stage primaryStage) {
try {


    //Upload Image Button
    final Button button = new Button("Display Image");

    //FileChooser
    final FileChooser fileChooser = new FileChooser();


    button.setOnAction(e -> {
        File selectedFile = fileChooser.showOpenDialog(primaryStage);
        if (selectedFile != null) {

            final InputStream targetStream;
            try {
                targetStream = new DataInputStream(new FileInputStream(selectedFile));
                Image image = new Image(targetStream);
                iv.setImage(image); // Set Image

            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            }


        }
    });

    final StackPane stac = new StackPane();
    stac.getChildren().add(button);
    stac.getChildren().add(iv); // Add ImageView

    Scene scene = new Scene(stac, 1600, 800);
    //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();

} catch (Exception e) {
    e.printStackTrace();
}
}