学习QT已经一个月了,觉得QT真是个好东西,他的这种跨平台机制简直太棒了!还有QML这东西,真牛B!在这段学习期间,因为一个项目的需要,需要将图片转成数据,然后传出去,那边接收到数据后再对应转换成图片,才有了下面这个小工具,开发环境:QT 4.7.0,在LINUX下面重新编译也可以在LINUX下面运行,代码不用改!先放张截图吧。
本人比较懒,还是贴出代码来吧!其中主要就是两个函数。
void picToData(QString fromPic,QString toData,QString myformat);
void dataToPic(QString fromData,QString toPic,QString myformat); 外加几个辅助处理函数:QString getFileName(QString filter);//获取打开对话框中指定格式的文件QStringList getFileNames(QString filter);//获取打开对话框中指定格式的文件列表void delayTime(int s); frmmain.h代码:
#ifndef FRMMAIN_H
#define FRMMAIN_H
#include QWidget>
#include QtGui>
#include QDrag>
namespace Ui {
class frmmain;
}
class frmmain : public QWidget
{
Q_OBJECT
public:
explicit frmmain(QWidget *parent = 0);
~frmmain();
private:
Ui::frmmain *ui;
private slots:
void on_btnChangeToJpg_clicked();
void on_btnChangeToTxt_clicked();
void on_btnSelectTxt_clicked();
void on_btnSelectFile_clicked();
void on_btnDataToPic_clicked();
void on_btnPicToData_clicked();
void on_btn2_clicked();
void on_btn1_clicked();
void picToData(QString fromPic,QString toData,QString myformat);
void dataToPic(QString fromData,QString toPic,QString myformat);
QString getFileName(QString filter);//获取打开对话框中指定格式的文件
QStringList getFileNames(QString filter);//获取打开对话框中指定格式的文件列表
void delayTime(int s);
};
#endif // FRMMAIN_H
#define FRMMAIN_H
#include QWidget>
#include QtGui>
#include QDrag>
namespace Ui {
class frmmain;
}
class frmmain : public QWidget
{
Q_OBJECT
public:
explicit frmmain(QWidget *parent = 0);
~frmmain();
private:
Ui::frmmain *ui;
private slots:
void on_btnChangeToJpg_clicked();
void on_btnChangeToTxt_clicked();
void on_btnSelectTxt_clicked();
void on_btnSelectFile_clicked();
void on_btnDataToPic_clicked();
void on_btnPicToData_clicked();
void on_btn2_clicked();
void on_btn1_clicked();
void picToData(QString fromPic,QString toData,QString myformat);
void dataToPic(QString fromData,QString toPic,QString myformat);
QString getFileName(QString filter);//获取打开对话框中指定格式的文件
QStringList getFileNames(QString filter);//获取打开对话框中指定格式的文件列表
void delayTime(int s);
};
#endif // FRMMAIN_H
frmmain.cpp代码:
#include "frmmain.h"
#include "ui_frmmain.h"
frmmain::frmmain(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmmain)
{
ui->setupUi(this);
}
frmmain::~frmmain()
{
delete ui;
}
//图片转数据
void frmmain::picToData(QString fromPic, QString toData, QString myformat)
{
QImage img(fromPic);
QByteArray ba;
QBuffer buf(&ba);
img.save(&buf,"JPG");//按照JPG解码保存数据
QByteArray cc=qCompress(ba,1);
QByteArray hh;
if (myformat=="Hex")
{
hh=cc.toHex();//16进制数据
}
else
{
hh=cc.toBase64();//base64数据
}
QString str(hh);
QFile file(toData);
if (file.open(QFile::WriteOnly))
{
file.write(str.toLatin1());
file.close();
}
}
//数据转图片
void frmmain::dataToPic(QString fromData, QString toPic, QString myformat)
{
QFile file(fromData);
if (file.open(QFile::ReadOnly))
{
QByteArray read=file.readAll();
file.close();
QString ss=QString::fromLatin1(read.data(),read.size());
QByteArray rc;
if (myformat=="Hex")
{
rc=QByteArray::fromHex(ss.toAscii());
}
else
{
rc=QByteArray::fromBase64(ss.toAscii());
}
QByteArray rdc=qUncompress(rc);
QImage img;
img.loadFromData(rdc);
img.save(toPic);
}
}
QString frmmain::getFileName(QString filter)
{
QString fileName;
fileName=QFileDialog::getOpenFileName(this,tr("选择文件"),QCoreApplication::applicationDirPath(),filter);
if (!fileName.isEmpty())
{
return fileName;
}
else
{
return "";
}
}
QStringList frmmain::getFileNames(QString filter)
{
QStringList fileNames;
fileNames=QFileDialog::getOpenFileNames(this,tr("选择文件"),QCoreApplication::applicationDirPath(),filter);
if (!fileNames.isEmpty())
{
return fileNames;
}
else
{
return fileNames;
}
}
void frmmain::on_btn1_clicked()
{
ui->pic->setText(this->getFileName(tr("图片文件(*.jpg)")));
}
void frmmain::on_btn2_clicked()
{
ui->txt->setText(this->getFileName(tr("文本文件(*.txt)")));
}
void frmmain::on_btnPicToData_clicked()
{
if (ui->pic->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择图片文件"));
return;
}
if (ui->txt->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择文本文件"));
return;
}
if (ui->rbtn16->isChecked())
{
this->picToData(ui->pic->text(),ui->txt->text(),"Hex");
}
else
{
this->picToData(ui->pic->text(),ui->txt->text(),"Base64");
}
}
void frmmain::on_btnDataToPic_clicked()
{
if (ui->pic->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择图片文件"));
return;
}
if (ui->txt->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择文本文件"));
return;
}
if (ui->rbtn16->isChecked())
{
this->dataToPic(ui->txt->text(),ui->pic->text(),"Hex");
}
else
{
this->dataToPic(ui->txt->text(),ui->pic->text(),"Base64");
}
}
void frmmain::on_btnSelectFile_clicked()
{
ui->listWidget->addItems(this->getFileNames(tr("图片文件(*.jpg)")));
}
void frmmain::delayTime(int s)
{
QTime t=QTime::currentTime().addMSecs(s);
while(QTime::currentTime()t)
{
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
}
void frmmain::on_btnSelectTxt_clicked()
{
ui->listWidget->addItems(this->getFileNames(tr("文本文件(*.txt)")));
}
void frmmain::on_btnChangeToTxt_clicked()
{
QListWidgetItem *myitem;
if (ui->listWidget->count()==0)
{
return;
}
for (int i=0;iui->listWidget->count();i++)
{
QString timeNow=QTime::currentTime().toString("hhmmss");
QString dateNow=QDate::currentDate().toString("yyyyMMdd");
QString saveFileName=QCoreApplication::applicationDirPath()+tr("/imagedata/%1%2.txt").arg(dateNow,timeNow);
myitem=ui->listWidget->item(i);
if (ui->rbtn16->isChecked())
{
this->picToData(myitem->text(),saveFileName,"Hex");
}
else
{
this->picToData(myitem->text(),saveFileName,"Base64");
}
this->delayTime(1000);//为了确保命名不冲突和转换完成,延时1秒
}
QMessageBox::information(this,tr("提示"),tr("恭喜你!转换完成!"));
}
void frmmain::on_btnChangeToJpg_clicked()
{
QListWidgetItem *myitem;
if (ui->listWidget->count()==0)
{
return;
}
for (int i=0;iui->listWidget->count();i++)
{
QString timeNow=QTime::currentTime().toString("hhmmss");
QString dateNow=QDate::currentDate().toString("yyyyMMdd");
QString saveFileName=QCoreApplication::applicationDirPath()+tr("/imageto/%1%2.jpg").arg(dateNow,timeNow);
myitem=ui->listWidget->item(i);
if (ui->rbtn16->isChecked())
{
this->dataToPic(myitem->text(),saveFileName,"Hex");
}
else
{
this->dataToPic(myitem->text(),saveFileName,"Base64");
}
this->delayTime(1000);//为了确保命名不冲突和转换完成,延时1秒
}
QMessageBox::information(this,tr("提示"),tr("恭喜你!转换完成!"));
}
#include "ui_frmmain.h"
frmmain::frmmain(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmmain)
{
ui->setupUi(this);
}
frmmain::~frmmain()
{
delete ui;
}
//图片转数据
void frmmain::picToData(QString fromPic, QString toData, QString myformat)
{
QImage img(fromPic);
QByteArray ba;
QBuffer buf(&ba);
img.save(&buf,"JPG");//按照JPG解码保存数据
QByteArray cc=qCompress(ba,1);
QByteArray hh;
if (myformat=="Hex")
{
hh=cc.toHex();//16进制数据
}
else
{
hh=cc.toBase64();//base64数据
}
QString str(hh);
QFile file(toData);
if (file.open(QFile::WriteOnly))
{
file.write(str.toLatin1());
file.close();
}
}
//数据转图片
void frmmain::dataToPic(QString fromData, QString toPic, QString myformat)
{
QFile file(fromData);
if (file.open(QFile::ReadOnly))
{
QByteArray read=file.readAll();
file.close();
QString ss=QString::fromLatin1(read.data(),read.size());
QByteArray rc;
if (myformat=="Hex")
{
rc=QByteArray::fromHex(ss.toAscii());
}
else
{
rc=QByteArray::fromBase64(ss.toAscii());
}
QByteArray rdc=qUncompress(rc);
QImage img;
img.loadFromData(rdc);
img.save(toPic);
}
}
QString frmmain::getFileName(QString filter)
{
QString fileName;
fileName=QFileDialog::getOpenFileName(this,tr("选择文件"),QCoreApplication::applicationDirPath(),filter);
if (!fileName.isEmpty())
{
return fileName;
}
else
{
return "";
}
}
QStringList frmmain::getFileNames(QString filter)
{
QStringList fileNames;
fileNames=QFileDialog::getOpenFileNames(this,tr("选择文件"),QCoreApplication::applicationDirPath(),filter);
if (!fileNames.isEmpty())
{
return fileNames;
}
else
{
return fileNames;
}
}
void frmmain::on_btn1_clicked()
{
ui->pic->setText(this->getFileName(tr("图片文件(*.jpg)")));
}
void frmmain::on_btn2_clicked()
{
ui->txt->setText(this->getFileName(tr("文本文件(*.txt)")));
}
void frmmain::on_btnPicToData_clicked()
{
if (ui->pic->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择图片文件"));
return;
}
if (ui->txt->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择文本文件"));
return;
}
if (ui->rbtn16->isChecked())
{
this->picToData(ui->pic->text(),ui->txt->text(),"Hex");
}
else
{
this->picToData(ui->pic->text(),ui->txt->text(),"Base64");
}
}
void frmmain::on_btnDataToPic_clicked()
{
if (ui->pic->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择图片文件"));
return;
}
if (ui->txt->text()=="")
{
QMessageBox::information(this,tr("提示"),tr("请选择文本文件"));
return;
}
if (ui->rbtn16->isChecked())
{
this->dataToPic(ui->txt->text(),ui->pic->text(),"Hex");
}
else
{
this->dataToPic(ui->txt->text(),ui->pic->text(),"Base64");
}
}
void frmmain::on_btnSelectFile_clicked()
{
ui->listWidget->addItems(this->getFileNames(tr("图片文件(*.jpg)")));
}
void frmmain::delayTime(int s)
{
QTime t=QTime::currentTime().addMSecs(s);
while(QTime::currentTime()t)
{
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
}
void frmmain::on_btnSelectTxt_clicked()
{
ui->listWidget->addItems(this->getFileNames(tr("文本文件(*.txt)")));
}
void frmmain::on_btnChangeToTxt_clicked()
{
QListWidgetItem *myitem;
if (ui->listWidget->count()==0)
{
return;
}
for (int i=0;iui->listWidget->count();i++)
{
QString timeNow=QTime::currentTime().toString("hhmmss");
QString dateNow=QDate::currentDate().toString("yyyyMMdd");
QString saveFileName=QCoreApplication::applicationDirPath()+tr("/imagedata/%1%2.txt").arg(dateNow,timeNow);
myitem=ui->listWidget->item(i);
if (ui->rbtn16->isChecked())
{
this->picToData(myitem->text(),saveFileName,"Hex");
}
else
{
this->picToData(myitem->text(),saveFileName,"Base64");
}
this->delayTime(1000);//为了确保命名不冲突和转换完成,延时1秒
}
QMessageBox::information(this,tr("提示"),tr("恭喜你!转换完成!"));
}
void frmmain::on_btnChangeToJpg_clicked()
{
QListWidgetItem *myitem;
if (ui->listWidget->count()==0)
{
return;
}
for (int i=0;iui->listWidget->count();i++)
{
QString timeNow=QTime::currentTime().toString("hhmmss");
QString dateNow=QDate::currentDate().toString("yyyyMMdd");
QString saveFileName=QCoreApplication::applicationDirPath()+tr("/imageto/%1%2.jpg").arg(dateNow,timeNow);
myitem=ui->listWidget->item(i);
if (ui->rbtn16->isChecked())
{
this->dataToPic(myitem->text(),saveFileName,"Hex");
}
else
{
this->dataToPic(myitem->text(),saveFileName,"Base64");
}
this->delayTime(1000);//为了确保命名不冲突和转换完成,延时1秒
}
QMessageBox::information(this,tr("提示"),tr("恭喜你!转换完成!"));
}