使用FreeType2绘制字体并描边

serial
BlueMatthew 1 year ago
parent e53f70f2c8
commit 6ef0f3b652

@ -928,45 +928,23 @@ void visualize(const char* filename, const ncnn::Mat& m)
cv::imwrite(filename, a);
}
void DrawOutlineText(cv::Mat& mat, const std::string& str, cv::Point startPoint, double fontScale, cv::Scalar clr1, cv::Scalar clr2, int thickness1, int thickness2)
void DrawOutlineText(cv::Ptr<cv::ft::FreeType2> ft2, cv::Mat& mat, const std::string& str, cv::Point startPoint, int fontSize, cv::Scalar clr, int thickness)
{
std::vector<std::string> chars;
splitUtf8Str(str, chars);
std::vector<std::string> lines = split(str, "\n");
int lineHeight = 0;
cv::Point pt = startPoint;
cv::Size textSize, textSize2;
cv::Size textSize;
int baseline = 0;
cv::Point pt2;
for (std::vector<std::string>::const_iterator it = chars.cbegin(); it != chars.cend(); ++it )
{
if (it->compare("\n") == 0)
{
pt.x = startPoint.x;
pt.y += lineHeight > 0 ? (lineHeight + 2) : 0;
continue;
}
textSize = cv::getTextSize(*it, cv::FONT_HERSHEY_COMPLEX, fontScale, thickness1, &baseline);
textSize2 = cv::getTextSize(*it, cv::FONT_HERSHEY_COMPLEX, fontScale, thickness2, &baseline);
lineHeight = std::max(lineHeight, std::max(textSize.height, textSize2.height));
if (it->compare(" ") != 0)
for (std::vector<std::string>::const_iterator it = lines.cbegin(); it != lines.cend(); ++it )
{
// cv2.putText(image,"text",(180,150),cv2.FONT_HERSHEY_COMPLEX,3,(255,255,255),16,cv2.LINE_AA)
// cv2.putText(image,"text",(180,150),cv2.FONT_HERSHEY_COMPLEX,3,(0,0,0),4,cv2.LINE
// balck
textSize = ft2->getTextSize(*it, fontSize, thickness, &baseline);
lineHeight = std::max(lineHeight, textSize.height);
cv::putText(mat, *it, pt, cv::FONT_HERSHEY_COMPLEX, fontScale, clr1, thickness1, cv::LINE_AA);
// white
pt2 = pt;
// pt2.y += 1;
cv::putText(mat, *it, pt2, cv::FONT_HERSHEY_COMPLEX, fontScale, clr2, thickness2, cv::LINE_AA);
}
ft2->putText(mat, *it, pt, fontSize, clr, thickness, cv::LINE_AA, false);
pt.x += std::max(textSize.width, textSize2.width);
pt.x = startPoint.x;
pt.y += lineHeight > 0 ? (lineHeight + 2) : 0;
}
}
@ -1045,17 +1023,20 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
}
}
int thickness2 = 4 * ratio;
if (thickness2 < 2) thickness2 = 2;
int thickness1 = 2 * ratio;
if (thickness1 < 1) thickness1 = 1;
int thickness = 4 * ratio;
if (thickness < 2) thickness = 2;
cv::Scalar scalar1(0, 0, 0); // black
cv::Scalar scalar2(255, 255, 255); // white
cv::Scalar scalar(255, 255, 255); // white
int fontSize = 24;
std::string fontPath = m_appPath+ "fonts/Noto.ttf";
cv::Ptr<cv::ft::FreeType2> ft2;
ft2 = cv::ft::createFreeType2();
ft2->loadFontData(fontPath.c_str(), 0);
#ifdef _DEBUG
cv::Scalar scalar(0, 0, 255); // red
cv::Scalar scalarRed(0, 0, 255); // red
NdkCamera::CAPTURE_RESULT captureResult = mCamera->getCaptureResult();
char str[128] = { 0 };
@ -1068,13 +1049,9 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
captureResult.hdrMode);
// cv::putText(mat, str, cv::Point(0, mat.rows - 20), cv::FONT_HERSHEY_COMPLEX, fontScale, scalar, thickness1, cv::LINE_AA);
std::string fontPath = m_appPath+ "fonts/Noto.ttf";
cv::Ptr<cv::ft::FreeType2> ft2;
ft2 = cv::ft::createFreeType2();
ft2->loadFontData(fontPath.c_str(), 0);
ft2->putText(mat, str, cv::Point(0, mat.rows - 48),
36, scalar, thickness1, cv::LINE_AA, false);
fontSize, scalarRed, thickness, cv::LINE_AA, false);
// text.putText(mat, str.c_str(), cv::Point(0, mat.rows - 20), scalar);
@ -1093,8 +1070,7 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
continue;
}
textSize = cv::getTextSize(it->text, cv::FONT_HERSHEY_TRIPLEX, fontScale, thickness1, &baseline);
textSize2 = cv::getTextSize(it->text, cv::FONT_HERSHEY_TRIPLEX, fontScale, thickness2, &baseline);
textSize = ft2->getTextSize(it->text, fontSize, thickness, &baseline);
if (it->alignment == OSD_ALIGNMENT_TOP_LEFT)
{
@ -1125,7 +1101,7 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
pt2 = pt1;
pt2.y += textSize.height;
DrawOutlineText(mat, it->text, pt1, fontScale, scalar1, scalar2, thickness1, thickness2);
DrawOutlineText(ft2, mat, it->text, pt1, fontScale, scalar, thickness);
}
vector <int> params;

Loading…
Cancel
Save