qt快速加载图片_QT打开图片文件夹快速播放图片

本文介绍了使用QT实现从文件夹快速加载并播放图片的方法。首先通过QFileDialog打开图片文件夹,然后压缩并复制图片到新路径,最后利用PicManager和PicPlayer类实现图片的播放功能,每秒播放18帧。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值