我目前正在开发一个 Swing 程序,我将图片作为 blob 存储在 mysql 数据库中。由于此处描述的原因太长,我有时必须将之前从数据库加载的图片保存回数据库中。
下面是我当前如何在 java 中从数据库加载/保存到数据库的示例代码:
package oldutils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.sql.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import utils.SingletonConnection;
public class DisplayImage2 extends JFrame
{
private Connection connection = null;
private ResultSet result = null;
private PreparedStatement statement = null;
private Blob blob = null;
private ImageIcon imageIcon = null;
private JLabel labelPhoto = null;
private Image image = null;
private String query = null;
private byte[] byteArray = null;
public DisplayImage2()
{
super("Image Display");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connection = SingletonConnection.getConnection();
try
{
query = "SELECT * FROM photo WHERE pkPhoto=?";
statement = connection.prepareStatement(query);
statement.setInt(1, 61);
result = statement.executeQuery();
while ( result.next() )
{
byteArray = result.getBytes("photoImage");
image = Toolkit.getDefaultToolkit().createImage(byteArray);
imageIcon = new ImageIcon(image);
}
labelPhoto = new JLabel();
labelPhoto.setIcon(imageIcon);
add(labelPhoto);
setVisible(true);
try
{
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D bImageGraphics = bufferedImage.createGraphics(); // Obtain graphics
bImageGraphics.drawImage(image, null, null); //Draw Image into BufferedImage
RenderedImage rImage = (RenderedImage) bufferedImage; // Cast to rendered image
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(rImage, "png", bas);
byteArray = bas.toByteArray();
blob = new javax.sql.rowset.serial.SerialBlob(byteArray);
}
catch (IOException e)
{
e.printStackTrace();
}
query = "INSERT INTO photo (photoName, photoImage) VALUES(?,?)";
statement = connection.prepareStatement(query);
statement.setString(1, "Image Test");
statement.setBlob(2, blob);
statement.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new DisplayImage2();
}
}
问题在于,在首先将图片插入表中之前,首先将图片的压缩率/大小/质量仔细“制作”为 JPEG 图片。但是使用上面代码中的方法,使用 ImageIO.write(),我被迫在重新保存图片时增大图片的原始大小(例如“PNG”)或缩小图片(例如“JPG”)并丢失质量。有没有办法将图像位从数据库获取到内存,使用它们,然后将它们作为原始 blob 返回到数据库,而不改变大小或质量?
最佳答案
Leo 在他的评论中回答了这个问题,但由于他如此谦虚地否认投票点,所以我自己写它,以便它可以帮助某人......
所需要做的就是(假设 byteArray 仍在内存中)跳过所有 ImageBuffer/Graphics/.. 创建和操作部分,重用相同的 ByteArray[] 并像以前一样简单地获取一个 blob,然后保存它。
public DisplayImage2()
{
super("Image Display");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connection = SingletonConnection.getConnection();
// LOAD FROM BLOB
try
{
query = "SELECT * FROM photo WHERE pkPhoto=?";
statement = connection.prepareStatement(query);
statement.setInt(1, 61);
result = statement.executeQuery();
while ( result.next() )
{
byteArray = result.getBytes("photoImage");
image = Toolkit.getDefaultToolkit().createImage(byteArray);
imageIcon = new ImageIcon(image);
}
labelPhoto = new JLabel();
labelPhoto.setIcon(imageIcon);
add(labelPhoto);
setVisible(true);
// SAVE TO BLOB
blob = new javax.sql.rowset.serial.SerialBlob(byteArray);
query = "INSERT INTO photo (photoName, photoImage) VALUES(?,?)";
statement = connection.prepareStatement(query);
statement.setString(1, "Image Test");
statement.setBlob(2, blob);
statement.executeUpdate();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
关于java - Mysql blob 图片转换为 awt.image,然后 awt.image 转换为 blob,无需更改大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691544/