Wang Liping-Base64 encoded the image in Android,-androidbase64

Source: Internet
Author: User

Wang Liping-Base64 encoded the image in Android,-androidbase64

  1. // ------------------ Base64 -------------------//
    Public String bitmaptoString (Bitmap bitmap ){


    // Convert Bitmap into a string


    String string = null;


    ByteArrayOutputStream bStream = new ByteArrayOutputStream ();


    Bitmap. compress (CompressFormat. PNG, 100, bStream );


    Byte [] bytes = bStream. toByteArray ();


    String = Base64.encodeToString (bytes, Base64.DEFAULT );


    Return string;


    }


    // -------------- Base64 -----------------//
    Public Bitmap stringtoBitmap (String string ){


    // Convert the string to the Bitmap type


    Bitmap bitmap = null;


    Try {


    Byte [] bitmapArray;


    BitmapArray = Base64.decode (string, Base64.DEFAULT );


    Bitmap = BitmapFactory. decodeByteArray (bitmapArray, 0,


    BitmapArray. length );


    } Catch (Exception e ){


    E. printStackTrace ();


    }


    Return bitmap;


    }

How to perform base64 Decoding

The encoding rules:
①. 3 characters into 4 characters ..
② Add a line break for every 76 characters ..
③. The final Terminator must also be processed ..
For 1 is 4 characters 32 characters .. it has characters .. 24 bits... there are eight differences .. these 8 digits .. it is evenly distributed to two bytes in height .. 2*4 = 8....

The following is my implementation using C ++... with comments... implemented through the shift operation... friends who are not familiar with the shift operation may be a little difficult...

Void basedid (char * string, char * pch)
{
Int I = 0;
BYTE b1, b2, b3;

CAtlStringA str (string );
Int len = str. GetLength ();

Char B;
Do
{
//

B1 = string [I ++];

// B1 shifts two places to the right... the height is two places clear 0...
// B1. the remaining 2 characters must be placed in the 2nd characters ..
B = BASE64TABLE [b1> 2];
// Get 1st characters ..
* Pch ++ = B;
// Determine if the new line is to be wrapped. The 76 characters should be wrapped. Put it here. Avoid the new line when I = 1 ..
If (I % 76 = 0)
{
* Pch ++ = '\ R ';
* Pch ++ = '\ n ';
}

B2 = string [I ++];

// (The last two digits of b1 should be placed in the 6th B2. (the first two digits must be filled with 0) b1. it must be shifted to four places left... b2 shifts four places to the right... then add .. clear the maximum 2 digits 0 ..
// B2. The remaining 4 characters must be placed in the 3rd characters...
B = BASE64TABLE [(b1 <4) + (b2> 4) & (0x3f)];
// Get 2nd characters ..
* Pch ++ = B;

// If there are no 3rd characters, the loop will be stopped ..
/// Here cannot be> =... because the last 0x00 Terminator must be processed. len does not contain the final Terminator...
If (I> len)
Break; // ①

B3 = (BYTE) string [I ++];

// The four bits of b2 must be placed at the 3rd-bit length of 6th characters... b2. move two places left... b3 needs to shift 6 places to the right .. the top two digits are placed in the last two digits of the character ..
// B3 is left with 6 characters...
B = BASE64TABLE [(b2 <2) + (b3> 6) & (0x3f) ...... remaining full text>

Java uses base64 encoding for network images

String s = new sun. misc. BASE64Encoder (). encode (url. getByte ());

Related Article

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.