|
想用Qt对图片进行处理,使用混沌算法,对bmp和png两种格式可以正常使用(即可以加密和正常恢复),但在对jpg格式的图片进行加密处理后却无法正常恢复原图,求各位大神帮帮忙。加密代码如下- QString fileName = QFileDialog::getOpenFileName(this, "打开文件", "", "");
- if (fileName.isEmpty())
- return;
- QImage image(fileName);
- int chaos;
- double a;
- double x;
- double keys[][2] = {{4, 0.6}, {3.999, 0.8}, {3.7, 0.7}, {3.888, 0.6}};
- for (int kNum = 0; kNum < 4; ++kNum)
- {
- a = keys[kNum][0];
- x = keys[kNum][1];
- for (int c = 0; c < 1000; ++c)
- x = a*x*(1-x);
- for (int j = 0; j < image.height(); ++j)
- {
- for (int i = 0; i < image.width(); ++i)
- {
- chaos = 0;
- for (int c = 0; c < 8; ++c)
- {
- x = a*x*(1-x);
- if (x >= a/6)
- chaos = ((chaos << 1) | 1);
- else
- chaos = ((chaos << 1) | 0);
- }
- QColor color(image.pixel(i, j));
- color.setRed(color.red() ^ chaos);
- color.setGreen(color.green() ^ chaos);
- color.setBlue(color.blue() ^ chaos);
- image.setPixel(i, j, color.rgb());
- }
- }
- }
- QString saveName = QFileDialog::getSaveFileName(this, "保存文件", "", "");
- if (saveName.isEmpty())
- return;
- image.save(saveName, fileName.section('.', -1).toStdString().c_str(), 100);
|