如何在不压缩的情况下将位图转换为Base64字符串? [英] How to convert Bitmap into Base64 String without compressing?

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

问题描述

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

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

如果我有5MB的图像.转换后,我得到唯一的160KB图像.但就我而言,我不想压缩太多图像,我只想获取基于Bitmap图像(仅JPEG格式)的Base64字符串,而不是其他任何格式.请帮助我.

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.

推荐答案

尝试一下:

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

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

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