QT打开图片文件夹快速播放图片

来源:互联网 发布:易语言登陆器源码 编辑:程序博客网 时间:2024/04/30 01:25

1. 打开文件夹

void MainWindow::open()
{
    QFileDialog* fd = new QFileDialog(this);
      fd->setWindowTitle(tr("Open Folder"));
      fd->setDirectory("./");
      fd->setFileMode( QFileDialog::DirectoryOnly );
      QStringList fileName;
      if ( fd->exec() == QDialog::Accepted )
      {
          fileName = fd->selectedFiles();
          picManger.addFolderImages(fileName.at(0));
          picPlayer.setFileList(picManger.getFileList());
          qDebug() << "....... playNext 3 time:"<< fileName.at(0);
      }
}

2.压缩保存新图片:图片太大加载慢所以要压缩(参考:http://blog.csdn.net/shado_walker/article/details/54023137)

#include "picmanager.h"
#include <QStringList>
#include <QString>
#include <QList>
#include <QPixmap>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QDateTime>
#include <QMutex>
#include <QFile>
PicManager::PicManager(QObject *parent) :
    QObject(parent)
{
}
QStringList PicManager::getFileList()
{
    return this->newFileList;
}
void PicManager::addFolderImages(QString path)
{
    //判断路径是否存在
    QDir dir(path);
    if(!dir.exists())
    {
        qDebug() << "....... addFolderImages !dir.exists() return  path:"<< path;
        return;
    }
    QString newDir = createNewDir(path);
    if(newDir == ""){
        qDebug() << "....... addFolderImages newDir:"<< newDir << "return";
        return;
    }
    addImgPaths2FileList(path);
    compressAndCopyImgs2NewPath(newDir);
}
void PicManager::compressAndCopyImgs2NewPath(QString newPath)
{
   newFileList.clear();
   foreach(QString str, fileList) {
        int index = str.lastIndexOf("/");
        QString fileName =  str.right(str.length()-index);
        QString desFilePath = newPath + fileName;
        //qDebug() << ".......compressAndCopyImgs2NewPath desFilePath:"<< desFilePath;
        compressImg(str,desFilePath);
    }
}
QString PicManager::createNewDir(QString oldPath)
{
    QDir prodir;
    int index = oldPath.lastIndexOf("/");
    QString newDir = prodir.currentPath() + oldPath.right(oldPath.length()-index);
    QDir dir(newDir);
    if(dir.exists())
    {
        qDebug() << "....... createNewDir dir.exists() return  newDir:"<< newDir;
        return newDir;
    }else if(dir.mkdir(newDir)){
        return newDir;
        qDebug() << ".......   path:"<< oldPath << " newDirName:" << newDir ;
    }
    return "";
}
void PicManager::compressImg(QString srcFilePath,QString desFilePath)
{
    qDebug() << ".......compressImg srcFilePath:"<< srcFilePath << " desFilePath" << desFilePath;
    QFile desFile(desFilePath);
    if(desFile.exists()){
        newFileList.append(desFilePath);
        qDebug() << ".......compressImg file exists return";
        return;
    }
    QImage img;
    img.load(srcFilePath);
    QImage result = img.scaled(800, 600, Qt::KeepAspectRatio, Qt::FastTransformation);//.scaled(260, 140, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    bool isSuccess = result.save(desFilePath, "JPEG", 100);
    if (!isSuccess)
    {
        qDebug() << "save image fail!";
    }
    QFile file(desFilePath);
    qint64 fsz = file.size();
    int quality = 100;
    while (fsz > 1024 * 512)
    {
        quality = quality - 5;
        isSuccess = result.save(desFilePath, "JPEG", quality);
        if (!isSuccess)
        {
             qDebug() << "save image fail! quality=" << quality;
        }
         fsz = file.size();
         qDebug() << "+++++++++++++++++++++++quality=" << quality;
         if (quality <= 0)
        break;
    }
    newFileList.append(desFilePath);
}
void PicManager::addImgPaths2FileList(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Files);// | QDir::NoSymLinks
    QFileInfoList infolist = dir.entryInfoList();
    int file_count = infolist.count();
    if(file_count <= 0)
    {
        return;
    }
    //QStringList string_list;
    fileList.clear();
    for(int i=0; i<file_count; i++)
    {
        QFileInfo file_info = infolist.at(i);
        QString suffix = file_info.suffix();
        if(QString::compare(suffix, QString("JPG"), Qt::CaseInsensitive) == 0)
        {
            QString absolute_file_path = file_info.absoluteFilePath();
            qDebug() << "....... addFolderImages absolute_file_path:"<< absolute_file_path;
//            QFile file(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 <QStringList>
#include <QLabel>
#include <QDateTime>
#include <QString>
#include <QFile>
#include <QDebug>
PicPlayer::PicPlayer(QObject *parent) :
    QObject(parent)
{
    pThread = new PicPlayThread;
    qThread = new QThread(this);
    pThread->moveToThread(qThread);
    //start thread
    connect(this,&PicPlayer::startPlaySignals,pThread,&PicPlayThread::threadTimeOut);
    connect(pThread,&PicPlayThread::sendSignal,this,&PicPlayer::dealSignal);
}
void PicPlayer::initPalySreen(QLabel *showLbl)
{
    this->showLbl = showLbl;
}
void PicPlayer::setFileList(QStringList fileList)
{
    this->fileList = fileList;
    showFirstPic();
}
void PicPlayer::showFirstPic()
{
    if(fileList.size()<=0){
        return;
    }
    currentIndex = 0;
    this->playNext();
}
void PicPlayer::startPlay(int numPSecond = 18)
{
    this->fileList = fileList;
    if(fileList.size()<=0){
         return;
    }
    if(qThread->isRunning()){
        qDebug() << "........qThread->isRunning() return";
        return;
    }
    qDebug() << "........-> playSignal";
    pThread->setRunning(true);
    pThread->setNumPeSecond(numPSecond);
    //start thread
    qThread->start();
    emit startPlaySignals();
}
void PicPlayer::stopPlay()
{
    currentIndex = 0;
    pThread->setRunning(false);
    qThread->quit();
    qThread->wait();
}
void PicPlayer::dealSignal()
{
    qDebug() << "....... dealSignal currentIndex:"<< currentIndex;
    if(currentIndex == fileList.size() -1){
        //this->initUI();
        this->stopPlay();
        return;
    }
    this->playNext();
}
void PicPlayer::playNext()
{
    QString filePath = fileList.at(currentIndex);
    showPicInLbl(filePath);
}
void PicPlayer::showPicInLbl(QString filePath)
{
    QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
    QString str = time.toString("yyyy-MM-dd hh:mm:ss zzz ddd"); //设置显示格式
    qDebug() << "....... playNext start time:"<< str << "path:" << filePath;
    QImage image;
    image.load(filePath);
    qDebug() << "....... playNext 2 time:"<< QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz ddd") << "path:" << filePath;
    image = image.scaled(QSize(800,600),Qt::KeepAspectRatio);
    qDebug() << "....... playNext 3 time:"<< QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz ddd") << "path:" << filePath;
    showLbl->setPixmap(QPixmap::fromImage(image));
    showLbl->update();
    currentIndex += 1;
    QDateTime time2 = QDateTime::currentDateTime();//获取系统现在的时间
    QString str2 = time2.toString("yyyy-MM-dd hh:mm:ss zzz ddd"); //设置显示格式
    qDebug() << "....... playNext end  time:"<< str2 << "path:" << filePath;
}

#include "picplaythread.h"
#include "QThread"
#include <QDebug>
PicPlayThread::PicPlayThread(QObject *parent) :
    QObject(parent)
{
    this->onRunning = false;
    this->numPeSecond = 18;
}
void PicPlayThread::threadTimeOut()
{
    while(onRunning){
        qDebug() << "........-> sendSignal";
        emit sendSignal();
        QThread::msleep(1000/(this->numPeSecond));
    }
    if(!onRunning){
        this->onRunning = false;
        this->numPeSecond = 18;
    }
}
void PicPlayThread::setRunning(bool run = false)
{
    this->onRunning = run;
}
void PicPlayThread::setNumPeSecond(int num = 18)
{
    this->numPeSecond = num;
}



代码下载:http://download.csdn.net/detail/casun_li/9808993


下一步就是要研究将图片制作视频:暂没有参考:只找到一个IOS的文章:http://blog.csdn.net/willib/article/details/53858035


0 0
原创粉丝点击