【问题标题】:How to save an Uploded ImageView in javafx如何在 javafx 中保存上传的 ImageView
【发布时间】:2021-09-03 03:57:05
【问题描述】:

我正在开发一个 JavaFX 项目(应用程序),我已经设置了所有方法,所有方法都包括客户上传图像方法(他可以上传他的照片),我还有一个包含我所有应用程序信息的可序列化文件但它不允许我保存“客户上传”的图像。 我正在寻找一种方法,让我在更改场景或退出项目后保存这张照片

这是我的序列化代码:

public static void saveFile() {
    System.out.println("Saving");

    // Serialization
    try {
        // Saving of object in a file
        FileOutputStream file = new FileOutputStream("Rest.ser");
        ObjectOutputStream out = new ObjectOutputStream(file);

        out.writeObject(restaurant);

        out.close();
        file.close();

    }

    catch (IOException ex) {
        System.out.println(ex);
    }
}

public static boolean readingFile() {

    try {
        File filec = new File("Rest.ser");
        if (filec.isFile() == false || filec.canRead() == false)
            return false;
        FileInputStream file = new FileInputStream(filec);
        ObjectInputStream in = new ObjectInputStream(file);

        restaurant = (Restaurant) in.readObject();
        
        in.close();
        file.close();

        System.out.println("Object has been deserialized ");
        return true;
    }

    catch (IOException ex) {
        System.out.println("No ser file");
    }

    catch (ClassNotFoundException ex) {
        System.out.println("ClassNotFoundException is caught");
    }
    return false;
}

这是我的照片上传代码:

@FXML
private void UploadImageActionPerformed(ActionEvent event) {

    FileChooser fileChooser = new FileChooser();
   
    //Set extension filter
    FileChooser.ExtensionFilter extFilterJPG
            = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
    FileChooser.ExtensionFilter extFilterjpg
            = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
    FileChooser.ExtensionFilter extFilterPNG
            = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
    FileChooser.ExtensionFilter extFilterpng
            = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
    fileChooser.getExtensionFilters()
            .addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
    //Show open file dialog
    File file = fileChooser.showOpenDialog(null);

    try {
        BufferedImage bufferedImage = ImageIO.read(file);
       WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
        Customerimage.setImage(image);
        Customerimage.setFitWidth(200);
        Customerimage.setFitHeight(200);
        Customerimage.scaleXProperty();
        Customerimage.scaleYProperty();
        Customerimage.setSmooth(true);
        Customerimage.setCache(true);
    
        FileInputStream fin = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        

        for (int readNum; (readNum = fin.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        byte[] person_image = bos.toByteArray();

    } catch (IOException ex) {
        Logger.getLogger("ss");
    }

    
 }

蚂蚁可以帮助我:) 谢谢。

【问题讨论】:

  • “上传”是什么意思?如果您的应用程序完全是本地的,并且您实际上只是链接照片,那么只需存储路径(或将图像复制到应用程序特定的位置,然后存储 that 路径);从存储的路径加载图像(如果文件丢失,提醒用户)。如果您的应用程序是某个远程应用程序的接口,请将图像发送到服务器并让服务器存储它,但这是合适的;通过复制文件来发送图像(通过这种方式发送压缩版本而不是原始的、未压缩的像素数据)。
  • 请遵守 java 命名约定
  • 我想将图像上传到的场景称为@FXML screen,我想我无法理解你所说的,但我想不出可以帮助我存储图像路径的方法或代码。我是 javaFX 新手,我还在学习:\
  • 您可以加载一个image directly from a URL,它是该位置的字符串路径。 ImageView 也是如此。您可以根据用户在FileChooser 中选择的文件构建此位置。您可能不需要加载和保存图像数据所需的所有代码。如果您想记住图像位置,可以将其作为字符串存储在某处。
  • 无关,但对于序列化,在很多情况下,我会推荐使用Jackson 来序列化为 JSON 或 XML,而不是 Java 原生对象序列化器。那将用于元数据,例如文件位置。如果您需要保存实际的图像数据,则最好存储为标准格式,例如 PNG 或 JPEG。这两种方法可以结合使用(JSON 中的元数据和压缩图像格式的存储图像位)。

标签: javafx imageview saving-data


【解决方案1】:

您可以直接从 URL 加载图像,该 URL 是该位置的字符串路径。 ImageView 也是如此。您可以根据用户在 FileChooser 中选择的文件构建此位置。您可能不需要加载和保存图像数据所需的所有代码。如果您想记住图像位置,您可以将其作为字符串存储在某处。

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多