GuiThumbnail.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "GuiThumbnail.h"
  2. #include <string.h>
  3. #include <wchar.h>
  4. #include <thread>
  5. void processFile(const std::wstring& srcFile, const std::wstring& thumbFile)
  6. {
  7. // AI EPS INDD 文件导出缩略图
  8. bool ret = AdobeThumbnail_W(srcFile.c_str(), thumbFile.c_str());
  9. // CorelDRAW CDR 文件导出缩略图
  10. if (!ret)
  11. ret = CorelThumbnail_W(srcFile.c_str(), thumbFile.c_str());
  12. // 可以在此处添加处理成功或失败的逻辑
  13. if (!ret){
  14. char bad_File[MAX_PATH] = "001_Bad_Filelist.txt";
  15. FILE * pf = fopen(bad_File, "a+");
  16. WCHARTochar(bad_File, srcFile.c_str());
  17. fprintf(pf, "Name: %s\n", bad_File);
  18. fclose(pf);
  19. }
  20. }
  21. // 执行提取缩略图 主功能
  22. bool GuiThumbnail(const wchar_t* keyWord, const wchar_t* savePath)
  23. {
  24. wchar_t src_filename[MAX_PATH] = {0};
  25. wchar_t thumb_filename[MAX_PATH] = {0};
  26. Everything_SetSearch(keyWord);
  27. if (false == Everything_Query(TRUE))
  28. return false;
  29. std::vector<std::wstring> vec_files;
  30. std::vector<std::wstring> vec_names;
  31. const wchar_t* rs = L"(.+)(\\.(?:ai|AI|indd|INDD|Indd|eps|EPS|Eps|pdf|PDF|cdr|CDR|Cdr))"; // 正则字符串,exp开始的单词
  32. std::wregex expression(rs); // 字符串传递给构造函数,建立正则表达式
  33. for (int i = 0 ; i < Everything_GetNumResults(); i++) {
  34. bool ret = std::regex_match((wchar_t*)Everything_GetResultFileName(i), expression);
  35. if (ret) {
  36. Everything_GetResultFullPathNameW(i, src_filename, MAX_PATH);
  37. vec_files.push_back(src_filename);
  38. vec_names.push_back((wchar_t*)Everything_GetResultFileName(i));
  39. }
  40. }
  41. // 如果没有目录建立,进入目录
  42. _wmkdir(savePath);
  43. _wchdir(savePath);
  44. // // 文件导出缩略图
  45. // for (int i = 0 ; i != vec_files.size(); i++) {
  46. // wcscpy(src_filename, vec_files[i].c_str());
  47. // wcscpy(thumb_filename, vec_names[i].c_str());
  48. // wcscat(thumb_filename, L"_T.jpg");
  49. // // AI EPS INDD 文件导出缩略图
  50. // bool ret = AdobeThumbnail_W(src_filename, thumb_filename);
  51. // if (!ret) // CorelDRAW CDR 文件导出缩略图
  52. // ret = CorelThumbnail_W(src_filename, thumb_filename);
  53. // }
  54. const int trn = 8;
  55. std::vector<std::thread> threads;
  56. while (vec_files.size() > trn) {
  57. for (int i = 0; i < trn; i++) {
  58. const std::wstring& srcFilename = vec_files[i];
  59. const std::wstring& thumbFilename = vec_names[i] + L"_T.jpg";
  60. threads.emplace_back(std::thread(processFile, srcFilename, thumbFilename));
  61. }
  62. // 等待所有线程执行完毕
  63. for (auto& trd : threads) trd.join();
  64. threads.clear();
  65. vec_files.erase(vec_files.begin(), vec_files.begin() + trn);
  66. vec_names.erase(vec_names.begin(), vec_names.begin() + trn);
  67. }
  68. for (int i = 0; i < vec_files.size(); i++) {
  69. const std::wstring& srcFilename = vec_files[i];
  70. const std::wstring& thumbFilename = vec_names[i] + L"_T.jpg";
  71. threads.emplace_back(std::thread(processFile, srcFilename, thumbFilename));
  72. }
  73. // 等待所有线程执行完毕
  74. for (auto& trd : threads) trd.join();
  75. return true;
  76. }
  77. // 临时PNG显示文件
  78. bool Thumbnail_TempPng(const wchar_t* src_filename, const wchar_t* tmppng)
  79. {
  80. bool ret = AdobeThumbnail_W(src_filename, tmppng);
  81. if (!ret)
  82. ret = CorelThumbnail_W(src_filename, tmppng);
  83. return ret;
  84. }