Convert image to string java

Convert image to string java

Below is demo java program where we will read image from local folder and convert it to string and once we convert image to string we will convert it back byte array and write back to the file to validate it.

We will use 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.

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 ConvertImageToString {

    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 byte representation of string 
        //to write back to the file
        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 image to string java

 

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

That’s it convert image to string java completed.

 

Leave a Reply

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