添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to store an image uploaded by the user into the database with a LONGBLOB attribute... I ran into a problem with a PreparedStatement that has two methods to set a blob which are:

public void setBinaryStream(int parameterIndex, InputStream x)

public void setBlob(int parameterIndex, Blob x)

public void setBlob(int parameterIndex, InputStream inputStream)

Now the problem is I have a BufferedImage object which must be converted into Blob or InputStream to upload...

How can I do this without losing the original image format or quality?

You need to save it using some appropriate (lossless) format. The png format is one option.

The ImageIO class has methods for writing out an image to an output stream.

Here's a complete example of how you get hold of an InputStream from which you can read the PNG-representation of the content of a BufferedImage :

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
                Thanks Csujo and @aioobe for quick answers....but in both cases i have to manage also the incoming format of image...is there no other way to do the same thing without that format conversion ??
– Asif
                Oct 4, 2011 at 8:49
                Then use a FileInputStream to save the original byte array along side with the buffered image. To the extent of my knowledge, there's no way of "recovering" the exact sequence of bytes that was once used to generate the content of a BufferedImage.
– aioobe
                Oct 4, 2011 at 9:04
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image,"jpg", bas);
byte[] bytes = bas.toByteArray();
InputStream is = new ByteArrayInputStream(bytes);

Don't forget to check the second ImageIO.write parameter to your required image format (e.g. "jpg", "bmp", "png").

Write functions to serialize/unserialize the BufferedImage to/from a byte [] and use classes ByteArrayInputStream and ByteArrayOutputStream.

You probably need to serialize the image type, width, height and the image pixel values (obtained with BufferedImage.getRGB(x, y)).

The alternative is to call ImageIO.write(image, "png", outStream) to convert the image to PNG format and write it to a ByteArrayOutputStream. There is an equivalent ImageIO.read(inputStream) method to read the image from a ByteArrayInputStream. There is some processing overhead in converting to/from PNG. But PNG is a compressed image format and you will save a lot of space compared with storing the images uncompressed.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
Blob blFile = new javax.sql.rowset.serial.SerialBlob(baos.toByteArray());
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.