yfabio
yfabio

Reputation: 141

How to convert byte array into javaFX image?

I am rewriting my question because I am struggle to find this solution. I've made a simple form to register some basic information about a student you know name, last name and so on. It also has an option to choose a photo by filechooser class to find a picture from the file system.

Let's take a look at this piece of code.

@FXML
public void onBrowserPhoto() throws IOException {
    File file = fileChooser.showOpenDialog(stage);
    if (file != null) {
        is = new FileInputStream(file);
        Image image = new Image(is);
        imageView.setImage(image);
    }

}

At this point when the user clicks the button they will choose a picture then I created a new fileInputStream and reference that is variable which is an inputream object.

private void businessOnSave() {
        if (currentStudent.getId() == 0) {
            try {
                currentStudent.setIs(is);
                currentStudent
                        .setGenger(buttonMale.isSelected() == true ? buttonMale.getText() : buttonFemale.getText());
                if (!checkStudent(currentStudent)) {
                    service.saveStudent(currentStudent);
                    studentUnBind(currentStudent);
                    sweep();
                    listView.getItems().add(currentStudent.getNickname());
                    buttonNew.disableProperty().setValue(false);
                    is.close();
                } else {
                    return;
                }

            } catch (ServiceException | IOException e) {
                e.printStackTrace();
            }
        }

When the use clicks on the save buttom this method will be called and the currentStudent variable receives that inputream (is) next it will be doing some stuff then I save it with service object. Everithing is perfect the student obj is saved.

Here is the method which save a new student

@Override
public void save(Student t) throws SQLException {
    try (PreparedStatement ps = con.prepareStatement(INSERT)) {
        ps.setString(1, t.getNickname());
        ps.setString(2, t.getLastName());
        ps.setString(3, t.getEmail());
        ps.setString(4, t.getPhone());
        ps.setString(5, t.getGenger());
        ps.setDate(6, Date.valueOf(t.getBirthDate()));
        ps.setBinaryStream(7, t.getIs());
        ps.executeUpdate();
    }
}

As you can see I am passing that inpustream variable to a preparedStatement object and it is ok, I confirm in the database the data is there. but when I bring this record from database it doesn't show the image on the UI.

here is the method that return a student record from that base.

public Student getStudentByName(String name) throws SQLException {
    Student student = new Student();
    try (PreparedStatement ps = con.prepareStatement(GET_STUDENT_BY_NAME)) {
        ps.setString(1, name);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                student.setId(rs.getInt("student_id"));
                student.setNickname(rs.getString("nickname"));
                student.setLastName(rs.getString("lastname"));
                student.setEmail(rs.getString("email"));
                student.setPhone(rs.getString("phone"));
                student.setGenger(rs.getString("genger"));
                student.setBirthDate(rs.getDate("birthdate").toLocalDate());                    

                try(InputStream is = rs.getBinaryStream("photo")){

                    BufferedImage bg = ImageIO.read(is);

                    if(bg != null){
                        Image image = SwingFXUtils.toFXImage(bg, null);
                        student.setPhoto(image);
                    }                       

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

            }

        }

    }

    return student;
}

The bg objec is null and i have no clue why because on database the image bytes is there. I have made another way to get the bytes then take care of where the student objet is created but it didn't work as well. If someone know a solution to this problem i will be happy. Thank you for taking the time to reading this question.

Upvotes: 10

Views: 11089

Answers (1)

Prakash
Prakash

Reputation: 359

To draw image, first convert a byte array into Image using the following code:

Image img = new Image(new ByteArrayInputStream(buffer));

Then use the drawImage method of graphicsContext:

graphicsContext.drawImage(img, x, y, width, heighth);

So my suggestion is not to store Image in the student object.

You can use byte array property and when you are displaying student record convert into Image as above.

Upvotes: 24

Related Questions