无法使用QImage从原始数据重建RGB565图像

时间:2015-09-23 11:10:44

标签: qt file rgb qimage

我正在尝试从Intel hex 386格式的文件重建图像。解析文件后,我复制到QByteArray的所有数据和相同的数组用于创建QImage对象。但是重建后我得到的任何图像并不完美。我得到蓝色而不是黑色,边缘不清晰等。我正在解析的文本文件是来自STM32F4控制器(arm)的ram内存转储。图像以RGB565格式存储。

创建图片的代码:

{
    QString strFilename;
    Hex386Parser oFileParser;
    strFilename = QFileDialog::getOpenFileName(this,"Select a file", QDir::homePath());
    oFileParser.parseFile(strFilename, oByteArray);
    QImage image(320, 240, QImage::Format_RGB16);

    for (int y = 0; y < image.height(); y++)
    {
        memcpy(image.scanLine(y), oByteArray.constData() + y * image.bytesPerLine(),
            image.bytesPerLine());
    }

    qDebug() <<"Size of the byte array is " <<oByteArray.size();
    QLabel *label = new QLabel();
    label->setPixmap(QPixmap::fromImage(image));
    label->show();
}

用于解析文件的代码:

#define QT_NO_CAST_TO_ASCII
void Hex386Parser::parseFile(QString strFilename,QByteArray& ref_ByteArray)
{
    QFile oFile(strFilename);
    std::stringstream sstr;
    QString strLength;

    int unLength = 0, unAddress = 0,unDescriptor =0xFFFF,nIndex =0,nlineno=0;
    if (oFile.open((QIODevice::ReadOnly | QIODevice::Text)))
    {
        QTextStream in(&oFile);
        while (!in.atEnd())
        {
            QString line = in.readLine();
            nIndex = 0;
            nlineno++;
            //unsigned char *pCharFrame = (unsigned char *)line.toStdString().c_str();
            if (':' != line.at(nIndex))
            {
                // file corrupted
                return;
            }
            nIndex++;

            {
                strLength = line.mid(nIndex, 2);
                sstr << strLength.toStdString();
                sstr << std::hex;
                sstr >> unLength; // get length of the record
                strLength.clear();
                sstr.clear();
            }

            nIndex += 2;
            unAddress = line.mid(nIndex,4).toInt(); //  get address bytes
            nIndex +=4;
            unDescriptor = line.mid(nIndex, 2).toInt(); // get data descriptor
            nIndex += 2;
            switch(unDescriptor)
            {
            case data_record:
                ref_ByteArray.append((line.mid(nIndex, unLength )));
                // add data to bytearray
                break;
            case end_of_file_record:
                break;
            case extended_segment_address_record:
                break;
            case extended_linear_address_record:
                break;
            case start_linear_address_record:
                break;
            }
        }
        oFile.close();
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

line包含十六进制字符串数据表示,其中每个字节编码为两个字符。

你想要二进制字节。因此,2 * unLength符号应该从line中读取。然后,该数据字符串应转换为二进制,例如:

{
case data_record:
    QByteArray hex = line.mid(nIndex, 2 * unLength ).toLatin1();
    QByteArray binary = QByteArray::fromHex(hex);
    ref_ByteArray.append(binary);
...
}