将base64编码转换为图片

    /**
     * 将字符串转为图片
     * @param imgStr
     * @return
     */
    @RequestMapping("/generateImage")
    public static boolean generateImage(String imgStr,String imgFile) throws Exception {// 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 判断图像数据是否为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            String imgFilePath = imgFile;// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

 

posted on 2022-06-07 13:51  青小记  阅读(1427)  评论(0编辑  收藏  举报