Java实现Base64码与图片互转

介绍Java实现Base64码与图片互转方法

工具类

import java.io.*;

import org.apache.commons.codec.binary.Base64;

public class Base64ImageUtils {

    /**
     * 图片转化成base64字符串
     * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     * @param imgPath
     * @return
     */
    public static String getImageBase64Str(String imgPath) {
        File imgFile = new File(imgPath); // 待处理的图片
        if (!imgFile.exists()) {
            return null;
        }

        InputStream in = null;
        byte[] data = null;
        String encode = null; // 返回Base64编码过的字节数组字符串
        // 对字节数组Base64编码
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            int len = in.read(data);
            if (len > 0) {
                return new String(Base64.encodeBase64(data));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * base64字符串转化成图片
     * 对字节数组字符串进行Base64解码并生成图片
     * @param imgData 图片编码
     * @param imgPath 存放到本地路径
     * @return
     * @throws IOException
     */
    public static boolean generateImage(String imgData, String imgPath) throws IOException {
        if (imgData == null) // 图像数据为空
            return false;
        OutputStream out = null;
        try {
            out = new FileOutputStream(imgPath);
            // Base64解码
            byte[] b = Base64.decodeBase64(imgData);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
            out.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return false;
    }
}

测试

public static void main(String[] args) throws Exception {
    String imageStr = Base64ImageUtils.getImageBase64Str("C:\\Users\\yezhou\\Downloads\\logo128.jpg");
    System.out.println(imageStr);
    String imageData = "/9j/4AAQSkZJRgABAAEBLAEsAAD//gAfTEVBRCBUZWNobm9sb2dpZXMgSW5jLiBWMS4wMQD/2wCEAAUFBQgFCAwHBwwMCQkJDA0MDAwMDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0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4Dk4OOcegxz9c9MkfKZ3h6lalCdKLl7OTckld2atdJau1tbd7mclpoZPgbTJ7eWS5lQxoUKDcMZJZTx6gbeT7j3x52RYerCpPETi4w5OVcyau3JPRPWytq/MUFbU7nVNLh1aHyJwcZyCOCp6ZHUd+hH64I+txeEp42n7Gte17prRxfdfk09GjRq+hT0bw/b6JuMOWd+CzYzj0GOgJwT1zgelcuCy6ll/M6bcpy0cpWvbeySskur7iUeXY3a9koKACgAoAKAMDU/Ddpqkgml3I/GShA3Y45yp7cZGOAPSgDXtLWOyiWCEbUToPryT9SSSaAMjxLZS31k0cI3OCG29yBkHHvznHU44ycCgDjPCWk3UV6s8kbxpGGyXUrklSoAzjJyc8dgc9sgHqVABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFAH//Z";
    Base64ImageUtils.generateImage(imageData, "C:\\Users\\yezhou\\Downloads\\mylogo.jpg");
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/26/java-implementation-of-base64-code-and-image-conversion/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Java实现Base64码与图片互转
介绍Java实现Base64码与图片互转方法 工具类 import java.io.*; import org.apache.commons.codec.binary.Base64; public class Base64ImageUtils { /……
<<上一篇
下一篇>>
文章目录
关闭
目 录