如何将unsigned char*转换为图像文件(如jpg)在c++中
How i can convert unsigned char* to image file (like jpg) in c++?
我有一个opengl应用程序,在格式unsigned char*中创建一个纹理,我要将此纹理保存在一个图像文件中,但我不知道该怎么做。有人能帮帮我吗?
这是我创建的纹理:
static unsigned char* pDepthTexBuf;
这是我使用这个纹理的代码:
glBindTexture(GL_TEXTURE_2D, depthTexID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pDepthTexBuf);
但我怎么能保存这个纹理"pDepthTexBuf"在图像文件?
这是一个非常复杂的问题…我建议参考其他公共示例,例如:http://www.andrewewhite.net/wordpress/2008/09/02/very-simple-jpeg-writer-in-c-c/
基本上,您需要集成一个图像库,然后使用它支持的任何钩子来保存您的数据。
最简单的方法可能是使用像OpenCV这样的库,它有一些非常容易使用的机制来将RGB数据的字节数组转换为图像文件。
你可以在这里看到读取OpenGL图像缓冲区并将其存储为PNG文件的示例。保存JPG文件可能就像更改输出文件的扩展名一样简单。
// Create an OpenCV matrix of the appropriate size and depth
cv::Mat img(windowSize.y, windowSize.x, CV_8UC3);
glPixelStorei(GL_PACK_ALIGNMENT, (img.step & 3) ? 1 : 4);
glPixelStorei(GL_PACK_ROW_LENGTH, img.step / img.elemSize());
// Fetch the pixels as BGR byte values
glReadPixels(0, 0, img.cols, img.rows, GL_BGR, GL_UNSIGNED_BYTE, img.data);
// Image files use Y = down, so we need to flip the image on the X axis
cv::flip(img, img, 0);
static int counter = 0;
static char buffer[128];
sprintf(buffer, "screenshot%05i.png", counter++);
// write the image file
bool success = cv::imwrite(buffer, img);
if (!success) {
throw std::runtime_error("Failed to write image");
}
相关文章:
- JPG-如何从ICC配置文件部分APP2读取/提取数据
- 是否有一种标准化的方法通过TCP/IP网络发送JPG图像
- 如何使用Winforms C#或C++.Net在不更改文件中其他内容的情况下修改jpg文件中的Orientation e
- 如何在c++中将image.jpg转换为image.ppm
- 我可以使用 stb_image_write.h 将 JPG 图像写入内存吗?
- Paths.get( "abc/def.jpg" ) 在 C 和 C++ 中获取与操作系统无关的路径的等价物是什么?
- 从文件中读取原始压缩缓冲区.jpg使用 jpeglib
- 将 JPG 编码数组从 ROS sensor_msgs/压缩图像保存到 roscpp 中的文件
- 将图像.jpg转换为 Base64
- 如何以CV_16UC1的方式读取JPG图像并在屏幕上打印?
- OpenGL - SOIL 不会加载 JPG 或 PNG 图像
- 使用套接字将.jpg从 Android 发送到 c++
- 如何在C 中删除或删除.jpg和.dpx图像文件
- 有没有办法仅通过JPG文件循环
- 使用C 将JPG文件添加到输出的HTML文件中
- 无法将图像保存在具有白色背景的JPG中 OpenCV
- 将文本(.txt)文件与图像(.jpg)文件进行匹配
- c++:尝试将创建的图像保存到.jpg文件,但图像无法打开
- 如何在 Windows 8 上的 C++ 中将.jpg转换为.png
- 如何将 pcl::直方图保存为 PNG 或 JPG 文件