Hello everyone, I'm Andy Lau.
Today's project optimizes the ability to upload avatars. Use the Imagecropper plugin to finish cropping the picture.
This plugin cut out the images are Base64 encrypted string, upload avatar also involves how to convert the encrypted string into a picture of the problem.
Here's the code:
/*** @Description: Convert base64 encoded string to Picture * @Author: * @CreateTime: *@paramIMGSTR Base64 Encoded String *@parampath picture paths-specific to file *@return*/ Public Static Booleangenerateimage (String imgstr, String path) {if(Imgstr = =NULL)return false; Base64decoder Decoder=NewBase64decoder ();Try {//decryptionbyte[] B =Decoder.decodebuffer (IMGSTR);//working with Data for(inti = 0; i < b.length; ++i) {if(B[i] < 0) {B[i]+ = 256; }} OutputStream out=NewFileOutputStream (path); Out.write (b); Out.flush (); Out.close ();return true; } Catch(Exception e) {return false; }}
Decryption naturally also has encryption. The following are the encryption:
/*** @Description: Converted to Base64 encoded string according to image address * @Author: * @CreateTime: *@return */ Public Staticstring Getimagestr (String imgfile) {InputStream InputStream=NULL; byte[] data =NULL; Try{InputStream=NewFileInputStream (Imgfile); Data=New byte[Inputstream.available ()]; Inputstream.read (data); Inputstream.close (); } Catch(IOException e) {e.printstacktrace (); } //EncryptBase64encoder encoder =NewBase64encoder (); returnEncoder.encode (data);}
The main function of a test is then pasted
/***/publicstaticvoid main (string[] args) {= Getimagestr ("f:/86619-106.jpg"); System.out.println (strimg); " F:/86619-107.jpg ");}
All right, finish the call.
However, it is important to note that the Base64 encoded string returned by the generic plug-in is prefixed with a prefix.
"Data:image/jpeg;base64," this has to be removed before decoding.
Java Processing Picture base64 encoding for mutual conversion