如何在QT中从json加载base64图像数据

时间:2021-01-15 20:27:31

I am beginning with QT5 and trying to load an image from base64 json data. I can load directly from a base64 string but is unsuccessful when trying to load from json object.

我从QT5开始并尝试从base64 json数据加载图像。我可以直接从base64字符串加载,但在尝试从json对象加载时不成功。

the error i am getting is error: conversion from 'QJsonValueRef' to non-scalar type 'QByteArray' requested

我得到的错误是错误:从'QJsonValueRef'转换为非标量类型'QByteArray'请求

I tried changing toUtf8 toAcsii() etc. but similar error are being produced. Any help and suggestions will be much appreciated.

我尝试更改为Utf8 toAcsii()等,但正在产生类似的错误。任何帮助和建议将不胜感激。

QString strReply = (QString)reply->readAll(); // json data from a servlet (created using gson library)
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
QByteArray imgbase64 = jsonObj["photo"]; // image data
QImage img;
img.loadFromData(QByteArray::fromBase64(imgbase64));
ui->outputImage->setPixmap(QPixmap::fromImage(img));
ui->outputImage->setScaledContents( true );
ui->outputImage->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );

2 个解决方案

#1


1  

error: conversion from 'QJsonValueRef' to non-scalar type 'QByteArray' requested*

错误:从'QJsonValueRef'转换为非标量类型'QByteArray'请求*

Since you didn't specify this, I'm expect that the error you're seeing is coming from this line: -

既然你没有指定这个,我希望你看到的错误来自这一行: -

QByteArray imgbase64 = jsonObj["photo"]; // image data

Calling the [] operator on a QJsonObject returns a QJsonValue. There is no overloaded = operator in QByteArray to initialise it from a QJsonValue.

在QJsonObject上调用[]运算符会返回QJsonValue。 QByteArray中没有重载=运算符来从QJsonValue初始化它。

What you need to do is use the QJsonValue functions and do something like this: -

你需要做的是使用QJsonValue函数并执行以下操作: -

QByteArray imgbase64;
if(jsonObj["photo"].isString())
{
    imgbase64 = jsonObj["photo"].toString().toUtf8();
}

Since the photo object is expected to be in Base64, it is acceptable as a string object.

由于照片对象应该在Base64中,因此可以作为字符串对象使用。

#2


0  

Following code converts JSON object image field to QString and QString to QImage -

以下代码将JSON对象图像字段转换为QString,将QString转换为QImage -

QString base64img = jsonObj["photo"].toString();
QByteArray by = QByteArray::fromBase64(base64img.toLatin1());
QImage img = QImage::fromData(by,"JPEG");

#1


1  

error: conversion from 'QJsonValueRef' to non-scalar type 'QByteArray' requested*

错误:从'QJsonValueRef'转换为非标量类型'QByteArray'请求*

Since you didn't specify this, I'm expect that the error you're seeing is coming from this line: -

既然你没有指定这个,我希望你看到的错误来自这一行: -

QByteArray imgbase64 = jsonObj["photo"]; // image data

Calling the [] operator on a QJsonObject returns a QJsonValue. There is no overloaded = operator in QByteArray to initialise it from a QJsonValue.

在QJsonObject上调用[]运算符会返回QJsonValue。 QByteArray中没有重载=运算符来从QJsonValue初始化它。

What you need to do is use the QJsonValue functions and do something like this: -

你需要做的是使用QJsonValue函数并执行以下操作: -

QByteArray imgbase64;
if(jsonObj["photo"].isString())
{
    imgbase64 = jsonObj["photo"].toString().toUtf8();
}

Since the photo object is expected to be in Base64, it is acceptable as a string object.

由于照片对象应该在Base64中,因此可以作为字符串对象使用。

#2


0  

Following code converts JSON object image field to QString and QString to QImage -

以下代码将JSON对象图像字段转换为QString,将QString转换为QImage -

QString base64img = jsonObj["photo"].toString();
QByteArray by = QByteArray::fromBase64(base64img.toLatin1());
QImage img = QImage::fromData(by,"JPEG");