How to use:
The following IMG is taken over by Base64 encoded pictures
To store this picture.
String img=request.getparameter ("img");
System.out.print (img+ "= =");
Typeconverter.generateimage (IMG);
public class TypeConverter {//Picture into Base64 string public static string Getimagestr (string path) {//Convert picture file to byte array string, and
It is BASE64 encoded to handle String Imgfile = path;//The picture to be processed inputstream in = null;
byte[] data = null;
Reads the picture byte array try {in = new FileInputStream (imgfile);
data = new byte[in.available ()];
In.read (data);
In.close ();
catch (IOException e) {e.printstacktrace ();
///To byte array Base64 encoding Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string}//base64 string into a picture public static Boolean generateimage (Strin
G imgstr) {System.out.print ("has received the method of converting bytecode into pictures");
Base64 decodes a byte array string and generates a picture if (imgstr = = null)//image data is null return false;
Base64decoder decoder = new Base64decoder (); try {//base64 decoding byte[] B = Decoder.decodebuffer (IMGSTR); for (int i=0;i<b.length;++i) {if (b[i]<0) {//adjust exception data b
[i]+=256;
}///generate JPEG picture String imagepath= "C:";
System.currenttimemillis () String Imgfilepath = "c:\\inetpub\\wwwroot\\school_mart.jpg";//newly generated picture
OutputStream out = new FileOutputStream (Imgfilepath);
Out.write (b);
Out.flush ();
Out.close ();
return true;
catch (Exception e) {return false;
}
}
}
Android-side code: converting Bitmap to Base64
/**
* Converts bitmap to Base64 string
* @param bit
* @return
/Public
string bitmap2strbybase64 by Base32 ( Bitmap bit) {
bytearrayoutputstream bos=new bytearrayoutputstream ();
Bit.compress (Bitmap.CompressFormat.JPEG, BOS),//parameter 100 means uncompressed
byte[] Bytes=bos.tobytearray ();
Return base64.encodetostring (bytes, base64.default);
}