Wednesday, November 23, 2016

Convert Base64 encoded string to Image using Java

This program will help you convert a base64 string to an image and then save the image to the current directory using Java.

Language Used:
Java

Git Repo:
https://github.com/csanuragjain/extra/tree/master/Base64ToImage

Pom Dependency:
 <dependency>  
   <groupId>commons-codec</groupId>  
   <artifactId>commons-codec</artifactId>  
   <version>1.10</version>  
 </dependency>  

Program:

Main Method:
      public static void main(String[] args) throws IOException {  
           // TODO Auto-generated method stub  
           Scanner s=new Scanner(System.in);  
           System.out.println("Enter base64 string to be converted to image");  
           String base64=s.nextLine();  
           byte[] base64Val=convertToImg(base64);  
           writeByteToImageFile(base64Val, "image.png");  
           System.out.println("Saved the base64 as image in current directory with name image.png");  
      }  

How it works:
1) First we make a scanner object and ask user to enter the base64 string which need to be converted to image.
2) We call the convertToImg method which convert the base64 string to byte[]
3) We pass the above retrieved byte[] as argument to writeByteToImageFile method. This will convert the byte[] to image and will save as image.png which was the second argument.

convertToImg method:
 public static byte[] convertToImg(String base64) throws IOException  
      {  
           return Base64.decodeBase64(base64);  
      }  

How it works:
1) We pass the base64 string to Base64.decodeBase64 method which converts the base64 string to byte[]

writeByteToImageFile method:
 public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException  
      {  
           File imgFile = new File(imgFileName);  
           BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));  
           ImageIO.write(img, "png", imgFile);  
      }  

How it works:
1) This method obtains the byte[] and the image file name
2) We make a file object pointing to image file name
3) We make a BufferedImage object from the image bytes we passed
4) We use the ImageIO.write to write the BufferedImage into a physical file.

Output:
 Enter base64 string to be converted to image  
 /9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQPEBQQEBQVFRQUFRUUFA8UFRQUFBUUFBQWFhUUFRQYHCggGBolHRQVITEiJikrLjAuFx8zODMtNygtLisBCgoKDg0OGhAQGiwkHyQsLC0tLCwsLCwsLCwsLCwsLCwtLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIALI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JK4sS5uxIU2QZeF4o1oLZbb2s7HBrnA2bcOyDrd6cw7TY1caTUReOpOmjizY2pWjKw2Pa92Z1jk1p5DmT4n6AeJXNqtSpvlj0N2HE47vqbJjwdQVyWbioJVglCSCVNEBTQASgVUnKRYypyiwWpyiyKUcosmk5RYpOUWKTlFlhYhAIGVKQVNQkqUklxqyRiyoKSAsSUEYKHKCSgqCAoJCzRDCxYCEgqWCkqAUlAW3KB3Ndi4xroPkFiyTVSDVYkkIQS1AbTg57RHhssog3DVmupBUjJClEEKQVBEGVtUmJKAkIClSCUAUkhDE//9k=  
 Saved the base64 as image in current directory with name image.png  

Full Program:
 package com.cooltrickshome;  
 import java.awt.image.BufferedImage;  
 import java.io.ByteArrayInputStream;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.Scanner;  
 import javax.imageio.ImageIO;  
 import org.apache.commons.codec.binary.Base64;  
 public class Base64ToImage {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) throws IOException {  
           // TODO Auto-generated method stub  
           Scanner s=new Scanner(System.in);  
           System.out.println("Enter base64 string to be converted to image");  
           String base64=s.nextLine();  
           byte[] base64Val=convertToImg(base64);  
           writeByteToImageFile(base64Val, "image.png");  
           System.out.println("Saved the base64 as image in current directory with name image.png");  
      }  
      public static byte[] convertToImg(String base64) throws IOException  
      {  
           return Base64.decodeBase64(base64);  
      }  
      public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException  
      {  
           File imgFile = new File(imgFileName);  
           BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));  
           ImageIO.write(img, "png", imgFile);  
      }  
 }  

Hope it helps :)

2 comments: