• 15387阅读
  • 7回复

请教各位朋友,在QT下怎么把图片转换成字符串,然后又可以通过字符串重新生成那个图片文件? [复制链接]

上一主题 下一主题
离线ted522
 
只看楼主 倒序阅读 楼主  发表于: 2010-07-23
        请教各位朋友,在QT下怎么把图片转换成字符串,然后又可以通过字符串重新生成那个图片文件?              字符串可以是16进制,base64等等
    我想请教,怎么在QT下有什么函数,或者什么代码可以把图片转换成字符串的格式,
    如把BMP、png图片转换成16进制的.txt文件,
    然后通过那个16进制串,或者那个TXT文件又把它生产一个图片格式的文件?
    我是个新手,请各位朋友指点一下,拜谢~~~
离线dbzhang800

只看该作者 1楼 发表于: 2010-07-23
1. 首先,怎么保存图片到硬盘文件应该知道吧?
    如果 知道,所做的无非是将你的Image保存到内存中,比如QByteArray

2. 图片到QByteArray,方法太多了
  比如:
          QImage 的manual中的对 save 成员函数的介绍中,举的例子就是保存到 QByteArray

再比如 QDataStream 是很容易想到的办法

再比如,直接访问Image的 bits

再比如:

3 得到QByteArray,怎么转换成字符串(此处指代全部是可显示字符的QByteArray),方法就更多了
   比如 直接 toHex
  比如 直接 toBase64
...

4. 多看Qt自带的Manual
离线ted522
只看该作者 2楼 发表于: 2010-07-23
回 1楼(dbzhang800) 的帖子
哇,谢谢这位朋友,我先照你说的去看看那些资料先!
离线ted522
只看该作者 3楼 发表于: 2010-07-27
回 1楼(dbzhang800) 的帖子
大哥,的确是用那些内容,不过我看了也还不是很懂,能不能联系一下,教小弟这个问题。。。我QQ308156717
离线ted522
只看该作者 4楼 发表于: 2010-07-27
回 1楼(dbzhang800) 的帖子
3 得到QByteArray,怎么转换成字符串(此处指代全部是可显示字符的QByteArray),方法就更多了
   比如 直接 toHex
  比如 直接 toBase64
这一步看了Manual 就很容易搞定,我再看看前面。。。
离线ted522
只看该作者 5楼 发表于: 2010-07-27
回 1楼(dbzhang800) 的帖子
#include <QByteArray>
#include <QtDebug>
#include <iostream>
#include <QImage>
#include <QBuffer>

using namespace std;
int main()
{
         QByteArray text("Qt is great!");
         text.toHex();        // returns "UXQgaXMgZ3JlYXQh"a
         //qDebug()<<text.toHex();
         QByteArray text1 = QByteArray::fromHex(text.toHex());
         //qDebug()<<text1.data();
         QImage image("new.png");
         QByteArray ba;
         QBuffer buffer(&ba);
         buffer.open(QIODevice::WriteOnly);
         image.save(&buffer, "PNG"); // writes image into ba in PNG format
         ba.toHex();
         //qDebug()<<ba.toHex();
         QByteArray text4 = QByteArray::fromHex(ba.toHex());
         //qDebug()<<text4.data();
         //qDebug()<<ba.data();


}
这是我写的测试一下功能的。。。代码
这里,我把当前文件夹new.png转成的16进制,在 QByteArray 中,现在我想知道怎么利用转出来的 QByteArray  重新生成那个new.png文件,好比我要生成一个new2.png.
我查了一下,
QImage QImage::fromData ( const QByteArray & data, const char * format = 0 )   [static]
This is an overloaded function.

Loads an image from the given QByteArray data.

还有
bool    save ( const QString & fileName, const char * format = 0, int quality = -1 ) const
好像可以生成

我想新建一个文件,需要QFILE一个文件先吧??然后怎么做呢。。。。
离线ted522
只看该作者 6楼 发表于: 2010-07-28
回 5楼(ted522) 的帖子
已解决
#include <QCoreApplication>
#include <QImage>
#include <QFile>
#include <QByteArray>
#include <QBuffer>


int main(int argc, char *argv[])
{
      QCoreApplication app(argc, argv);
      
      QImage image("test.png");
      QByteArray ba;
      QBuffer buf(&ba);
      image.save(&buf, "BMP");
      
      QByteArray compressed = qCompress(ba, 1); // better just open file with QFile, load data, compress and toHex?
      QByteArray hexed = compressed.toHex();
      // save to a file
      QString str(hexed);
      QFile f("test.hex");
      if (f.exists())
          f.remove();
      if (f.open(QFile::WriteOnly))
      {
          f.write(str.toLatin1()); // holds only 0..f nothing special.
      }
      else
          qDebug("failed to open file \"test.hex\"");
      f.close();
      ////----------
      if (f.open(QFile::ReadOnly))
      {
          QByteArray read = f.readAll();
          f.close();
          QString rStr = QString::fromLatin1(read.data(), read.size());
          if (rStr != str)
              qDebug("Writed and read two different hexed strings.");
          QByteArray readCompressed = QByteArray::fromHex(rStr.toAscii());
          if (readCompressed != compressed)
              qDebug("bytes before hexing and dehexing _is_ different.");
          QByteArray readDecompressed = qUncompress(readCompressed);
          if (readDecompressed != ba)
              qDebug("bytes before and after compressions are different.");
          QImage readImg;
          //QBuffer readBuf(&readDecompressed);
          readImg.loadFromData(readDecompressed);
          if (readImg.isNull())
              qDebug("The image is null. Something failed.");
          readImg.save("test.bmp");
      }
      else
          qDebug("failed to open test.hex file for reading");
      return 0;
}
离线tootzoe
只看该作者 7楼 发表于: 2010-07-29
  1. #include <QtGui>
  2. int main(int argc, char **argv)
  3. {
  4.     QApplication app(argc, argv);
  5.      QSettings settings("imgIniFile", QSettings::IniFormat);
  6.     if( argc >= 2    )  /// 随便传递一个启动参数,就能显示图片
  7.         if(settings.contains("Logo")){
  8.                 QPixmap pix ;
  9.                 pix.loadFromData(QByteArray::fromHex( settings.value("Logo").toByteArray()));
  10.                 if(!pix.isNull()){
  11.                 QLabel imgLabel;
  12.                 imgLabel.setPixmap(pix);
  13.                 imgLabel.show();
  14.                 return app.exec();
  15.                }
  16.         }
  17.     settings.setValue("SiteName", "TOOTzoe.com");
  18.     QImage img("configure32.jpg");
  19.     QBuffer  buf  ;
  20.     img.save(&buf, "JPG");
  21.     settings.setValue("Logo" , buf.buffer().toHex()) ;
  22.     return  0;
  23. }
快速回复
限100 字节
 
上一个 下一个