Convert a Base64 encoded image data back to an image

  #
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import javax.imageio.ImageIO; import sun.misc.BASE64Decoder; /** * Input : Base64 encoded Image * Outpu : Save it as image file (png) */ public class Base64ImageTest { public static void main(String args[]); { // this is not the real stream just for example String base64String = "BORw0KGgoAAAANSUhEUgAAAUAAAAHgCAYAAADUjLREAAAgAElEQVR4AexdB4BU1dX+ZmZ7ZWGX3pHeu6goitgQDCZGjdHYu4nGqL81mmaJvdd"; // decode base64 encoded image BASE64Decoder decoder = new BASE64Decoder(); byte[] decodedBytes = decoder.decodeBuffer(base64String); log.debug("Decoded upload data : " + decodedBytes.length); String uploadFile = "/tmp/test.png"; log.debug("File save path : " + uploadFile); // buffered image from the decoded bytes BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedBytes)); if (image == null) { log.error("Buffered Image is null"); } File f = new File(uploadFile); // write the image ImageIO.write(image, "png", f); } }

Also See



Loading Please Wait...