osgEarth的中文乱码问题解决

osgEarth的中文乱码问题解决

1.osg解决方法:

osgDB自带一个转换函数

std::string beiJingStr = "北京";
std::string str = osgDB::convertStringFromCurrentCodePageToUTF8(beiJingStr);

2.qt解决方法:

qt中可以使用QString::fromLocal8Bit或者QStringLiteral

QString beiJingStr = QString::fromLocal8Bit("北京");
//QString beiJingStr = QStringLiteral("北京");
std::string str = beiJingStr.toStdString();

3.重点来了

textstyle编码方式设置为utf8,不设置的话还是不能成功转换

textStyle->encoding() = osgEarth::TextSymbol::Encoding::ENCODING_UTF8;

4.添加地标代码

void addLabel()//添加地标
{
	osgEarth::Style style;
	osgEarth::TextSymbol* textStyle = style.getOrCreateSymbol<osgEarth::TextSymbol>();
	//设置颜色
	textStyle->fill()->color() = osg::Vec4f(1.0, 1.0, 1.0,0.5);
	//设置边框
	//textStyle->halo()->color()= osg::Vec4f(1.0, 1.0, 1.0, 1.0);

	QString fontPath = QCoreApplication::applicationDirPath() + "./data/fonts/simkai.ttf";
	textStyle->font()= fontPath.toStdString();
	textStyle->size() = 20.0;
	textStyle->encoding() = osgEarth::TextSymbol::Encoding::ENCODING_UTF8;

	QString ChinaImagePath = QCoreApplication::applicationDirPath() + "./data/icons/China.jpg";
	osg::Image* ChinaImage = osgDB::readImageFile(ChinaImagePath.toStdString());
	ChinaImage->scaleImage(100, 50, 1);
	
	osgEarth::GeoPoint beiJingpos(mapNode->getMapSRS(), 116.2, 39.56, 0.0);

	//test 设置中文
	/*
	1.qt的方法 设置QString::fromLocal8Bit或者QStringLiteral
	2.osg自带转换方法 osgDB::convertStringFromCurrentCodePageToUTF8()
	*/
	//QString beiJingStr = QString::fromLocal8Bit("北京");
	std::string beiJingStr = "北京";
	std::string str = osgDB::convertStringFromCurrentCodePageToUTF8(beiJingStr);
	osgEarth::PlaceNode* pn = new osgEarth::PlaceNode(beiJingpos, str,style,ChinaImage);
	root->addChild(pn);
}

5.结果展示

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值