Qt QImage像素处理问题

3
我目前正在使用Qt编写隐写应用程序。我试图将我的消息位隐藏在像素的最低有效位的蓝色中。
从调试中可以看出,这一部分按照预期工作。然而,在隐藏了我的比特消息之后,我保存了图像,然后重新打开它。这就是问题产生的地方。
当我读取(重新打开)图像时,我读取的scanLines与我之前写入的不同,我无法弄清原因。也许只是我太傻了,或者我错过了什么。任何帮助都将不胜感激。
我目前的代码如下:
void MainWindow::Encrypt(QImage image, QString message) {
    if(image.isNull()) {
        qDebug() << "PROBLEM";
    }

    image = image.convertToFormat(QImage::Format_ARGB32);

    QVector<bool> bvec;
    QByteArray bytes = message.toAscii();
    char mask;
    QRgb tempPix;

    for(int i = 0; i < bytes.size(); i++) {
        for(int j = 0; j < 8; j++) {
            mask = (0x01 << j);
            bvec.push_back((bytes[i] & mask) == mask);
        }
    }

    if(image.height() < bvec.size()) {
        qDebug() << "Not enough space in image";
    }

    for(int j = 0; j < bvec.size(); j++) {
        QRgb *pixel = (QRgb *)image.scanLine(j);
        tempPix = *pixel;
        int blue = qBlue(tempPix);

        blue &= 0xFE;
        blue |= (bvec[j] == 1) ? 0x01 : 0x00;
        *pixel = qRgba(qRed(tempPix), qGreen(tempPix), blue, qAlpha(tempPix));
    }

    if(image.save(filename) != true) {
       emit addToStatusLog("Did not save. Error");
    }   
}

void MainWindow::Decrypt(QImage image) {
    char temp = 0x00;
    qint8 mask = 0x01;
    QVector<bool> bvec;
    QRgb *pixel;
    int blue;

    image = image.convertToFormat(QImage::Format_ARGB32);

    for(int i = 0; i < image.height(); i++) {
        pixel = (QRgb *)image.scanLine(i);
        blue = qBlue(*pixel);
        bvec.push_back((blue & mask) == mask);
    }

     for(int j = 0; j < bvec.size(); j++) {
        if(j % 8 == 0 && j != 0) {
            qDebug() << temp;
            temp = 0x00;
        }
        temp |= (bvec[j]) ? (0x01 << (j%8)) : 0x00;
    }

    qDebug() << temp;
}

谢谢


1
你难道不是在使用有损压缩格式吗? - Marcelo MD
1个回答

10

请确保不要使用像JPEG这样的有损格式进行保存。


非常感谢,将其保存为PNG格式解决了问题。 =] - mdec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接