c++ - Qt QImage像素操作问题

标签 c++ qt bit-manipulation steganography qimage

我目前正在使用 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;
}

谢谢

最佳答案

确保您没有使用有损格式保存,例如 JPEG。

关于c++ - Qt QImage像素操作问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/857962/

相关文章:

c++ - 如何去序列化一个大型的,复杂的对象?

c# - C#按位运算符

c++ - 在 CMake 中包含目录

c++ - 如何在创建时自动注册一个类

c++ - 工具栏中带有 QAction 的 Qt QActionGroup

c++ - 在 Qt 中运行函数时如何断开所有信号?

c# - Bitwise Shift - 在 C#.net 与 PHP 中获得不同的结果

c++ - 在这个位移操作中做了什么?

c++ - 为什么我在树的插入函数中收到段错误?

c++ - 如何正确初始化全局变量?