我目前正在开发一个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”格式)缩小图片并丢失质量。有没有办法将图像位从数据库中拿到内存中,使用它们,然后将它们作为原始斑点放回数据库中,而无需改变大小或质量?

最佳答案

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

10-08 05:37
查看更多