将得到的图片base64编码存到本地

将图片保存到本地,然后将路径放到数据库中  

一、调用方法

//传入参数  图片编码,前缀,然后获取到相对路径,将pic存入数据库
 String pic1 =uploadLocal(bestImage,"pic1");

二、实现保存到本地方法


//此方法是将图片下载到本地,传入的参数plateImage 是图片得base64编码,qianzhui 是 图片得一个前缀
    private String uploadLocal(String plateImage,String qianzhui ) throws IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String datestr = sdf.format(new Date());
//        本地路径与时间拼接
        String FilePath = "/ruoyi/"+datestr;
//        将本地路径放入到file文件中,检查如果没有此路径创建新得目录
//        创建一个File对象来表示上述路径。如果该路径不存在,则调用mkdirs()方法创建所有必需的父目录。
        File file = new File(FilePath);
        if (!file.exists()) {
            // 创建文件根目录
            file.mkdirs();
        }
        String ImageDataUrl="";
        String dbpath = null;
        if(plateImage!=null && !plateImage.equals("") && plateImage.length()>0) {
            //获取图片编码base64
//            byte[] filebyte = Base64Utils.decode(plateImage.getBytes());
            byte[] filebyte = Base64.getDecoder().decode(plateImage); // 正确解码Base64字符串
//            byte[] filebyte = plateImage.getBytes();
            System.out.println(filebyte);
            //拼接图片名字
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date();
            String pic_time = sdf1.format(date);
            String orgName ="/"+qianzhui+pic_time+"_image.jpg";
            //添加路径
            String savePath =FilePath+ orgName;
            File savefile = new File(savePath);
            FileOutputStream fos = new FileOutputStream(savefile);
            fos.write(filebyte);
            // 返回文件的绝对路径
            ImageDataUrl = savefile.getPath();
            System.out.println(ImageDataUrl);
            //拼接相对路径
            dbpath = "/ruoyi/"+datestr + orgName;

        }
        return dbpath;
    }

### C++ 中将 Base64 编码的数据转换为图片 为了在 C++ 中将 Base64 编码的数据转换为图像文件,通常需要两个主要步骤: 1. **Base64 解码**: 将 Base64 字符串解码回原始的二进制数据。 2. **保存为图像文件**: 使用适当的库将这些二进制数据写入磁盘作为图像文件。 下面是一个完整的示例程序来展示这一过程。假设 `a["img_data"]` 是 JSON 对象中的 Base64 图像字符串[^1]。 #### 示例代码 ```cpp #include <iostream> #include <fstream> #include <string> #include "json/json.h" // 假设使用的是 JsonCpp 库解析 JSON 数据 #include "BASE64.h" // 包含 BASE64 头文件用于处理编解码操作 void saveImageFromBase64(const std::string& base64String, const std::string& outputPath) { // 创建共享数组对象存储解码后的图像数据 SharedCArray imgData; // 调用 Base64 解码函数 AiTools::base64Decode(imgData, base64String.c_str(), static_cast<int>(base64String.size())); // 打开目标文件流准备写入二进制模式下的图像数据 std::ofstream imageFile(outputPath, std::ios::binary); if (imageFile.is_open()) { // 写入解码得到的字节数组至文件中 imageFile.write(reinterpret_cast<const char*>(imgData.data()), imgData.size()); // 关闭文件流完成保存动作 imageFile.close(); std::cout << "成功保存图像到路径:" << outputPath << std::endl; } else { std::cerr << "无法打开指定位置创建新图像文件." << std::endl; } } int main() { // 示例:从JSON读取Base64编码的图像并将其保存本地文件 // 构造模拟的Json::Value对象表示输入源 Json::Value a; a["img_data"] = "/9j/4AAQSkZJRgABAQE..."; // 这里省略了实际的Base64字符串内容 // 定义输出路径 std::string outputPath = "./output_image.jpg"; // 调用辅助方法执行转换逻辑 saveImageFromBase64(a["img_data"].asString(), outputPath); return 0; } ``` 此段代码展示了如何通过调用自定义的 `saveImageFromBase64()` 函数接收 Base64 编码形式的图像字符串参数,并利用之前提到过的 `AiTools::base64Decode()` 方法对其进行解码,最后把结果存成真实的 JPEG 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值