简体   繁体   English

从文件上传 Java FX 图像

[英]Java FX image upload from file

i was trying create a image icon on label text field using file chooser but the current code works for java and not java fx if anyone know to fix my code it will be very helpful我正在尝试使用文件选择器在标签文本字段上创建一个图像图标,但当前代码适用于 java 而不是 java fx 如果有人知道修复我的代码,这将非常有帮助

package EmploymentPayroll;

import java.awt.Image;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import javafx.scene.control.Label;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JOptionPane;


/**
 * FXML Controller class
 *
 * @author Shafeen
 */
public class AddEmployeeController implements Initializable {

    @FXML
    private Label img;
    private ImageIcon format = null;
    String filename = null;
    byte[] person_image = null;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

  @FXML
    private void UploadImageActionPerformed(ActionEvent event) {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();

        filename = f.getAbsolutePath();
        *error*  ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(img.getWidth(), img.getHeight(), Image.SCALE_DEFAULT));

        *error* img.setIcon(imageIcon);       


         try {
            File image = new File(filename);
            FileInputStream fis = new FileInputStream (image);
            ByteArrayOutputStream bos= new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for(int readNum; (readNum=fis.read(buf))!=-1; ){
                bos.write(buf,0,readNum);
            }
            person_image=bos.toByteArray();
        }

        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }

    }   

this is the java fx form where when i click on picture it should load the file and save as BLOB type这是 java fx 表单,当我点击图片时,它应该加载文件并保存为 BLOB 类型

在此处输入图片说明

error one错误一

在此处输入图片说明

error two错误二

在此处输入图片说明

thanks for help感谢帮助

if anyone is looking to upload images using java fx and save in database in blob format then you can refer this code thank you如果有人希望使用 java fx 上传图像并以 blob 格式保存在数据库中,那么您可以参考此代码,谢谢

@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);
            img.setImage(image);
            img.setFitWidth(200);
            img.setFitHeight(200);
            img.scaleXProperty();
            img.scaleYProperty();
            img.setSmooth(true);
            img.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);
            }
            person_image = bos.toByteArray();

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


byte[] person_image = null;

 @FXML
    private void handleaddemployee(ActionEvent event) throws IOException {

        try {
                String empID = "nill";
                if (!txtempID.getText().equals("")) {
                    empID = txtempID.getText();
                }

                Employee e = new Employee(
                        empID,
                        person_image 
                );
                dbemployee.addEmployee(e);

            } else {
                 System.out.println("not working");
            }

        } catch (SQLException ex) {
           System.out.println("error");

        } catch (NumberFormatException e) {
         System.out.println("number error");

        }

    }

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

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