Tuesday, January 6, 2009

Convert a jpg image to base64 encoding

Below is a snippet of code in java for converting a jpg image to base64.

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.imageio.ImageIO;


public class ImageEncode {

/**
* @param args
*/
public static String image;

public static void main(String[] args) {
try {
File f = new File("c:\\temp\\index.jpg");
BufferedImage _image = ImageIO.read(f);

if (_image != null) {
try {
java.io.ByteArrayOutputStream os= new java.io.ByteArrayOutputStream();
ImageIO.write(_image, "jpg", os);
byte[] data= os.toByteArray();
image = new sun.misc.BASE64Encoder().encode(data);

//write to file the encoded character
FileOutputStream fout=null;
OutputStreamWriter out = null;
OutputStream bout = null;
fout = new FileOutputStream ("c:\\temp\\encodedImage.txt");
bout= new BufferedOutputStream(fout);
out = new OutputStreamWriter(bout, "utf-8");
out.write(image);
out.close();

} catch (java.lang.Exception ex2) {
System.out.println("Exception: " + ex2);
ex2.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}

Converting a base64 encoded image back to jpg file

Below is a small snippet of java code for converting a base64 encoded image back to jpg format. For the smooth running of the program I assume that the base64 encoded image is stored in a .txt file and the program will read the file and create jpg image from it.

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;


public class ImageDecode {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {

StringBuffer Line= new StringBuffer();

FileInputStream fstream = new FileInputStream("c:\\temp\\test2.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
Line.append(strLine);
}
//Close the input stream
in.close();


String base64 = Line.toString();
System.out.println(base64);
byte decoded[] = new sun.misc.BASE64Decoder().decodeBuffer(base64);
FileOutputStream fos = null;
fos = new FileOutputStream("c:\\temp\\test212.jpg");
fos.write(decoded);
fos.close();

} catch (java.lang.Exception ex2) {
System.out.println("Exception: " + ex2);
ex2.printStackTrace();
}

}

}

Note: create the respective folders before you test the program

Good Blog

Visit a good Blog

Handling unicode characters while opening a csv file in MS Excel

Eventhough if we write the unicode characters using utf-8 in csv file , the unicode characters will write properly in .csv file and it will display properly when we open the file using notepad.

The problem will occur when we open the .csv file using Excel. The problem is unicode characters will not appear properly and the values will get strangled when openeing in Excel.The reason is excel will support only unicode charactesr using UNICODE endocing and not using UTF-8. For doing this please follow the below given steps.

This will help when writing Chinese , Japanese characters in csv file and opening it using Excel.

First when writing entries to the file, use TAB '\t' to separate entities instead of the default COMMA ',' separation.
The file encodng should be UNICODE instead of UTF-8

Sample code snippets in Java. (The same concept can be used for other programming languages also)

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\temp" +"\\"+"test.csv"), "UNICODE"));
;
;
//writing the values to csv file
strTextToWrite = "value1" + "\t";
for (int j=0;j<10;> j++)
{
strTextToWrite += "value"+j +"\t";
}
;
;
out.write(strTextToWrite + "\n");
;
;
out.close();