Last active
April 9, 2017 07:52
-
-
Save nasacj/de21a7ce0978a1d753631e93a42b60ec to your computer and use it in GitHub Desktop.
Read BufferedImage From Byte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import javax.imageio.ImageIO; | |
public class ReadBufferedImageFromByte { | |
public static void main(String[] args) { | |
try { | |
byte[] imageInByte; | |
BufferedImage originalImage = ImageIO.read(new File( | |
"c:/darksouls.jpg")); | |
// convert BufferedImage to byte array | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(originalImage, "jpg", baos); | |
baos.flush(); | |
imageInByte = baos.toByteArray(); | |
baos.close(); | |
// convert byte array back to BufferedImage | |
InputStream in = new ByteArrayInputStream(imageInByte); | |
BufferedImage bImageFromConvert = ImageIO.read(in); | |
ImageIO.write(bImageFromConvert, "jpg", new File( | |
"c:/new-darksouls.jpg")); | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment