[英]How can i suppport both Unicode and Multi-Byte Character Set in my code?
[英]No mapping for the Unicode character in target multi-byte code page C++
我的代碼過去工作得很好,我只是實現了一些線程,現在它壞了,我發現問題出在我專門用於這些代碼塊的變量 image_path 中:
//Global vars
namespace fs = std::filesystem;
std::vector<fs::path> image_path; //Vector of paths in which to store all paths to the images
編輯發現如果我打印 el.path().String() 它會隨機崩潰
for (const auto& el : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied))
{
if (is_directory(el) != true)
{
if ((el.path().extension().compare(".jpg")) == 0 || (el.path().extension().compare(".jpeg")) == 0 || (el.path().extension().compare(".png")) == 0)
{
image_path.push_back(el.path());
count++;
}
}
}
void hash_function(int start_point)
{
int counter = 0;
try
{
long int hasher = 1; //Store the hashed value of an image
std::unordered_map<long int, int>::iterator it; //Iterator for hashmap
for (int i = start_point; i < image_path.size(); i += THREADS)
{
std::cout << "Count = " << counter << "\n";
cv::Mat processing_image = cv::imread(image_path[i].string(), cv::IMREAD_COLOR); //had to change from image_path[i].u8string() to .string() due to a library update
hasher = 1; //Must reset hasher to 1
counter++;
if (processing_image.dims != 0)
{
cv::Mat gray_image; //Define gray image to create gray scale images
cv::cvtColor(processing_image, gray_image, cv::COLOR_BGR2GRAY);
cv::resize(gray_image, gray_image, SIZE);
for (int j = 0; j < (gray_image.cols - 1); j++)
{
for (int l = 0; l < gray_image.rows; l++)
{
hasher = hasher + gray_image.at<uchar>(l, j); //will leave it like this for now, seems to work
}
hasher = hasher * hasher; //just to make hasher even more unique
}
//Mutex this bad boi
mtx.lock();
//Extremely important things here
if (hashmap.empty() == true)
{
hashmap[hasher] = i;
//Add hash value to each path (path is a number of the position of a path in the vector image_path)
}
else
{
//Solved the counting error by simply skipping the already in check
it = hashmap.find(hasher);
if (it != hashmap.end())
{
repeated.push_back(image_path[it->second]);
count++;
repeated.push_back(image_path[i]);
count++;
}
else
{
//Add hash value to each path (path is a number of the position of a path in the vector image_path)
hashmap[hasher] = i;
}
}
mtx.unlock();
//No more important thingys :D
}else
{
hasher = -1;
}
}
}catch(std::exception & e)
{
std::cout << e.what();
exit(0);
}
}
hash_function在返回錯誤之前正好執行 58 次迭代,打印為: No mapping for the Unicode character in target multi-byte code page我已經看到錯誤是 UTF-8 和 ANSI 轉換錯誤,但我找不到任何方法為了解決這個問題,我不能使用 u8string() 而不是 string(),因為 OpenCV 庫需要一個 string()。
解決方案:不要使用 Windows 附帶的愚蠢的官方文件系統庫,只需使用 boost 版本https://www.boost.org/doc/libs/1_73_0/index.html 。 它只是更快,錯誤更少
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.