开发环境: Qt5.7 Creator + OpenCV2.4.9(自己编译的);
问题:imread中文路径无法成功,英文路径就可以。
解决办法1:
cv::Mat image ;
QFile file(strFilePath); //strFilePath 路径
if(file.open(QIODevice::ReadOnly ))
{
QByteArray byteArray = file.readAll();
std::vector<char> data(byteArray.data(),byteArray.data()+ byteArray.size());
image = cv::imdecode(cv::Mat(data),1); //>0 Return a 3-channel color image
file.close();
}
解决办法2:
QString strPath = 中文路径地址
std::string path = strPath.toLocal8Bit().toStdString(); //关键是这个
cv::Mat image = cv::imread(path,1 ); //成功了
解决办法3://Unicode转码为UTF-8
void UNICODE_to_UTF8(CStringW& unicodeString, std::string& str)
{
int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), NULL, 0, NULL, NULL);
char* buffer = new char[stringLength + 1];
::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), buffer, stringLength, NULL, NULL);
buffer[stringLength] = '\0';
str = buffer;
delete[] buffer;
}
办法3 参考自文章http://blog.csdn.net/leixiaohua1020/article/details/42363593, 我还没有测试