Created
August 1, 2014 07:30
-
-
Save pgbytes/c811f178ed0b524c0cb0 to your computer and use it in GitHub Desktop.
Convert java.awt.Image to byte array #java #images
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 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")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment