public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
上面的代码是将图像转换为base64 字符串,但我想将图像转换为base64 二进制,而base64二进制的转换输出将看起来像“[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6,7 ,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26,31 ,30,29,26,28,28,32,36,46,39,32,34,44,35,28,28,40,55,41,44,48,49,52,52,52,31 ,39,57,61,56,50,60,46,51,52,50,255,219,0,67,1,9,9,9,12,11,12,24,13,13,24,50,33 ,28,33,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50 ,. ................,253,246,255,0,190,141,20,80,2,249,142,57,222,223,153,160,200,231,146,237,159,173,20,80,1,230,63,247,219,254,250,52,155,223,251,237,255,0,125,26,40,160,4,103,102,108,177,44,79 ,82,78,73,162,138,40,3,255,217]“ ..任何人都可以帮助我吗?提前谢谢。
答案 0 :(得分:1)
在第一步中,您将其编码为Base64。获取二进制数据是将其解码回来。 :)你可以像下面这样做
byte[] byteArr = Base64.decode("Pass BASE64 String here", Base64.DEFAULT);
答案 1 :(得分:0)
看到这个答案
Java - Convert image to Base64
在这篇文章中,用户将数据写入文件,然后使用Apache Commons IOUtils从那里读取数据:
Base64.encode(FileUtils.readFileToByteArray(file));
还指定了数组的限制来捕获编码的字节数组。
byte[] byteArray = new byte[102400];
base64String = Base64.encode(byteArray);
答案 2 :(得分:0)
Bitmap to Base64 (Java)
private String bitmapToBase64(Bitmap bitmap)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}