0

my application connects to a database in mysql using phpmyadmin and stores an image in the database, but my problem is when I download the image from the database and post the image on a imageview the image has a very low quality where also it's color is being afected too.

if I post the image directly on the database using phpmyadmin and then download the image using my app the image looks fine, but if I upload the image from my app and then i download it then the quality is bad.

the way to post the image is past the image to a byte[] and then uploading to the database that uses the type blob.

private byte[] imagenToByte(Image imagen) {
    BufferedImage bufferimage = SwingFXUtils.fromFXImage(imagen, null);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      try {
        ImageIO.write(bufferimage, "jpg", output );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      byte [] data = output.toByteArray();
      return data;
}

can you help me please?

EDIT

@FXML
public void eventoBotonSeleccionarImagen() {
    FileChooser imagenSeleccionada = new FileChooser();
    FileChooser.ExtensionFilter filtroImagenjpg = new ExtensionFilter("Archivos *.jpg", "*.jpg");
    FileChooser.ExtensionFilter filtroImagenJPG = new ExtensionFilter("Archivos *.JPG", "*.JPG");

    File archivo = imagenSeleccionada.showOpenDialog(null);

    try {
        BufferedImage bufferedImage = ImageIO.read(archivo);
        Image image = SwingFXUtils.toFXImage(bufferedImage, null);
        imageViewMonstruo.setImage(image);
    }
    catch(Exception e) {
        e.printStackTrace();
    }

}
Adriiboom
  • 85
  • 13
  • 1
    How to you load/create the original image (`Image` object)? Why do you use JPEG and not e.g. PNG? – Itai Nov 19 '18 at 08:04
  • Look at the edit, the purpose is to select the image from the pc, I use jpg because the images are in jpg, and it works, but the image uploaded has a low quality with it's colors being afected. – Adriiboom Nov 19 '18 at 09:33

1 Answers1

0

I found the answer to the question thanks to this this question.

What I've done is changing from Blob to longblob in the database and adding png instead of jpg, the code result is this

private byte[] imagenToByte(Image imagen) {
    BufferedImage bufferimage = SwingFXUtils.fromFXImage(imagen, null);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      try {
        ImageIO.write(bufferimage, "png", output );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      byte [] data = output.toByteArray();
      return data;
}

And finally it upload and download the images with the same quality

Slaw
  • 37,820
  • 8
  • 53
  • 80
Adriiboom
  • 85
  • 13