将 QImage 转换为字符*
convert QImage to char*
我捕获图像并使用QByteArray在其中存储和保存图像:
QImage image(WEB_SCREENSHOT_WIDTH, page.viewportSize().height()/*65000,*/, QImage::Format_ARGB32_Premultiplied);
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
我想将 QByteArray 传递给 char* 并在函数结束时返回它,如下所示:
unsigned char* char_return = (unsigned char*)bytes.data();
我必须这样做,因为我的程序的其余部分是 C 基础......
但是最后保存的文件无法打开
请帮帮我
编辑:
最小函数是这样的:
unsigned char* web_screenshot::get_web_image(){
QImage image(WEB_SCREENSHOT_WIDTH, page.viewportSize().height(), QImage::Format_ARGB32_Premultiplied);
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
unsigned char* char_return = (unsigned char*)bytes.data();
return char_return;
}
像这样制作 bytes.data(( 的深层副本:
unsigned char *data = (unsigned char *) malloc unsigned char(bytes.size());
memcpy(data, reinterpret_cast<unsigned char *>(bytes.data()), bytes.size());
更新:
下面,您可以看到一个功能示例,其中原始图像数据的副本用于加载UI上显示的新图像:
unsigned char* deepCopyImageData(int &size){
QImage image("test.png"); // test image
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
buffer.close();
unsigned char *data = (unsigned char *) malloc(bytes.size());
memcpy(data, reinterpret_cast<unsigned char *>(bytes.data()), bytes.size());
size = bytes.size();
return data;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int size;
unsigned char *data = deepCopyImageData(size);
QImage image;
if(!image.loadFromData(data,size))
qWarning("Image loading failed");
free(data); data = nullptr;
QLabel *label = new QLabel();
label->setPixmap(QPixmap::fromImage(image));
label->show();
return a.exec();
}
相关文章:
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 字符转换功能 std::isupper() & std::islower() C++17
- 将字符 * 转换为字符串 *
- 在编译时将常量字符* 转换为常量 char_type*
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 无法使用字符串流将字符转换为字符串C++
- 将无符号字符 C++ 转换为 C#
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法<string>从"常量字符 []"转换为<类名>
- 不能将字符转换为整数吗?
- 编译时将字符*转换为字节
- CP1251:从字符* 转换为 wchar_t* 时的编码失真
- 将单个字符转换为 std::string 前缀 \x01
- PPM 将字符转换为 int 授予负数
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类
- 将常量字符* 转换为字符时出错