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 ...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
As the name implies, an anonymous inner class isn’t defined using an explicit name. An anonymous inner class is created when you combine ins...
No comments:
Post a Comment