Java Processing Picture base64 encoding for mutual conversion

Source: Internet
Author: User
Tags base64

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.