关于Bitmap和Base64字符串之间的相互转换

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 19:23   11   0

在bitmap转Base64字符串的时候,转换的速度和你要的图片质量有关。如果将100换成40,将瞬间转换好

相关代码如下:


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class Base64Util {

    /**
     * 这个图片转字符串的方法-----比较慢
     * @param imageFile 图片的路径
     * @return
     */
    public static String getImageStr(String imageFile){
        InputStream in=null;
        byte[] data=null;
        //读取图片字节数组
        try {
            in=new FileInputStream(imageFile);
            data=new byte[in.available()];
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //对字节数组Base64编码
        return Base64.encodeToString(data, Base64.DEFAULT);

    }

    /**
     * 这个转字符串的方法----降低图片质量则比较快
     * @param imgFile  图片路径
     * @return
     */
    public static String bitmapToStrByBase64(String imgFile){

        InputStream in;
        Bitmap bitmap = null;
        try {
            in=new FileInputStream(imgFile);
            bitmap= BitmapFactory.decodeStream(in);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);  //转字符串的快慢程度和你要的质量有关
        byte[] bytes=bos.toByteArray();
        return Base64.encodeToString(bytes, android.util.Base64.DEFAULT);
    }

    /**
     * 字节数组字符串进行base64编码生成图片 保存到imgFile途径
     * @param imgStr    字节数组字符串的路径
     * @param imgFile   生成的图片路径
     * @return
     */
    public static boolean generateImage(String imgStr,String imgFile){
        //对字节数组字符串进行Base64解码并生成图片
        if (imgStr==null){  //图像数据为空
            return false;
        }
        try {
            byte[] bytes = Base64.decode(imgStr, Base64.DEFAULT);
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i]<0){
                    //调整异常数据
                    bytes[i]+=256;
                }
            }
            //生成jpeg图片

            OutputStream out=new FileOutputStream(imgFile);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * bitmap转为base64 字符串
     * @param bitmap 图片
     * @return
     */
    public static String bitmapToBase64(Bitmap bitmap){
        String result=null;
        ByteArrayOutputStream baos=null;
        try {
            if (bitmap!=null){
                baos=new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes=baos.toByteArray();
                result= android.util.Base64.encodeToString(bitmapBytes, android.util.Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (baos!=null){
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * base64转为bitmap
     * @param base64Data
     * @return
     */
    public static Bitmap base64ToBitmap(String base64Data){
        byte[] bytes= android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes,0,bytes.length);
    }

}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP