DCoder DCoder - 1 year ago 103
Java Question

Write Base64 encoded image to file

How to write Base64 encoded image to file?

I have encoded image into string using Base64 for that, First I read the file then convert it into byte array then apply Base64 encoding to store the image string.
Now my problem is how to decode it.

byte dearr[] = Base64.decodeBase64(crntImage);
File outF = new File("c:/decode/abc.bmp");
BufferedImage img02 = ImageIO.write(img02, "bmp", outF);

is a string variable which contain string representation of image.

Answer Source

Assuming the image data is already in the format you want, you don't need image ImageIO at all - you just need to write the data to the file:

// Note preferred way of declaring an array variable
byte[] data = Base64.decodeBase64(crntImage);
try (OutputStream stream = new FileOutputStream("c:/decode/abc.bmp")) {

(I'm assuming you're using Java 7 here - if not, you'll need to write a manual try/finally statement to close the stream.)

If the image data isn't in the format you want, you'll need to give more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download