简体   繁体   English

如何在不压缩图像的情况下将图像转换为Base64字符串?

[英]How to convert a image into Base64 string without compressing the image?

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. 请提出一些建议。

First of all using PNG compression will not lose image quality. 首先,使用PNG压缩不会丢失图像质量。

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();

To get Bitmap back from byte array: 要从字节数组取回Bitmap

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

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

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