图片和base64 互转

3 篇文章 0 订阅

   在此介绍Spring中的 org.springframework.util.Base64Utils 工具类,提供了丰富的encode 和 decode 方法。

   我们一般对图片byte和base64后的 String 进行互转处理。这里主要是使用 Base64Utils.encodeToString ()和Base64Utils.decodeFromString().

下面是继承了Spring Base64Utils的类,可以对其进行扩展。

package com.huitongjy.generator.reactor.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * created by htjy_ks on 2019-07-29
 */
public class Base64Util extends Base64Utils {

    private static Logger logger = LoggerFactory.getLogger(Base64Util.class);

    /**
     * 将图片文件转换成base64字符串,参数为该图片的路径
     *
     * @param imageFile
     * @return java.lang.String
     */
    public static String ImageToBase64(String imageFile) {
        InputStream in = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imageFile);
            byte[] data = new byte[in.available()];
            in.read(data);
            return encodeToString(data);
//          return "data:image/jpeg;base64," + encodeToString(data);// 返回Base64编码过的字节数组字符串
        } catch (Exception e) {

            if (in!=null){
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            }
        }
        return null;
    }

    /**
     * 将base64解码成图片并保存在传入的路径下
     * 第一个参数为base64 ,第二个参数为路径
     *
     * @param base64, imgFilePath
     * @return boolean
     */
    public static boolean Base64ToImage(String base64, String imgFilePath) {
        // 对字节数组字符串进行Base64解码并生成图片
        if (base64 == null) // 图像数据为空
            return false;
        try {
            // Base64解码
            byte[] b = decodeFromString(base64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * base64 String 转文件流
     */
    public static BufferedImage getImageInput(String imageBase64) throws IOException {
        byte[] b = decodeFromString(imageBase64);
        InputStream in = new ByteArrayInputStream(b);
        return ImageIO.read(in);
    }
    /**
     * 图片流 转 base64
     */
    public static String getBase64Str(BufferedImage bi) throws IOException {
        ByteArrayOutputStream ot = new ByteArrayOutputStream();
        ImageIO.write(bi, "png",ot);
        // byte 转base64
        return encodeToString(ot.toByteArray());
    }
}

在此,扩展增加了将文件和base64转码后的字符串进行互转。

由于我们处理图片需要用到BufferedImage,在此添加字符串转文件流的工具类。

以上供学习图片处理的同学参考。

有不对的或者需要补充的欢迎大家提出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值