1. 打开文件夹
voidMainWindow::open()
{
QFileDialog*fd=newQFileDialog(this);
fd->setWindowTitle(tr("OpenFolder"));
fd->setDirectory("./");
fd->setFileMode(QFileDialog::DirectoryOnly);
QStringListfileName;
if(fd->exec()==QDialog::Accepted)
{
fileName=fd->selectedFiles();
picManger.addFolderImages(fileName.at(0));
picPlayer.setFileList(picManger.getFileList());
qDebug()<
}
}
#include"picmanager.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
PicManager::PicManager(QObject*parent):
QObject(parent)
{
}
QStringListPicManager::getFileList()
{
returnthis->newFileList;
}
voidPicManager::addFolderImages(QStringpath)
{
//判断路径是否存在
QDirdir(path);
if(!dir.exists())
{
qDebug()<
return;
}
QStringnewDir=createNewDir(path);
if(newDir==""){
qDebug()<
return;
}
addImgPaths2FileList(path);
compressAndCopyImgs2NewPath(newDir);
}
voidPicManager::compressAndCopyImgs2NewPath(QStringnewPath)
{
newFileList.clear();
foreach(QStringstr,fileList){
intindex=str.lastIndexOf("/");
QStringfileName=str.right(str.length()-index);
QStringdesFilePath=newPath+fileName;
//qDebug()<
compressImg(str,desFilePath);
}
}
QStringPicManager::createNewDir(QStringoldPath)
{
QDirprodir;
intindex=oldPath.lastIndexOf("/");
QStringnewDir=prodir.currentPath()+oldPath.right(oldPath.length()-index);
QDirdir(newDir);
if(dir.exists())
{
qDebug()<
returnnewDir;
}elseif(dir.mkdir(newDir)){
returnnewDir;
qDebug()<
}
return"";
}
voidPicManager::compressImg(QStringsrcFilePath,QStringdesFilePath)
{
qDebug()<
QFiledesFile(desFilePath);
if(desFile.exists()){
newFileList.append(desFilePath);
qDebug()<
return;
}
QImageimg;
img.load(srcFilePath);
QImageresult=img.scaled(800,600,Qt::KeepAspectRatio,Qt::FastTransformation);//.scaled(260,140,Qt::KeepAspectRatio,Qt::SmoothTransformation);
boolisSuccess=result.save(desFilePath,"JPEG",100);
if(!isSuccess)
{
qDebug()<
}
QFilefile(desFilePath);
qint64fsz=file.size();
intquality=100;
while(fsz>1024*512)
{
quality=quality-5;
isSuccess=result.save(desFilePath,"JPEG",quality);
if(!isSuccess)
{
qDebug()<
}
fsz=file.size();
qDebug()<
if(quality<=0)
break;
}
newFileList.append(desFilePath);
}
voidPicManager::addImgPaths2FileList(QStringpath)
{
QDirdir(path);
dir.setFilter(QDir::Files);//|QDir::NoSymLinks
QFileInfoListinfolist=dir.entryInfoList();
intfile_count=infolist.count();
if(file_count<=0)
{
return;
}
//QStringListstring_list;
fileList.clear();
for(inti=0;i
{
QFileInfofile_info=infolist.at(i);
QStringsuffix=file_info.suffix();
if(QString::compare(suffix,QString("JPG"),Qt::CaseInsensitive)==0)
{
QStringabsolute_file_path=file_info.absoluteFilePath();
qDebug()<
//QFilefile(absolute_file_path);
//file.rename(i>9?i+".jpg":"0"+i+".jpg");
fileList.append(absolute_file_path);
}
}
}
3.播放
picManger.addFolderImages("/Users/ljx/imgs");
picPlayer.initPalySreen(ui->imgShowLbl);
picPlayer.setFileList(picManger.getFileList());
picPlayer.startPlay(18);
#include"picplayer.h"
#include
#include
#include
#include
#include
#include
PicPlayer::PicPlayer(QObject*parent):
QObject(parent)
{
pThread=newPicPlayThread;
qThread=newQThread(this);
pThread->moveToThread(qThread);
//startthread
connect(this,&PicPlayer::startPlaySignals,pThread,&PicPlayThread::threadTimeOut);
connect(pThread,&PicPlayThread::sendSignal,this,&PicPlayer::dealSignal);
}
voidPicPlayer::initPalySreen(QLabel*showLbl)
{
this->showLbl=showLbl;
}
voidPicPlayer::setFileList(QStringListfileList)
{
this->fileList=fileList;
showFirstPic();
}
voidPicPlayer::showFirstPic()
{
if(fileList.size()<=0){
return;
}
currentIndex=0;
this->playNext();
}
voidPicPlayer::startPlay(intnumPSecond=18)
{
this->fileList=fileList;
if(fileList.size()<=0){
return;
}
if(qThread->isRunning()){
qDebug()<isRunning()return";
return;
}
qDebug()<playSignal";
pThread->setRunning(true);
pThread->setNumPeSecond(numPSecond);
//startthread
qThread->start();
emitstartPlaySignals();
}
voidPicPlayer::stopPlay()
{
currentIndex=0;
pThread->setRunning(false);
qThread->quit();
qThread->wait();
}
voidPicPlayer::dealSignal()
{
qDebug()<
if(currentIndex==fileList.size()-1){
//this->initUI();
this->stopPlay();
return;
}
this->playNext();
}
voidPicPlayer::playNext()
{
QStringfilePath=fileList.at(currentIndex);
showPicInLbl(filePath);
}
voidPicPlayer::showPicInLbl(QStringfilePath)
{
QDateTimetime=QDateTime::currentDateTime();//获取系统现在的时间
QStringstr=time.toString("yyyy-MM-ddhh:mm:sszzzddd");//设置显示格式
qDebug()<
QImageimage;
image.load(filePath);
qDebug()<
image=image.scaled(QSize(800,600),Qt::KeepAspectRatio);
qDebug()<
showLbl->setPixmap(QPixmap::fromImage(image));
showLbl->update();
currentIndex+=1;
QDateTimetime2=QDateTime::currentDateTime();//获取系统现在的时间
QStringstr2=time2.toString("yyyy-MM-ddhh:mm:sszzzddd");//设置显示格式
qDebug()<
}
#include"picplaythread.h"
#include"QThread"
#include
PicPlayThread::PicPlayThread(QObject*parent):
QObject(parent)
{
this->onRunning=false;
this->numPeSecond=18;
}
voidPicPlayThread::threadTimeOut()
{
while(onRunning){
qDebug()<sendSignal";
emitsendSignal();
QThread::msleep(1000/(this->numPeSecond));
}
if(!onRunning){
this->onRunning=false;
this->numPeSecond=18;
}
}
voidPicPlayThread::setRunning(boolrun=false)
{
this->onRunning=run;
}
voidPicPlayThread::setNumPeSecond(intnum=18)
{
this->numPeSecond=num;
}
下一步就是要研究将图片制作视频:暂没有参考:只找到一个IOS的文章:http://blog.csdn.net/willib/article/details/53858035