简体   繁体   English

Android Base64编码

[英]Android Base64 encoding

public String encodeTobase64(Bitmap image){
    System.gc();  //For memory efficiency
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = null;
     imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

Can I split the byte array into multiple sections and then run encoding on them on different threads so that it could be fast for larger images and then join them at last. 我可以将字节数组拆分为多个部分,然后在不同的线程上对它们进行编码,这样对于较大的图像来说可能很快,然后最后将它们加入。

If you process your bytes by a multiple of 3 at a time it actually is possible as shown in this post . 如果你通过一次它实际上是可能的,如图3的倍数处理您的字节这个职位

Otherwise that will not work because of the way the Base64 algorithm works. 否则,由于Base64算法的工作方式而无法正常工作。 When you split the array you might have more or less padding per segment so that when you joined the String together in the end and tried to do the reverse operation (back to a byte[] ) it would likely fail. 拆分数组时,每个段可能具有或多或少的填充,因此,当最后将String连接在一起并尝试执行反向操作(返回byte[] )时,它很可能会失败。

Here is a small example: 这是一个小例子:

String test = "This is a test";
byte[] testBytes = test.getBytes();
int mid = testBytes.length / 2;

byte[] part1 = Arrays.copyOfRange(testBytes, 0, mid);
byte[] part2 = Arrays.copyOfRange(testBytes, mid, testBytes.length);

Encoder base64Encoder = Base64.getEncoder();
System.out.println(base64Encoder.encodeToString(testBytes));
System.out.println(base64Encoder.encodeToString(part1));
System.out.println(base64Encoder.encodeToString(part2));

This will output: 这将输出:

VGhpcyBpcyBhIHRlc3Q=
VGhpcyBpcw==
IGEgdGVzdA==

Notice if you put the second two Strings together they do not equal the first. 请注意,如果将第二个两个Strings放在一起,则它们不等于第一个。 The = characters are the padding. =字符是填充。

If you are worried about the efficiency of Base64 encoding an image you can do the operation on a single background thread. 如果您担心使用Base64编码图像的效率,可以在单个后台线程上执行操作。 You can use an AsyncTask as one option to help you to do so. 您可以使用AsyncTask作为一种选择来帮助您做到这一点。

For encoding base64 partially, first you need to understand how base64 encoding work. 要部分编码base64,首先需要了解base64编码是如何工作的。 Check below URL for it. 检查下面的URL。

  1. http://www.hcidata.info/base64.htm http://www.hcidata.info/base64.htm

  2. https://blogs.oracle.com/rammenon/entry/base64_explained https://blogs.oracle.com/rammenon/entry/base64_explained

For eg in case of string. 例如在字符串的情况下。 You just need to divide the array of string into the arrays of size 3 for example if you want to convert "12345678aa" in base64 then 您只需要将字符串数组划分为大小为3的数组,例如,如果要在base64中转换“ 12345678aa”,则

convert in below sequence 按以下顺序转换

  1. 123 - MTIz 123-MTIz
  2. 456 - NDU2 456-NDU2
  3. 78a - Nzhh 78a-Nzhh
  4. a - YQ== 一个-YQ ==

Now just merge you result. 现在只合并您的结果。 Which will become "MTIzNDU2NzhhYQ==". 它将变成“ MTIzNDU2NzhhYQ ==“。 Which is the encoded value of "12345678aa". 这是“ 12345678aa”的编码值。

Chears................. :) Chears ................. :)

You forgot to decode bitmap before converting it to byte array. 您忘记解码位图,然后再将其转换为字节数组。

add the following line 添加以下行

 Bitmap bm = BitmapFactory.decodeFile(image);

after

System.gc();  //For memory efficiency

So your code will be as follows: 因此,您的代码将如下所示:

    public String encodeTobase64(Bitmap image){

    System.gc();  //For memory efficiency
    Bitmap bm = BitmapFactory.decodeFile(image);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray(); 
    String imageEncoded = null;
    imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM