Skip to content

Instantly share code, notes, and snippets.

@nasacj
Last active April 9, 2017 07:52
Show Gist options
  • Save nasacj/de21a7ce0978a1d753631e93a42b60ec to your computer and use it in GitHub Desktop.
Save nasacj/de21a7ce0978a1d753631e93a42b60ec to your computer and use it in GitHub Desktop.
Read BufferedImage From Byte
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