如何在不压缩的情况下将Bitmap转换为Base64字符串? [重复]

问题描述 投票:0回答:1

我正在尝试使用此代码将位图图像转换为base64字符串。我得到了一个非常低质量的图像。将位图转换为Base64 Sring后,如何获得高质量的图像

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

如果我有一个5MB的图像。转换后,我得到了唯一的160KB图像。但在我的情况下,我不想太多地压缩我的图像我只想基于Bitmap图像获得Base64字符串只有JPEG格式,而不是任何其他格式。请帮我解决一下这个。

android android-bitmap tobase64string
1个回答
1
投票

试试这个:

 public String getBase64Image(Bitmap bitmap) {
            try {
                ByteBuffer buffer = 
                ByteBuffer.allocate(bitmap.getRowBytes() * 
                bitmap.getHeight());
                bitmap.copyPixelsToBuffer(buffer);
                byte[] data = buffer.array();
                return Base64.encodeToString(bytes, Base64.DEFAULT);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
© www.soinside.com 2019 - 2024. All rights reserved.