import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author YNZ */ public class Image2Array { public static void main(String[] args) { byte[] imageArray = null; BufferedImage image = null; File file = new File("./src/tmp", "hit.jpg"); try { image = ImageIO.read(file); try (ByteArrayOutputStream baos = new ByteArrayOutputStream(1024)) { System.out.println("write to buffer" + ImageIO.write(image, "jpg", baos)); imageArray = baos.toByteArray(); System.out.println("size of array " + imageArray.length); baos.flush(); } } catch (IOException ex) { Logger.getLogger(Image2Array.class.getName()).log(Level.SEVERE, null, ex); } } }
Wednesday, 23 August 2017
How to convert an image into byte array
Sometime we need to convert an image into byte array in order to persist it into database.
Subscribe to:
Post Comments (Atom)
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
Could not extract response: no suitable HttpMessageConverter found for response type [class dk.enettet.evu.core.model.Address] and content ...
-
First time met this hibernate exception. I think this issue should due to one to one relationship. One driver has one car; one car has on...
-
A large object refers to the entity property that is modified by @Lob. It may be persisted in several records. However, in database manage...
No comments:
Post a Comment