How to convert Bitmap into Base64 String without compressing? [duplicate]

[亡魂溺海] 提交于 2020-01-06 06:17:06

问题


I am trying to convert a bitmap image into base64 String using this code. I and getting a very low-quality image. How can I get a good quality image after convert bitmap into 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;
}

If I have a 5MB image. After convert, I am getting the only 160KB image. But in my case, I don't want to compress my image too much I just want to get Base64 String based on Bitmap image only JPEG format, not any other. Please help me with this.


回答1:


Try this:

 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;
        }


来源:https://stackoverflow.com/questions/55785798/how-to-convert-bitmap-into-base64-string-without-compressing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!