Java picture converted to base64 format

Source: Internet
Author: User
Tags base64

/** * @Descriptionmap Convert a picture file into a byte array string and encode it Base64 * @author temdy * @Date 2015-01-26 * @param path picture path * @return */pu    Blic static string ImageToBase64 (string path) {//Converts the picture file to a byte array string and BASE64 encodes it byte[] data = null;        Reads a picture byte array try {InputStream in = new FileInputStream (path);        data = new byte[in.available ()];        In.read (data);    In.close ();    } catch (IOException e) {e.printstacktrace ();    }//byte array Base64 encoded Base64encoder encoder = new Base64encoder (); return Encoder.encode (data);//returns BASE64 encoded byte array string}/** * @Descriptionmap Base64 decode byte array string and generate picture * @author Temdy * @Date 2015-01-26 * @param base64 picture Base64 Data * @param path picture path * @return */public static Boolean base64toimage (String base64, St    Ring path) {//Base64 decoding the byte array string and generating a picture if (base64 = = null) {//image data is empty return false;    } Base64decoder decoder = new Base64decoder ();        try {//Base64 decode byte[] bytes = Decoder.decodebuffer (base64); For (int i = 0; i < bytes.length;            ++i) {if (Bytes[i] < 0) {//Adjust exception data Bytes[i] + = 256;        }}//generate JPEG picture outputstream out = new FileOutputStream (path);        Out.write (bytes);        Out.flush ();        Out.close ();    return true;    } catch (Exception e) {return false; }}

Java picture converted to base64 format

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.