[英]compress bitmap to string android
Hi guys I have a problem when I go to encode the image to a bitmap and then do a comparison with the decode an encoded image with tools via the Internet but does not give me the same decoding and can not figure out why. 嗨,大家好,我想将图像编码为位图,然后与通过互联网使用工具对编码后的图像进行解码进行比较,但是却无法进行相同的解码,也无法找出原因。
String path = "/sdcard/bluetooth/bluetooth.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
I used the below for encoding and decoding and it works fine for me 我使用以下代码进行编码和解码,对我来说效果很好
Encode 编码
public static String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("LOOK", imageEncoded);
return imageEncoded;
}
Try the below for decoding and set the resulting bitmap to imageview and check with the original. 请尝试以下方法进行解码,然后将生成的位图设置为imageview并与原始图像进行检查。
Decode. 解码。
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.