C++搜索文件夹内所有图像并按顺序播放

在文件夹内搜索所有指定格式的文件内容,并按顺序播放。本程序中以图像为例演示。
主要用到_finddata_t、_findfirst、_findnext、_findclose等结构体( _finddata_t )或函数(其他三个)。他们都属于io.h头文件。
首先是头文件库如下:
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. #include "string"  
  4. #include <vector>  
  5. #include <io.h>  
  6. #include <opencv2/opencv.hpp>  
  7.   
  8. using namespace cv;  
  9. using namespace std;  
主函数,实现操作:
  1. int _tmain(int argc, _TCHAR* argv[])  
  2. {  
  3.     // 图像文件夹  
  4.     string file_path = "D:\\image\\";  
  5.   
  6.     // 图像文件路径  
  7.     string search_path = file_path + "*.png";  
  8.     vector<string> file_list;
  9.     // 搜索指定路径下的所有.png文件  
  10.     if (!get_filelist_from_dir(search_path, file_list))  
  11.     {  
  12.         cout << "open file error!" << endl;  
  13.     }  
  14.       
  15.     for (int i = 0 ; i < file_list.size(); i++)  
  16.     {  
  17.         string dstimage_path = file_path + file_list[i];  
  18.            
  19.         Mat Image = imread(dstimage_path);  
  20.           
  21.         /*-------------------------------------------------*/  
  22.                   
  23.                 /*可以在次数插入对图像的其他操作*/  
  24.   
  25.         /*-------------------------------------------------*/  
  26.   
  27.         // 显示  
  28.         imshow("【显示图像】", Image);   
  29.            
  30.         char ckey = waitKey(5);  
  31.   
  32.         // 按下Esc键则退出程序    
  33.         if(ckey == 27)    
  34.         {    
  35.             return 0;    
  36.         }    
  37.           
  38.     }  
  39.   
  40.     return 0;  
  41. }  
对于路境内的指定文件,这里是用的.png的图像,当然你也可以用这个方法来处理诸如.bmp、.jpg等其他图像;也可以是用这种方式来处理.txt等非图像文件,套路是不变的!
其中的bool get_filelist_from_dir(string path, vector<string>& files);函数实现文件寻找功能,如下:
  1. bool get_filelist_from_dir(string path, vector<string>& files)  
  2. {  
  3.     long   hFile   =   0;  
  4.     struct _finddata_t fileinfo;  
  5.     files.clear();  
  6.     if((hFile = _findfirst(path.c_str(), &fileinfo)) !=  -1)  
  7.     {  
  8.         do  
  9.         {  
  10.             if(!(fileinfo.attrib &  _A_SUBDIR))  
  11.                 files.push_back(fileinfo.name);  
  12.         }  
  13.         while(_findnext(hFile, &fileinfo)  == 0);  
  14.   
  15.         _findclose(hFile);  
  16.         return true;  
  17.     }  
  18.     else  
  19.         return false;  
  20. }  
这里面也没什么难度,只是以前没有见过开头所说的那几个函数的话,确实就不太了解。
对于函数的内容,MSDN就可以查看,不多赘述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值