c++ base64写jpg

时间: 2023-10-14 10:16:59 浏览: 38
这里提供一个基于 C++11 标准的 base64 编码和解码实现,以及使用 libjpeg-turbo 库读取和写入 JPEG 图片的示例代码: ```cpp #include <iostream> #include <string> #include <vector> #include <cstring> #include <cstdlib> #include <fstream> #include <sstream> #include <algorithm> #include <iterator> #include <jpeglib.h> // Base64 编码 std::string base64_encode(const std::string& input) { static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; std::string output; output.reserve(((input.size() + 2) / 3) * 4); uint32_t buf = 0; int bits = 0; for (char c : input) { buf = (buf << 8) | static_cast<uint8_t>(c); bits += 8; while (bits >= 6) { bits -= 6; output.push_back(table[(buf >> bits) & 0x3F]); } } if (bits > 0) { buf <<= (6 - bits); output.push_back(table[buf & 0x3F]); while (bits < 6) { output.push_back('='); bits += 2; } } return output; } // Base64 解码 std::string base64_decode(const std::string& input) { static const unsigned char table[] = { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 0-15 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63, // 16-31 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 64, 99, 99, // 32-47 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 48-63 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99, // 64-79 99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 80-95 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 99, 99, 99, 99, 99, // 96-111 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 112-127 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 128-143 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 144-159 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 160-175 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 176-191 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 192-207 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 208-223 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 224-239 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 // 240-255 }; std::string output; output.reserve(input.size() * 3 / 4); uint32_t buf = 0; int bits = 0; for (char c : input) { if (c == '=') { break; } buf = (buf << 6) | table[static_cast<uint8_t>(c)]; bits += 6; if (bits >= 8) { bits -= 8; output.push_back((buf >> bits) & 0xFF); } } return output; } // 读取 JPEG 图片 bool read_jpeg(const std::string& filename, std::vector<uint8_t>& data, int& width, int& height) { FILE* fp = fopen(filename.c_str(), "rb"); if (!fp) { return false; } struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); width = cinfo.output_width; height = cinfo.output_height; int row_stride = width * cinfo.output_components; data.resize(row_stride * height); while (cinfo.output_scanline < cinfo.output_height) { uint8_t* buffer[1] = { &data[cinfo.output_scanline * row_stride] }; jpeg_read_scanlines(&cinfo, buffer, 1); } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(fp); return true; } // 写入 JPEG 图片 bool write_jpeg(const std::string& filename, const std::vector<uint8_t>& data, int width, int height, int quality = 90) { FILE* fp = fopen(filename.c_str(), "wb"); if (!fp) { return false; } struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, fp); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); jpeg_start_compress(&cinfo, TRUE); int row_stride = width * 3; while (cinfo.next_scanline < cinfo.image_height) { const uint8_t* buffer = &data[cinfo.next_scanline * row_stride]; jpeg_write_scanlines(&cinfo, const_cast<uint8_t**>(&buffer), 1); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); fclose(fp); return true; } int main() { // 读取 JPEG 图片 std::vector<uint8_t> jpg_data; int width = 0, height = 0; if (!read_jpeg("input.jpg", jpg_data, width, height)) { std::cerr << "Failed to read input.jpg" << std::endl; return EXIT_FAILURE; } // Base64 编码 std::string base64_data = base64_encode(std::string(reinterpret_cast<const char*>(jpg_data.data()), jpg_data.size())); // Base64 解码 std::vector<uint8_t> decoded_data(base64_decode(base64_data)); // 写入 JPEG 图片 if (!write_jpeg("output.jpg", decoded_data, width, height)) { std::cerr << "Failed to write output.jpg" << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } ``` 说明: - `base64_encode` 函数实现了 Base64 编码。 - `base64_decode` 函数实现了 Base64 解码。 - `read_jpeg` 函数使用 libjpeg-turbo 库读取 JPEG 图片。 - `write_jpeg` 函数使用 libjpeg-turbo 库写入 JPEG 图片。 - 主函数中读取输入图片,进行 Base64 编码,再解码为二进制数据,并写入输出图片。 注意事项: - 本示例代码使用了 libjpeg-turbo 库,请确保已经安装并配置好该库。 - 本示例代码仅支持 RGB 格式的 JPEG 图片,如果需要处理其他格式的 JPEG 图片,需要修改相关代码。

相关推荐

最新推荐

recommend-type

Java课程设计-java web 网上商城,后台商品管理(前后端源码+数据库+文档) .zip

项目规划与设计: 确定系统需求,包括商品管理的功能(如添加商品、编辑商品、删除商品、查看商品列表等)。 设计数据库模型,包括商品表、类别表、库存表等。 确定系统的技术栈,如使用Spring MVC作为MVC框架、Hibernate或MyBatis作为ORM框架、Spring Security进行权限控制等。 环境搭建: 搭建开发环境,包括安装JDK、配置Servlet容器(如Tomcat)、配置数据库(如MySQL)等。 创建一个Maven项目,添加所需的依赖库。 数据库设计与创建: 根据设计好的数据库模型,在数据库中创建相应的表结构。 后端开发: 创建Java实体类,对应数据库中的表结构。 编写数据访问层(DAO)代码,实现对商品信息的增删改查操作。 编写服务层(Service)代码,实现业务逻辑,如商品管理的各种操作。 开发控制器层(Controller),实现与前端页面的交互,接收请求并调用相应的服务进行处理。 前端开发: 使用HTML、CSS和JavaScript等前端技术,设计并实现商品管理页面的界面。 通过Ajax技术,实现前后端的数据交互,如异步加载商品列表、实
recommend-type

母线电容计算 .xmcd

变频器 母线电容计算 mathcad
recommend-type

2022年中国大学生计算机设计大赛国赛优秀作品点评微课与教学辅助&数媒静态设计专业组视频

2022年中国大学生计算机设计大赛国赛优秀作品点评微课与教学辅助&数媒静态设计专业组视频提取方式是百度网盘分享地址
recommend-type

输出月份英文名pta.doc

如果您想要在程序中输出月份的英文名称,并且提到了“pta”,但这里的“pta”与月份名称的输出没有直接关系(除非您是在特定的上下文中使用它作为一个变量名或标识符)。不过,我将直接给出如何输出月份英文名称的代码示例。 在Python中,您可以使用一个列表(list)或字典(dictionary)来存储月份的英文名称,并根据需要输出它们。以下是一个简单的示例: python # 使用列表存储月份的英文名称 months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] # 假设我们要输出第5个月份(即May) month_index = 4 # 注意列表索引从0开始,所以5月份是索引4 print(months[month_index]) # 输出: May # 或者,如果您想要通过月份的数字(1-12)来输出名称,可以稍作调整 def get_mo
recommend-type

单片机C语言Proteus仿真实例万年历

单片机C语言Proteus仿真实例万年历提取方式是百度网盘分享地址
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。