1 #include <windows.h>
 2 #include <iostream>
 3 #include <opencv2/opencv.hpp>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 char* WcharToChar(const wchar_t* wp)
 8 {
 9     char *m_char;
10     int len = WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
11     m_char = new char[len + 1];
12     WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), m_char, len, NULL, NULL);
13     m_char[len] = '\0';
14     return m_char;
15 }
16 
17 wchar_t* CharToWchar(const char* c)
18 {
19     wchar_t *m_wchar;
20     int len = MultiByteToWideChar(CP_ACP, 0, c, strlen(c), NULL, 0);
21     m_wchar = new wchar_t[len + 1];
22     MultiByteToWideChar(CP_ACP, 0, c, strlen(c), m_wchar, len);
23     m_wchar[len] = '\0';
24     return m_wchar;
25 }
26 
27 wchar_t* StringToWchar(const string& s)
28 {
29     const char* p = s.c_str();
30     return CharToWchar(p);
31 }
32 int main()
33 {
34     //定义文件格式
35     const string file_form = "*.jpg";
36     const string filePath = "M";
37     string file_ReadPath = filePath + "/" + file_form;
38     HANDLE hFile;
39     //指定搜索目录和文件类型,如搜索d盘的音频文件可以是"D:\\*.mp3"
40     LPCTSTR lpFileName = StringToWchar(file_ReadPath);
41     //搜索得到的文件信息将储存在pNextInfo中;
42     WIN32_FIND_DATA pNextInfo;
43     //请注意是 &pNextInfo , 不是 pNextInfo;
44     hFile = FindFirstFile(lpFileName, &pNextInfo);
45     int j = 0;
46     if (hFile == INVALID_HANDLE_VALUE)
47     {
48         return false;//搜索失败
49     }
50     do
51     {
52         //必须加这句,不然会加载.和..的文件而加载不了图片,
53         if (pNextInfo.cFileName[0] == '.')//过滤.和..
54             continue;
55         j++;//读取一张图
56         //wcout<<pNextInfo.cFileName<<endl;
57         printf("%s\n", WcharToChar(pNextInfo.cFileName));
58         //对读入的图片进行处理
59         Mat srcImage = imread(filePath + "\\" + WcharToChar(pNextInfo.cFileName), CV_LOAD_IMAGE_GRAYSCALE);
60         if (1)
61         {
62             stringstream ss;
63             ss << "h" << j << ".png";
64             imwrite(ss.str(), srcImage);
65         }
66 
67     } while (FindNextFile(hFile, &pNextInfo) && j<6);
68     
69 
70 }