mysql - 上传图像的 javafx 质量

标签 mysql eclipse javafx phpmyadmin

我的应用程序使用 phpmyadmin 连接到 mysql 中的数据库并将图像存储在数据库中,但我的问题是当我从数据库下载图像并将图像发布到 imageview 上时,图像的质量非常低颜色也受到影响。

如果我使用 phpmyadmin 将图像直接发布到数据库,然后使用我的应用程序下载图像,则图像看起来不错,但如果我从我的应用程序上传图像,然后下载它,则质量很差。

发布图片的方式是将图片转为byte[],然后上传到使用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;
}

你能帮我吗?

编辑

@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();
    }

}

最佳答案

多亏了这个,我找到了问题的答案 this question .

我所做的是将数据库中的Blob更改为longblob并添加png而不是jpg,代码结果是这样的

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;
}

最后上传和下载相同质量的图像

关于mysql - 上传图像的 javafx 质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53366689/

相关文章:

java - 空指针访问 : The variable data can only be null at this location

mysql - 从 MySQL 数据库中获取按字段分组的最高值的行

mysql - 如何使用 JRuby 覆盖 ActiveRecord::Type::Boolean

java - 如何在 Eclipse 中使用 Maven 进行构建

java - 在 eclipse 中使用 Runtime.getRuntime().exec

javafx - 当移动其中的物体时,如何防止一个组自行移动?

multithreading - JavaFX 应用程序线程如何工作?

php - 如何显示多个 SQL 数据库行的内容

mysql - 显示/查看数据库 MySQL 中的索引

java - java.lang.module.ResolutionException: