mysql blob图片到awt.image,然后awt.image到blob,不改变大小

Zek101

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

Zek101

狮子座的评论回答了这个问题,但是由于他谦虚地拒绝投票,我亲自写了它,以便对某人有所帮助。

所需的所有操作(假设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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Oracle导入BLOB(Image)到Hive

Java Multipart 文件到 blob 然后返回到 Image 有大量随机字符

Java的:是'sun.awt.image`包过时?

什么是sun.awt.image.BufImgSurfaceData?

sun.awt.image.ByteInterleavedRaster.setPixels

File中的java.awt.Image

将AWT图形代码移植到Android

转换AWT /光标到JavaFX的场景/光标?

可调整大小的滚动窗格到awt框架

文字到图片与mysql信息

将blob(image)传递到php / laravel并获取输入并保存

MySQL / MariaDB:如何最好地确保可变大小的BLOB是唯一的

如何最好地序列化java.awt.Image?

将位图转换为java.awt.image.BufferedImage

硒:java.awt.image.RasterFormatException:(Y +高度)是使用元素的大小和位置时光栅的外

MySQL Store Image BLOB不良实践性能

通过php将映像镜像到mysql blob

使用齐射显示从MySQL到Android的BLOB图像

发送和接收PNGencoded到php mysql BLOB

将<svg> <image>移动到Blob图片中

上载的图片未显示在mysql表PHP BLOB中

图片发布和mysql BLOB变量出现问题

在MySQL中将BLOB图像从Mysql数据库显示到动态div中

您如何确定图像(java.awt.Image)是否为黑白且没有颜色?

java.awt.image.BufferedImage.getRBG不返回期望值

如何在Android Studio中导入java.awt.image.BufferedImage

什么是java.awt.image.BufferedImage对于屏幕外渲染最有用的类型?

在调用java.awt.image.RenderedImage类的getWidth()方法时获取java.security.AccessControlException

可以使用Java.awt.Image android应用程序