我目前正在开发一个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放回到数据库中而不进行任何大小或质量的更改?
答案 0 :(得分:0)
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();
}
}