二维码识别:Halcon与C++中多字节环境下的字节编码格式设置和转换

Halcon环境下可通过设置set_system(‘filename_encoding’, ‘utf8’),可以将二维码的识别结果解析出汉字。

VS环境下则需要将utf8转换成gbk格式。代码如下:

int UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen)

{

 wchar_t * lpUnicodeStr = NULL;

 int nRetLen = 0;

 

 if(!lpUTF8Str)  //如果UTF8字符串为NULL则出错退出

         return 0;

 

 nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,NULL);  //获取转换到Unicode编码后所需要的字符空间长度

 lpUnicodeStr = new WCHAR[nRetLen + 1];  //为Unicode字符串空间

 nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen);  //转换到Unicode编码

 if(!nRetLen)  //转换失败则出错退出

         return 0;

 

 nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,NULL,NULL,NULL);  //获取转换到GBK编码后所需要的字符空间长度

 

 if(!lpGBKStr)  //输出缓冲区为空则返回转换后需要的空间大小

 {

         if(lpUnicodeStr)

                 delete []lpUnicodeStr;

         return nRetLen;

 }

 

 if(nGBKStrLen < nRetLen)  //如果输出缓冲区长度不够则退出

 {

         if(lpUnicodeStr)

                 delete []lpUnicodeStr;

         return 0;

 }

 

 nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL);  //转换到GBK编码

 

 if(lpUnicodeStr)

         delete []lpUnicodeStr;

 

 return nRetLen;

}

posted @ 2016-01-07 10:44  Jane_bai  阅读(1381)  评论(0编辑  收藏  举报