最近的项目需要在图片上写汉字,用了opencv,freetype来实现,但是在Linux下开发遇到了很多的问题,尤其是Puttext函数写连续的汉字时会出现乱码的情况,也是把我折磨的够惨,特此记录。
为啥要用wifstream呢?因为文件中是汉字,占双字节,如果只用ifstream读取的话,在PutText(const char*)这个函数的时候,汉字是不能正常显示的,因此只能用PutText(const wchar_t* ) 这个函数了,也是通过大量的试验去试各种方法,最终解决了乱码问题。
言归正传,直接看怎么读取文件中的汉字吧,并用wstring来表示,可以有wstring.data()来得到const wchar_t* 类型的。
#include<locale>
#include<iostream>
#include<string>
#include<fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <opencv/cv.h>
#include <stdio.h>
#include "normal.h"
#include <time.h>
#include "lib/CvxText.h"
#include <freetype2/ft2build.h>
using namespace std;
int main(){
Mat src = imread("drivelicense1.jpg");
if(src.data == NULL){
cout<<"read image error"<<endl;
return 0;
Mat src1 = src.clone();
CvxText chinese_text("/font/aa.ttf");
CvScalar fsize = cvScalar(38,0.05,0.15,0);
float p = 0.6;
chinese_text.setFont(NULL,&fsize,NULL,&p);
CvScalar color = CV_RGB(0,0,0);
IplImage ipl(src1);
locale china("zh_CN.UTF-8");
wifstream infile;
infile.open("../meta/roads_line.txt");
if(!infile.is_open()){
cout<<"fail to open the file"<<endl;
return 0;
infile.imbue(china);
int i=0;
wstring value;
while(!infile.eof()){
infile >> value;
char coor[256];
sprintf(coor,"%d",i);
imwrite("meta/desimage"+string(coor)+".jpg",src1);
wcout<<value<<endl;
if (infile.eof()) {
cout<<"End of file reached!"<<endl;
else if (infile.fail()){
cout<<"Input terminated by data mismatched!"<<endl;
else{
cout<<"Input terminated for unknown reason!"<<endl;
infile.close();
return 0;
#精简后只读取文件的代码
#include<locale>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
locale china("zh_CN.UTF-8");
wifstream infile;
infile.open("../meta/roads_line.txt");
if(!infile.is_open()){
cout<<"fail to open the file"<<endl;
return 0;
infile.imbue(china);
wstring value;
while(!infile.eof()){
infile >> value;
wcout<<value<<endl;
if (infile.eof()) {
cout<<"End of file reached!"<<endl;
else if (infile.fail()){
cout<<"Input terminated by data mismatched!"<<endl;
else{
cout<<"Input terminated for unknown reason!"<<endl;
infile.close();
return 0;
代码跑通后感觉很简单,但是网上相关wstring 的资料非常少,找了好久,试了很久才达到预期的功能,记录下来,希望以后用到的时候方便查阅。
在网上找了一个多小时,终于找到可以读取Unicode版本的代码,网上的资源真的太混乱了,还是google->stack overlow好啊:
记录代码,转载如下: https://stackoverflow.com/questions/26121782/read-unicode-file-with-special-characters-using-stdwifstream
代码如下:
打开运行框之后输入cmd打开,然后在cmd最上边右键→属性,点开就可以查看当前编码方式,我的电脑是GBK。
2.修改对应的文本文件编码方式。
编辑器是sublime text的修改方法 编辑器是记事本的修改方法
(1)用sublime text的比较简单,打开之后在文件→设置文件编码(reopen with ...
//读取filename文件的内容到wstring
bool ReadFileTowstring(const char *szfile, wstring &content)
CString filename(szfile);
一、文件类型为Unicode
/// 函数功能: 读入文件内容
/// 参考:http://blog.csdn.net/xiaobai1593/article/details/7060730
wstring readFileIntoStringuNNICODE(const char * filename) {
ifstream ifile(fil
本文实例讲述了C#实现xml文件反序列化读入数据到object的方法。分享给大家供大家参考。具体实现方法如下:
public static object DeSerializeFromXmlString(System.Type typeToDeserialize, string xmlString) {
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
MemoryStream memoryStream = new MemoryStream(bytes);
System.Xml.Serialization.
文章目录第 8 章 IO 库8.1 IO 类8.1.1 IO 对象无拷贝或赋值8.1.2 条件状态8.1.3 管理输出缓冲8.2 文件输入输出8.2.1 使用文件流对象8.2.2 文件模式8.3 string 流8.3.1 使用 istringstream8.3.2 使用 ostringstream
第 8 章 IO 库
8.1 IO 类
IO库类型和头文件头文件类型iostreamistream, wistream 从流读取数据ostream, wostream 向
你可以使用以下代码来读入含有中文字符的txt文件,并将文件内容保存为wstring格式: std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wifstream wif("文件名.txt");
std::wstring wstr = converter.from_bytes(
(std::istreambuf_iterator<char>(wif)),
(std::istreambuf_iterator<char>()));
月牙爱学习:
Opencv中traincascade+LBP的训练过程及方法
入梦似凡: