Java converts images to Base64 and Base64 into image encoding and save locally; and Android to Base64 coded __

Source: Internet
Author: User
Tags base64

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);
    }


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.