如何在不压缩图像的情况下将图像转换为Base64字符串? [英] How to convert a image into Base64 string without compressing the image?

查看:63
本文介绍了如何在不压缩图像的情况下将图像转换为Base64字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次更新同一张图片时,图像压缩方法都会降低图像质量.

Every time I update the same picture the image quality has been decreased by image compression method.

 public String BitMapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    base64Image = Base64.encodeToString(b, Base64.DEFAULT);
    return base64Image;
}

我不想一次又一次地压缩图像.请提出一些建议.

I don't want to compress image again and again. Please suggest something.

推荐答案

首先使用PNG压缩不会丢失图像质量.

First of all using PNG compression will not lose image quality.

如果无论如何都希望获得不压缩的字节,则可以尝试以下操作:

If you want to get the bytes without compression anyway, you can try the following:

ByteBuffer buffer = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();

要从字节数组中获取位图:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data));
return bitmap;

这篇关于如何在不压缩图像的情况下将图像转换为Base64字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆