图像到ByteArray到BLOB和BLOB到ByteArray到Java中的图像转换问题 [英] Image to ByteArray to BLOB and BLOB to ByteArray to Image Conversion Issues in Java

查看:168
本文介绍了图像到ByteArray到BLOB和BLOB到ByteArray到Java中的图像转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的ByteArrays& amp; BLOBS,我设法编写下面的Java代码,将图像写入Access DB(使用ucanaccess)并将其写回磁盘。

After a lot of learning on ByteArrays & BLOBS, I managed to write the below Java code to write an image into Access DB (Using ucanaccess) and writing it back to Disk.

当我将图像写回磁盘图像的格式不正确或者有些东西搞砸了,你无法打开那个图像。

When I write an image back to disk the image is in incorrect format or something is messed up that you cannot open that image.

我知道在DB上存储图像不是一个好习惯但是这个仅供我学习。

I understand it is not a good practice to store Images on DB but, this is only for my learning.

public static void Update_to_DB() throws SQLException, IOException {
    String URL = "jdbc:ucanaccess://C:\\Users\\bharat.nanwani\\Desktop\\Images.accdb";
    Connection conn = DriverManager.getConnection(URL);
    //Statement stmt = conn.createStatement();
    PreparedStatement p;

    File ImgPath = new File("C:\\Users\\bharat.nanwani\\Desktop\\Desert.jpg");
    BufferedImage bufferedimage = ImageIO.read(ImgPath);

    WritableRaster raster = bufferedimage.getRaster();
    DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

    byte[] bytearray = date.getdata();


    String query = "INSERT INTO Images(data) VALUES(?);";
    p = conn.prepareStatement(query);
    p.setBinaryStream(1, new ByteArrayInputStream(bytearray),bytearray.length);
    p.execute();
}

public static void update_to_DISK() throws SQLException, IOException {
        String URL = "jdbc:ucanaccess://C:\\Users\\bharat.nanwani\\Desktop\\Images.accdb";
        Connection conn = DriverManager.getConnection(URL);
        PreparedStatement p;
        ResultSet rs;
        String query = "SELECT Data FROM Images";
                p=conn.prepareStatement(query);
        rs = p.executeQuery();
        if (rs.next()) {
        Blob blob = rs.getBlob("Data");

        byte[] bytearray = blob.getBytes(1L, (int)blob.length());

        FileOutputStream fos = new FileOutputStream("C:\\Users\\bharat.nanwani\\Desktop\\New Folder\\test.jpg");
        fos.write(bytearray);
        fos.close();

        System.out.println(bytearray);
        } 
    }


推荐答案

首先,你应该把它分成两部分:

Firstly, you should separate this into two parts:


  • 在数据库中存储二进制数据并检索它

  • 加载图像文件并再次保存

没有必要使用数据库来测试第二部分 - 你应该诊断问题通过加载图像并直接保存到文件,跳过数据库。

There's no need to use a database to test the second part - you should diagnose the issues by loading the image and saving straight to a file, skipping the database.

不,我相信问题是你从WritableRaster的数据缓冲区中复制数据,并且然后将其保存到 .jpg 文件。这不是一个jpeg - 它是 WritableRaster 使用的内部格式。

No, I believe the problem is that you're copying the data from the WritableRaster's databuffer, and then saving that to a .jpg file. It's not a jpeg at that point - it's whatever the internal format of the WritableRaster uses.

如果你想要一个jpeg文件,您根本不需要使用ImageIO - 因为您已经使用jpeg文件启动。如果要以相同的图像开始和结束,只需复制文件(或者根据您的情况将文件保存到数据库中)。这只是将文件视为字节。

If you want a jpeg file, you don't need to use ImageIO at all - because you've started off with a jpeg file. If you want to start and end with the same image, just copy the file (or save the file to the database, in your case). That's just treating the file as bytes.

如果你需要做一些事情,比如以不同的格式保存,或者以不同的大小等等,那么你应该问问ImageIO库将图像再次保存为JPEG,重新编码...然后将结果存储为文件或数据库等。

If you need to do something like saving in a different format, or at a different size, etc, then you should ask the ImageIO libraries to save the image as a JPEG again, re-encoding it... and then store the result as a file or in the database etc.

这篇关于图像到ByteArray到BLOB和BLOB到ByteArray到Java中的图像转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆