真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C++OpenCV【解決putText不能顯示中文】-創(chuàng)新互聯(lián)

使用cv::putText寫(xiě)中文字符時(shí)輸出結(jié)果為"??????"。。。。。。這怎么能忍?

英吉沙ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!

python方法中可以將opencv圖片轉(zhuǎn)化為PIL,寫(xiě)中文之后再轉(zhuǎn)回opencv格式。

C++方法中通常利用freetype庫(kù)來(lái)實(shí)現(xiàn),freetype打包的win32靜態(tài)庫(kù)可以在C#通過(guò)dll引用進(jìn)行調(diào)用,這需要其版本跟CvxText對(duì)應(yīng),否則會(huì)出現(xiàn)錯(cuò)誤,其在x64平臺(tái)也一樣。

本文采用windows的GDI顯示系統(tǒng)的TrueType字體,沒(méi)有封裝,就兩個(gè)函數(shù),分成h和cpp文件,可以自己編輯文件名和函數(shù)名,也可以直接將cpp的代碼復(fù)制到你需要的程序中。


【putText.h】
#ifndef PUTTEXT_H_
#define PUTTEXT_H_

#include#include#includeusing namespace cv;

void GetStringSize(HDC hDC, const char* str, int* w, int* h);
void putTextHusky(Mat &dst, const char* str, Point org, Scalar color, int fontSize,
	const char *fn = "Arial", bool italic = false, bool underline = false);

#endif // PUTTEXT_H_
【putText.cpp】
#include "putText.h"

void GetStringSize(HDC hDC, const char* str, int* w, int* h)
{
	SIZE size;
	GetTextExtentPoint32A(hDC, str, strlen(str), &size);
	if (w != 0) *w = size.cx;
	if (h != 0) *h = size.cy;
}

void putTextHusky(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline)//后倆參數(shù):斜體,下劃線
{
	CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));

	int x, y, r, b;
	if (org.x >dst.cols || org.y >dst.rows) return;
	x = org.x< 0 ? -org.x : 0;
	y = org.y< 0 ? -org.y : 0;

	LOGFONTA lf;
	lf.lfHeight = -fontSize;
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = 5;
	lf.lfItalic = italic;   //斜體
	lf.lfUnderline = underline; //下劃線
	lf.lfStrikeOut = 0;
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfOutPrecision = 0;
	lf.lfClipPrecision = 0;
	lf.lfQuality = PROOF_QUALITY;
	lf.lfPitchAndFamily = 0;
	strcpy_s(lf.lfFaceName, fn);

	HFONT hf = CreateFontIndirectA(&lf);
	HDC hDC = CreateCompatibleDC(0);
	HFONT hOldFont = (HFONT)SelectObject(hDC, hf);

	int strBaseW = 0, strBaseH = 0;
	int singleRow = 0;
	char buf[1<< 12];
	strcpy_s(buf, str);
	char *bufT[1<< 12];  // 這個(gè)用于分隔字符串后剩余的字符,可能會(huì)超出。
	//處理多行
	{
		int nnh = 0;
		int cw, ch;

		const char* ln = strtok_s(buf, "\n", bufT);
		while (ln != 0)
		{
			GetStringSize(hDC, ln, &cw, &ch);
			strBaseW = max(strBaseW, cw);
			strBaseH = max(strBaseH, ch);

			ln = strtok_s(0, "\n", bufT);
			nnh++;
		}
		singleRow = strBaseH;
		strBaseH *= nnh;
	}

	if (org.x + strBaseW< 0 || org.y + strBaseH< 0)
	{
		SelectObject(hDC, hOldFont);
		DeleteObject(hf);
		DeleteObject(hDC);
		return;
	}

	r = org.x + strBaseW >dst.cols ? dst.cols - org.x - 1 : strBaseW - 1;
	b = org.y + strBaseH >dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;
	org.x = org.x< 0 ? 0 : org.x;
	org.y = org.y< 0 ? 0 : org.y;

	BITMAPINFO bmp = { 0 };
	BITMAPINFOHEADER& bih = bmp.bmiHeader;
	int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));

	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biWidth = strBaseW;
	bih.biHeight = strBaseH;
	bih.biPlanes = 1;
	bih.biBitCount = 24;
	bih.biCompression = BI_RGB;
	bih.biSizeImage = strBaseH * strDrawLineStep;
	bih.biClrUsed = 0;
	bih.biClrImportant = 0;

	void* pDibData = 0;
	HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);

	CV_Assert(pDibData != 0);
	HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);

	//color.val[2], color.val[1], color.val[0]
	SetTextColor(hDC, RGB(255, 255, 255));
	SetBkColor(hDC, 0);
	//SetStretchBltMode(hDC, COLORONCOLOR);

	strcpy_s(buf, str);
	const char* ln = strtok_s(buf, "\n", bufT);
	int outTextY = 0;
	while (ln != 0)
	{
		TextOutA(hDC, 0, outTextY, ln, strlen(ln));
		outTextY += singleRow;
		ln = strtok_s(0, "\n", bufT);
	}
	uchar* dstData = (uchar*)dst.data;
	int dstStep = dst.step / sizeof(dstData[0]);
	unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
	unsigned char* pStr = (unsigned char*)pDibData + x * 3;
	for (int tty = y; tty<= b; ++tty)
	{
		unsigned char* subImg = pImg + (tty - y) * dstStep;
		unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;
		for (int ttx = x; ttx<= r; ++ttx)
		{
			for (int n = 0; n< dst.channels(); ++n) {
				double vtxt = subStr[n] / 255.0;
				int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];
				subImg[n] = cvv >255 ? 255 : (cvv< 0 ? 0 : cvv);
			}

			subStr += 3;
			subImg += dst.channels();
		}
	}

	SelectObject(hDC, hOldBmp);
	SelectObject(hDC, hOldFont);
	DeleteObject(hf);
	DeleteObject(hBmp);
	DeleteDC(hDC);
}

第二個(gè)函數(shù)putTextHusky(),默認(rèn)使用Arial字體,也可以設(shè)置成操作系統(tǒng)中已經(jīng)安裝的字體,如“宋體”、“微軟雅黑”、“Times New Roman”等;默認(rèn)顯示非斜體、非下劃線。

【示例】
#include#include "putText.h"

using namespace std;
using namespace cv;

int main()//自己寫(xiě)庫(kù)解決putText無(wú)法顯示中文字符問(wèn)題
{
	Mat img = imread("lyf.png");

	putText(img, "中文無(wú)法顯示...", Point(50, 50), 2, 1, Scalar(0, 0, 255), 1, 8, false);
	putTextHusky(img, "Arial字體...", Point(50, 100), Scalar(0, 0, 255), 30, "Arial");
	putTextHusky(img, "微軟雅黑字體...", Point(50, 150), Scalar(0, 255, 0), 30, "微軟雅黑", true, true);
	putTextHusky(img, "楷體字體...\n", Point(50, 200), Scalar(128, 255, 0), 30, "楷體", true, true);
	//imwrite("lyfWrite.png", img);
	imshow("test", img);
	waitKey(0);
	
	system("pause");
	return 0;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧


網(wǎng)站題目:C++OpenCV【解決putText不能顯示中文】-創(chuàng)新互聯(lián)
文章分享:http://weahome.cn/article/dojicd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部