I implemented a C++ rendering software. Users are allowed to import image files inside it.
I want my software to support any possible language in the world. Right now importing for example a Chinese named file will fail.
Source code example. File is named 沙发模型12_0017.jpg:
bool AssetFileDropFileTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &filenames)
{
// 1) Read filename. Seems to be OK.
const wxString& wxFilename = filenames[0];
// 2) Convert to std string. Fails here return empty.
const std::string stdFilename = wxFilename.ToStdString();
// 3) Read image using OpenCV.
const cv::String opencvFilename= stdFilename ;
const cv::Mat image = cv::imread(opencvFilename, cv::IMREAD_UNCHANGED);
/* DO SOMETHING WITH image ...*/
return true;
}
However the wxString internal data seems to properly recognize the file. I am using WxWidgets 3.1.0.
[UPDATE] I also tried what is mentioned in Cross-platform file names and wxString::ToStdString()
So
Setting the locale when the application starts:
setlocale(LC_ALL, "C.UTF-8")
or std::locale::global(std::locale(""))
Calling wxString::ToStdString(wxMBConvUTF8())
when converting from wxString
to std::string
.
It doesn't work. For this filename 沙发模型12_0017.jpg I get this garbage string: æ²™å‘æ¨¡åž‹12_0017.jpg
So i managed to have it work. I decided to use std::wstring instead of std::string
So there is 2 step .
Convert wxString to std::wstring
const std::wstring fileName = wxFileName.ToStdWstring();
Read file using OpenCV. Done using this answer
Basically the file bytes are loaded in RAM then fed to cv::imdecode