每次我更新同一张图片时,图像压缩方法都会降低图像质量。
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;
}
我不想一次又一次地压缩图像。请提出一些建议。
最佳答案
首先使用PNG压缩不会损失图像质量。
如果你想在不压缩的情况下获取字节,你可以尝试以下方法:
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;
关于java - 如何在不压缩图像的情况下将图像转换为Base64字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679736/