by
75 5
3
4
4,112
10
Top 1% !
Famous
Tagged
Specified
Popularity: 2673rd place
This snippet is Public
LanguageJava
LicenseMIT_X11

Convert java.awt.Image to byte array

Convert java.awt.Image to byte array and back to Image object and also write to a file
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/104567/Convert-java-awt-Image-to-byte-array?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class SimpleConvertImage { public static void main(String[] args) throws IOException{ String dirName="C:\\"; ByteArrayOutputStream baos=new ByteArrayOutputStream(1000); BufferedImage img=ImageIO.read(new File(dirName,"rose.jpg")); ImageIO.write(img, "jpg", baos); baos.flush(); String base64String=Base64.encode(baos.toByteArray()); baos.close(); byte[] bytearray = Base64.decode(base64String); BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray)); ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg")); } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus