Convert string to image java

Convert string to image java

Below is program which will convert string to image java .

We are using Apache commons codec library to convert image to Base64 string so we need commons-codec-1.9.jar or latest version and this you could directly download from Apache site here or also download from bottom of the page download link.

Steps are below:

  • Read image from local system or from the network
  • Convert it to byte array
  • Convert byte array to Base64 string
  • Convert Base64 string to byte array and write back to the file
package com.javahonk;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;



public class ConvertStringToImage {

    public static void main(String[] args) {

        //Convert image to string
        System.out.println("Convert image to string");
        String imageString = convertImageToString();
        System.out.println("String representation of image:"
                +imageString);
        System.out.println("Convert image to string done");
        
        //Convert string to image
        System.out.println("Convert String to image");
        convertStringToImageByteArray(imageString);
        System.out.println("Convert String to image done");

    }
    
    private static String convertImageToString(){
        
        InputStream inputStream = null;
        
        String imageString = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {

            inputStream = new FileInputStream("resources"
                    + "\\Spring.png");

            byte[] buffer = new byte[1024];
            baos = new ByteArrayOutputStream();

            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }

            byte[] imageBytes = baos.toByteArray();

            imageString = Base64.encodeBase64String(imageBytes);
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                baos.close();
                inputStream.close();                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        return imageString;
    }
    
    private static void convertStringToImageByteArray(String 
            imageString){
        
        OutputStream outputStream = null;
        byte [] imageInByteArray = Base64.decodeBase64(
                imageString);
        try {
            outputStream = new FileOutputStream("resources"
                    + "\\Spring2.png");
            outputStream.write(imageInByteArray);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }


}

Output:

Convert string to image java

 

Spring jdbctemplate tutorial Download commons-codec-1.9.jarcommons-codec-1.9

That’s it convert string to image java completed.

 

Leave a Reply

Your email address will not be published. Required fields are marked *