c++opencvunicodewxwidgetschinese-locale

How to handle Chinese characters in a OpenCV-WxWidgets based software


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.

IDE screenshot showing the value of the wxFileName variable is as expected

[UPDATE] I also tried what is mentioned in Cross-platform file names and wxString::ToStdString()

So

  1. Setting the locale when the application starts:
    setlocale(LC_ALL, "C.UTF-8") or std::locale::global(std::locale(""))

  2. Calling wxString::ToStdString(wxMBConvUTF8()) when converting from wxString to std::string.

  3. It doesn't work. For this filename 沙发模型12_0017.jpg I get this garbage string: æ²™å‘æ¨¡åž‹12_0017.jpg


Solution

  • So i managed to have it work. I decided to use std::wstring instead of std::string
    So there is 2 step .

    1. Convert wxString to std::wstring
      const std::wstring fileName = wxFileName.ToStdWstring();

    2. Read file using OpenCV. Done using this answer
      Basically the file bytes are loaded in RAM then fed to cv::imdecode