Home  >  Article  >  Database  >  How to Convert a BufferedInputStream from a Database to a BufferedImage?

How to Convert a BufferedInputStream from a Database to a BufferedImage?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-13 09:47:02149browse

How to Convert a BufferedInputStream from a Database to a BufferedImage?

Converting BufferedInputStream to an Image

Problem:

How can I convert a BufferedInputStream object, retrieved from a database, into a BufferedImage? The existing code returns a null image.

Solution:

  1. Verify that the BufferedInputStream contains a valid image. Save it to a file and then read it back as a BufferedImage to confirm.
  2. Ensure that the correct length of the Blob is used when getting the bytes. For H2 database, the length is returned as a long, but Blob#getBytes expects an int.
  3. Alternatively, use Blob#getBinaryStream instead of Blob#getBytes. This may be necessary if the database does not store Blob contents in memory.

Example:

The following Java code uses Blob#getBinaryStream to read an image from the database and convert it to a BufferedImage:

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.imageio.ImageIO;

public class ImageDatabaseHelper {

    private Connection con;

    public BufferedImage loadImage(int imageId) throws IOException, SQLException {

        PreparedStatement stmt = null;
        ResultSet rs = null;
        BufferedImage image = null;

        try {

            stmt = con.prepareStatement("select image from images where id = ?");
            stmt.setInt(1, imageId);
            rs = stmt.executeQuery();

            while (rs.next()) {

                Blob blob = rs.getBlob(1);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try (ByteArrayInputStream bais = new ByteArrayInputStream(blob.getBinaryStream())) {
                    ImageIO.read(bais).writeTo(baos);
                }
                image = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
            }

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                stmt.close();
            } catch (Exception e) {
            }
        }

        return image;
    }

}

The above is the detailed content of How to Convert a BufferedInputStream from a Database to a BufferedImage?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn