Converting an image from a data stream to a bytes type facilitates base64 conversion for Network Transmission

Source: Internet
Author: User
Tags modulus

Android cannot directly use the base64 class in Java. You need to define and compile an encoding class to transfer photos over the network. The following describes how to build an encoding type, it facilitates network transmission.

 

 

Construct a base64 class:

Public class base64 {
 
Private Static final byte [] encodingtable = {(byte) 'A', (byte) 'B ',
(Byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G ',
(Byte) 'h', (byte) 'I', (byte) 'J', (byte) 'k', (byte) 'l ',
(Byte) 'M', (byte) 'n', (byte) 'O', (byte) 'P', (byte) 'Q ',
(Byte) 'R', (byte)'s ', (byte) 'T', (byte) 'U', (byte) 'V ',
(Byte) 'w', (byte) 'x', (byte) 'y', (byte) 'Z', (byte) 'A ',
(Byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F ',
(Byte) 'G', (byte) 'h', (byte) 'I', (byte) 'J', (byte) 'k ',
(Byte) 'l', (byte) 'M', (byte) 'n', (byte) 'O', (byte) 'P ',
(Byte) 'Q', (byte) 'R', (byte)'s ', (byte) 'T', (byte) 'U ',
(Byte) 'V', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'Z ',
(Byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4 ',
(Byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9 ',
(Byte) '+', (byte )'/'};

Private Static final byte [] decodingtable;

Static {
Decodingtable = new byte [128];
For (INT I = 0; I <128; I ++ ){
Decodingtable [I] = (byte)-1;
}
For (INT I = 'a'; I <= 'Z'; I ++ ){
Decodingtable [I] = (byte) (I-'A ');
}
For (INT I = 'a'; I <= 'Z'; I ++ ){
Decodingtable [I] = (byte) (I-'A' + 26 );
}
For (INT I = '0'; I <= '9'; I ++ ){
Decodingtable [I] = (byte) (I-'0' + 52 );
}
Decodingtable ['+'] = 62;
Decodingtable ['/'] = 63;
}

Public static byte [] encode (byte [] data ){
Byte [] bytes;
Int modulus = data. Length % 3;
If (modulus = 0 ){
Bytes = new byte [(4 * Data. Length)/3];
} Else {
Bytes = new byte [4 * (data. Length/3) + 1)];
}
Int datalength = (data. Length-modulus );
Int A1;
Int A2;
Int A3;
For (INT I = 0, j = 0; I <datalength; I + = 3, J + = 4 ){
A1 = data [I] & 0xff;
A2 = data [I + 1] & 0xff;
A3 = data [I + 2] & 0xff;
Bytes [J] = encodingtable [(A1 >>> 2) & 0x3f];
Bytes [J + 1] = encodingtable [(A1 <4) | (A2 >>> 4) & 0x3f];
Bytes [J + 2] = encodingtable [(A2 <2) | (A3 >>> 6) & 0x3f];
Bytes [J + 3] = encodingtable [A3 & 0x3f];
}
Int B1;
Int B2;
Int B3;
Int D1;
Int D2;
Switch (modulus ){
Case 0:/* nothing left to do */
Break;
Case 1:
D1 = data [data. Length-1] & 0xff;
B1 = (d1 >>> 2) & 0x3f;
B2 = (d1 <4) & 0x3f;
Bytes [bytes. Length-4] = encodingtable [B1];
Bytes [bytes. Length-3] = encodingtable [B2];
Bytes [bytes. Length-2] = (byte) '= ';
Bytes [bytes. Length-1] = (byte) '= ';
Break;
Case 2:
D1 = data [data. Length-2] & 0xff;
D2 = data [data. Length-1] & 0xff;
B1 = (d1 >>> 2) & 0x3f;
B2 = (d1 <4) | (D2 >>> 4) & 0x3f;
B3 = (D2 <2) & 0x3f;
Bytes [bytes. Length-4] = encodingtable [B1];
Bytes [bytes. Length-3] = encodingtable [B2];
Bytes [bytes. Length-2] = encodingtable [B3];
Bytes [bytes. Length-1] = (byte) '= ';
Break;
}
Return bytes;
}

Public static byte [] Decode (byte [] data ){
Byte [] bytes;
Byte B1;
Byte B2;
Byte B3;
Byte B4;
Data = discardnonbase64bytes (data );
If (data [data. Length-2] = '){
Bytes = new byte [(data. Length/4)-1) * 3) + 1];
} Else if (data [data. Length-1] = '){
Bytes = new byte [(data. Length/4)-1) * 3) + 2];
} Else {
Bytes = new byte [(data. Length/4) * 3)];
}
For (INT I = 0, j = 0; I <(data. Length-4); I + = 4, J + = 3 ){
B1 = decodingtable [DATA [I];
B2 = decodingtable [DATA [I + 1];
B3 = decodingtable [DATA [I + 2];
B4 = decodingtable [DATA [I + 3];
Bytes [J] = (byte) (b1 <2) | (B2> 4 ));
Bytes [J + 1] = (byte) (b2 <4) | (B3> 2 ));
Bytes [J + 2] = (byte) (B3 <6) | B4 );
}
If (data [data. Length-2] = '){
B1 = decodingtable [DATA [data. Length-4];
B2 = decodingtable [DATA [data. Length-3];
Bytes [bytes. Length-1] = (byte) (b1 <2) | (B2> 4 ));
} Else if (data [data. Length-1] = '){
B1 = decodingtable [DATA [data. Length-4];
B2 = decodingtable [DATA [data. Length-3];
B3 = decodingtable [DATA [data. Length-2];
Bytes [bytes. Length-2] = (byte) (b1 <2) | (B2> 4 ));
Bytes [bytes. Length-1] = (byte) (b2 <4) | (B3> 2 ));
} Else {
B1 = decodingtable [DATA [data. Length-4];
B2 = decodingtable [DATA [data. Length-3];
B3 = decodingtable [DATA [data. Length-2];
B4 = decodingtable [DATA [data. Length-1];
Bytes [bytes. Length-3] = (byte) (b1 <2) | (B2> 4 ));
Bytes [bytes. Length-2] = (byte) (b2 <4) | (B3> 2 ));
Bytes [bytes. Length-1] = (byte) (B3 <6) | B4 );
}
Return bytes;
}

Public static byte [] Decode (string data ){
Byte [] bytes;
Byte B1;
Byte B2;
Byte B3;
Byte B4;
Data = discardnonbase64chars (data );
If (data. charat (data. Length ()-2) = '){
Bytes = new byte [(data. Length ()/4)-1) * 3) + 1];
} Else if (data. charat (data. Length ()-1) = '){
Bytes = new byte [(data. Length ()/4)-1) * 3) + 2];
} Else {
Bytes = new byte [(data. Length ()/4) * 3)];
}
For (INT I = 0, j = 0; I <(data. Length ()-4); I + = 4, J + = 3 ){
B1 = decodingtable [data. charat (I)];
B2 = decodingtable [data. charat (I + 1)];
B3 = decodingtable [data. charat (I + 2)];
B4 = decodingtable [data. charat (I + 3)];
Bytes [J] = (byte) (b1 <2) | (B2> 4 ));
Bytes [J + 1] = (byte) (b2 <4) | (B3> 2 ));
Bytes [J + 2] = (byte) (B3 <6) | B4 );
}
If (data. charat (data. Length ()-2) = '){
B1 = decodingtable [data. charat (data. Length ()-4)];
B2 = decodingtable [data. charat (data. Length ()-3)];
Bytes [bytes. Length-1] = (byte) (b1 <2) | (B2> 4 ));
} Else if (data. charat (data. Length ()-1) = '){
B1 = decodingtable [data. charat (data. Length ()-4)];
B2 = decodingtable [data. charat (data. Length ()-3)];
B3 = decodingtable [data. charat (data. Length ()-2)];
Bytes [bytes. Length-2] = (byte) (b1 <2) | (B2> 4 ));
Bytes [bytes. Length-1] = (byte) (b2 <4) | (B3> 2 ));
} Else {
B1 = decodingtable [data. charat (data. Length ()-4)];
B2 = decodingtable [data. charat (data. Length ()-3)];
B3 = decodingtable [data. charat (data. Length ()-2)];
B4 = decodingtable [data. charat (data. Length ()-1)];
Bytes [bytes. Length-3] = (byte) (b1 <2) | (B2> 4 ));
Bytes [bytes. Length-2] = (byte) (b2 <4) | (B3> 2 ));
Bytes [bytes. Length-1] = (byte) (B3 <6) | B4 );
}
Return bytes;
}

Private Static byte [] discardnonbase64bytes (byte [] data ){
Byte [] temp = new byte [data. Length];
Int bytescopied = 0;
For (INT I = 0; I <data. length; I ++ ){
If (isvalidbase64byte (data [I]) {
Temp [bytescopied ++] = data [I];
}
}
Byte [] newdata = new byte [bytescopied];
System. arraycopy (temp, 0, newdata, 0, bytescopied );
Return newdata;
}

Private Static string discardnonbase64chars (string data ){
Stringbuffer sb = new stringbuffer ();
Int length = data. Length ();
For (INT I = 0; I <length; I ++ ){
If (isvalidbase64byte (byte) (data. charat (I )))){
SB. append (data. charat (I ));
}
}
Return sb. tostring ();
}

Private Static Boolean isvalidbase64byte (byte B ){
If (B = '){
Return true;
} Else if (B <0) | (B >=128 )){
Return false;
} Else if (decodingtable [B] =-1 ){
Return false;
}
Return true;
}
}

 

 

The conversion instance is:

Public static string readfile (File file ){
String result = "";
Fileinputstream FCM = NULL;
Byte [] xml = NULL;
Try {
If (file! = NULL ){
FS = new fileinputstream (File );
If (FS! = NULL ){
Int Len = FCM. Available ();
Xml = new byte [Len];
FS. Read (XML );
}
Base64 ENC = new base64 ();
Byte [] BB = enc. encode (XML );
Result = new string (BB, "UTF-8 ");
}
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (FS! = NULL ){
Try {
FCM. Close ();
FS = NULL;
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
Return result;
}

In this case, the result is the converted bytes type. You can parse the result based on the base64 of Java.

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.