完善水印功能

This commit is contained in:
luoliangyi 2022-06-14 17:06:19 +08:00
parent 80b67e2e2e
commit 34b239f8c3
5 changed files with 367 additions and 228 deletions

View File

@ -6,6 +6,7 @@ HGImgProc_ResizeImage
HGImgProc_ImageAdjustColors
HGImgProc_ImageAutoCrop
HGImgProc_ImageBlankCheck
HGImgProc_ImageDrawLine
HGImgProc_AddImageWatermark
HGImgProc_CreateOCRMgr

View File

@ -14,17 +14,17 @@ CvxText::~CvxText()
Destroy();
}
bool CvxText::Create(const HGChar* fontPath)
HGResult CvxText::Create(const HGChar* fontPath)
{
if (NULL != m_face)
{
return false;
return HGBASE_ERR_FAIL;
}
FT_Init_FreeType(&m_library);
if (NULL == m_library)
{
return false;
return HGBASE_ERR_FAIL;
}
FT_New_Face(m_library, fontPath, 0, &m_face);
@ -32,24 +32,24 @@ bool CvxText::Create(const HGChar* fontPath)
{
FT_Done_FreeType(m_library);
m_library = NULL;
return false;
return HGBASE_ERR_FAIL;
}
return true;
return HGBASE_ERR_OK;
}
bool CvxText::Destroy()
HGResult CvxText::Destroy()
{
if (NULL == m_face)
{
return false;
return HGBASE_ERR_FAIL;
}
FT_Done_Face(m_face);
m_face = NULL;
FT_Done_FreeType(m_library);
m_library = NULL;
return true;
return HGBASE_ERR_OK;
}
#if defined(HG_CMP_MSC)
@ -89,61 +89,18 @@ static uint16_t *GetUnicodeStr(const char *text)
}
#endif
bool CvxText::MeasureString(const HGChar* text, HGInt x, HGInt y, HGUInt size,
HGBool bold, HGBool underline, HGBool italic, HGBool strikeout, HGRect* pos)
HGResult CvxText::DrawString(HGImage image, const HGChar* text, HGColor color, HGUInt posType, HGInt locationX, HGInt locationY,
HGUInt fontSize, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout)
{
if (NULL == m_face || NULL == text || NULL == pos)
if (NULL == m_face)
{
return false;
return HGBASE_ERR_FAIL;
}
#if defined(HG_CMP_MSC)
WCHAR *pUnicode = GetUnicodeStr(text);
#else
uint16_t *pUnicode = GetUnicodeStr(text);
uint16_t *pUnicodeEx = pUnicode + 1;
#endif
pos->left = x;
pos->top = y;
pos->right = x;
pos->bottom = y;
int i = 0;
#if defined(HG_CMP_MSC)
while ('\0' != pUnicode[i])
#else
while ('\0' != pUnicodeEx[i])
#endif
{
if (0 != i)
{
pos->right += (size / 10);
}
uint32_t width, height;
#if defined(HG_CMP_MSC)
MeasureChar(pUnicode[i], size, bold, underline, italic, strikeout, width, height);
#else
MeasureChar(pUnicodeEx[i], size, bold, underline, italic, strikeout, width, height);
#endif
pos->right += width;
if (y + (int32_t)height > pos->bottom)
pos->bottom = y + (int32_t)height;
++i;
}
delete [] pUnicode;
return true;
}
bool CvxText::DrawString(HGImage image, const HGChar* text, HGInt x, HGInt y, HGColor color, HGUInt size,
HGBool bold, HGBool underline, HGBool italic, HGBool strikeout)
{
if (NULL == m_face || NULL == image || NULL == text)
{
return false;
}
assert(NULL != image);
assert(NULL != text && '\0' != *text);
assert(posType >= HGIMGPROC_WMPOSTYPE_LEFT && posType <= HGIMGPROC_WMPOSTYPE_LOCATION);
assert(0 != fontSize);
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
@ -153,72 +110,265 @@ bool CvxText::DrawString(HGImage image, const HGChar* text, HGInt x, HGInt y, HG
HGResult ret = HGBase_CloneImage(image, HGBASE_IMGTYPE_GRAY, 0, &imageTmp);
if (HGBASE_ERR_OK == ret)
{
ret = DrawString(imageTmp, text, x, y, color, size, bold, underline, italic, strikeout);
if (HGBASE_ERR_OK == ret)
{
ret = HGBase_CopyImage(imageTmp, image);
}
ret = DrawString(imageTmp, text, color, posType, locationX, locationY, fontSize, bold, underline, italic, strikeout);
HGBase_DestroyImage(imageTmp);
}
return ret;
}
HGUInt width, height;
std::vector<HGRect> vPos;
GetStringLocation(text, fontSize, bold, underline, italic, strikeout, width, height, vPos);
if (HGIMGPROC_WMPOSTYPE_LOCATION == posType)
{
#if defined(HG_CMP_MSC)
WCHAR *pUnicode = GetUnicodeStr(text);
WCHAR* pUnicode = GetUnicodeStr(text);
#else
uint16_t *pUnicode = GetUnicodeStr(text);
uint16_t *pUnicodeEx = pUnicode + 1;
uint16_t* pUnicode = GetUnicodeStr(text);
uint16_t* pUnicodeEx = pUnicode + 1;
#endif
int x2 = x;
int y2 = y;
int i = 0;
#if defined(HG_CMP_MSC)
while ('\0' != pUnicode[i])
#else
while ('\0' != pUnicodeEx[i])
#endif
{
#if defined(HG_CMP_MSC)
DrawChar(image, pUnicode[i], locationX + vPos[i].left, locationY + vPos[i].top, color, fontSize, bold, italic);
#else
DrawChar(image, pUnicodeEx[i], locationX + vPos[i].left, locationY + vPos[i].top, color, fontSize, bold, italic);
#endif
++i;
}
delete[] pUnicode;
if (underline)
{
HGImgProc_ImageDrawLine(image, locationX, locationY + height - 1, locationX + width, locationY + height - 1, 1, color);
}
if (strikeout)
{
if (underline)
HGImgProc_ImageDrawLine(image, locationX, locationY + (height - 1) / 2,
locationX + width, locationY + (height - 1) / 2, 1, color);
else
HGImgProc_ImageDrawLine(image, locationX, locationY + height / 2,
locationX + width, locationY + height / 2, 1, color);
}
return HGBASE_ERR_OK;
}
HGImageRoi imgRoi;
HGBase_GetImageROI(image, &imgRoi);
HGInt roiWidth = imgRoi.right - imgRoi.left;
HGInt roiHeight = imgRoi.bottom - imgRoi.top;
HGInt x, y;
if (HGIMGPROC_WMPOSTYPE_LEFT == posType)
{
x = 0;
y = (roiHeight - height) / 2;
}
else if (HGIMGPROC_WMPOSTYPE_TOP == posType)
{
x = (roiWidth - width) / 2;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHT == posType)
{
x = roiWidth - width;
y = (roiHeight - height) / 2;
}
else if (HGIMGPROC_WMPOSTYPE_BOTTOM == posType)
{
x = (roiWidth - width) / 2;
y = roiHeight - height;
}
else if (HGIMGPROC_WMPOSTYPE_LEFTTOP == posType)
{
x = 0;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHTTOP == posType)
{
x = roiWidth - width;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_LEFTBOTTOM == posType)
{
x = 0;
y = roiHeight - height;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHTBOTTOM == posType)
{
x = roiWidth - width;
y = roiHeight - height;
}
else
{
x = (roiWidth - width) / 2;
y = (roiHeight - height) / 2;
}
return DrawString(image, text, color, HGIMGPROC_WMPOSTYPE_LOCATION, x, y, fontSize, bold, underline, italic, strikeout);
}
void CvxText::MeasureChar(HGUInt wc, HGUInt fontSize, HGBool bold, HGBool italic, FT_BBox& acbox)
{
assert(NULL != m_face);
assert(0 != fontSize);
FT_Set_Pixel_Sizes(m_face, fontSize, fontSize);
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
if (bold)
{
if (m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Outline_Embolden(&m_face->glyph->outline, fontSize * 2);
}
}
if (italic)
{
if (m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Matrix matrix;
matrix.xx = 0x10000L;
matrix.xy = 0.5f * 0x10000L;
matrix.yx = 0;
matrix.yy = 0x10000L;
FT_Outline_Transform(&m_face->glyph->outline, &matrix);
}
}
if (m_face->glyph->format != FT_GLYPH_FORMAT_BITMAP)
{
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_NORMAL);
}
FT_Glyph glyph;
FT_Get_Glyph(m_face->glyph, &glyph);
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &acbox);
}
void CvxText::GetStringLocation(const HGChar* text, HGUInt fontSize, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout,
HGUInt& width, HGUInt& height, std::vector<HGRect> &vPos)
{
assert(NULL != text && '\0' != *text);
assert(0 != fontSize);
width = 0;
height = 0;
vPos.clear();
#if defined(HG_CMP_MSC)
WCHAR* pUnicode = GetUnicodeStr(text);
#else
uint16_t* pUnicode = GetUnicodeStr(text);
uint16_t* pUnicodeEx = pUnicode + 1;
#endif
HGInt origin = 0;
HGInt minY = MAXINT, maxY = MININT;
int i = 0;
#if defined(HG_CMP_MSC)
while ('\0' != pUnicode[i])
#else
while ('\0' != pUnicodeEx[i])
while ('\0' != pUnicodeEx[i])
#endif
{
if (0 != i)
FT_BBox acbox;
#if defined(HG_CMP_MSC)
MeasureChar(pUnicode[i], fontSize, bold, italic, acbox);
#else
MeasureChar(pUnicodeEx[i], fontSize, bold, italic, acbox);
#endif
HGRect pos;
pos.left = origin + acbox.xMin;
pos.right = origin + acbox.xMax;
pos.top = -acbox.yMax;
pos.bottom = -acbox.yMin;
vPos.push_back(pos);
#if defined(HG_CMP_MSC)
if (pUnicode[i] < 255 && (!isprint((int)pUnicode[i]) || isspace((int)pUnicode[i])))
#else
if (pUnicodeEx[i] < 255 && (!isprint((int)pUnicodeEx[i]) || isspace((int)pUnicodeEx[i])))
#endif
{
x2 += (size / 10);
origin += fontSize / 2;
}
else
{
origin += (acbox.xMax + acbox.xMin);
}
uint32_t width, height;
#if defined(HG_CMP_MSC)
DrawChar(image, pUnicode[i], x2, y2, color, size, bold, underline, italic, strikeout, width, height);
#else
DrawChar(image, pUnicodeEx[i], x2, y2, color, size, bold, underline, italic, strikeout, width, height);
#endif
x2 += width;
if (acbox.yMin < minY)
minY = acbox.yMin;
if (acbox.yMax > maxY)
maxY = acbox.yMax;
++i;
}
delete [] pUnicode;
return true;
delete[] pUnicode;
width = origin;
height = (maxY - minY);
if (underline)
++height;
for (int i = 0; i < (int)vPos.size(); ++i)
{
vPos[i].top += maxY;
vPos[i].bottom += maxY;
}
}
void CvxText::MeasureChar(HGUInt wc, HGUInt size, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout,
HGUInt& width, HGUInt& height)
void CvxText::DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color, HGUInt fontSize, HGBool bold, HGBool italic)
{
FT_Set_Pixel_Sizes(m_face, size, size);
assert(NULL != m_face);
FT_Set_Pixel_Sizes(m_face, fontSize, fontSize);
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
width = (0 == m_face->glyph->bitmap.width) ? size / 2 : m_face->glyph->bitmap.width;
height = (0 == m_face->glyph->bitmap.rows) ? size : m_face->glyph->bitmap.rows;
}
if (bold)
{
if (m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Outline_Embolden(&m_face->glyph->outline, fontSize * 2);
}
}
void CvxText::DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color,
HGUInt size, HGBool bold, HGBool italic, HGBool angle, HGBool underline, HGUInt& width, HGUInt& height)
{
FT_Set_Pixel_Sizes(m_face, size, size);
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_NORMAL);
if (italic)
{
if (m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Matrix matrix;
matrix.xx = 0x10000L;
matrix.xy = 0.5f * 0x10000L;
matrix.yx = 0;
matrix.yy = 0x10000L;
FT_Outline_Transform(&m_face->glyph->outline, &matrix);
}
}
if (m_face->glyph->format != FT_GLYPH_FORMAT_BITMAP)
{
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_NORMAL);
}
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
@ -253,7 +403,8 @@ void CvxText::DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color
int32_t xOffset = (int32_t)imgLeft + x + (int32_t)j;
int32_t yOffset = (int32_t)imgTop + y + (int32_t)i;
if (xOffset >= 0 && xOffset < (int32_t)imgWidth && yOffset >= 0 && yOffset < (int32_t)imgHeight)
if (xOffset >= (int32_t)imgLeft && xOffset < (int32_t)imgRight
&& yOffset >= (int32_t)imgTop && yOffset < (int32_t)imgBottom)
{
uint8_t *p = m_face->glyph->bitmap.buffer + m_face->glyph->bitmap.pitch * i + j;
int32_t opacity = HG_GETCOLOR_A(color) * (*p) / 255;
@ -306,7 +457,4 @@ void CvxText::DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color
}
}
}
width = (0 == m_face->glyph->bitmap.width) ? size / 2 : m_face->glyph->bitmap.width;
height = (0 == m_face->glyph->bitmap.rows) ? size : m_face->glyph->bitmap.rows;
}

View File

@ -4,6 +4,10 @@
#include "HGImgProc.h"
#include <ft2build.h>
#include <freetype/freetype.h>
#include <freetype/ftbitmap.h>
#include <freetype/ftoutln.h>
#include <freetype/ftglyph.h>
#include <vector>
class CvxText
{
@ -11,18 +15,16 @@ public:
CvxText();
~CvxText();
bool Create(const HGChar *fontPath);
bool Destroy();
bool MeasureString(const HGChar *text, HGInt x, HGInt y, HGUInt size,
HGBool bold, HGBool underline, HGBool italic, HGBool strikeout, HGRect *pos);
bool DrawString(HGImage image, const HGChar *text, HGInt x, HGInt y, HGColor color, HGUInt size,
HGBool bold, HGBool underline, HGBool italic, HGBool strikeout);
HGResult Create(const HGChar *fontPath);
HGResult Destroy();
HGResult DrawString(HGImage image, const HGChar* text, HGColor color, HGUInt posType, HGInt locationX, HGInt locationY,
HGUInt fontSize, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout);
private:
void MeasureChar(HGUInt wc, HGUInt size, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout,
HGUInt &width, HGUInt &height);
void DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color,
HGUInt size, HGBool bold, HGBool italic, HGBool angle, HGBool underline, HGUInt &width, HGUInt &height);
void MeasureChar(HGUInt wc, HGUInt fontSize, HGBool bold, HGBool italic, FT_BBox& acbox);
void GetStringLocation(const HGChar* text, HGUInt fontSize, HGBool bold, HGBool underline, HGBool italic, HGBool strikeout,
HGUInt &width, HGUInt &height, std::vector<HGRect> &vPos);
void DrawChar(HGImage image, HGUInt wc, HGInt x, HGInt y, HGColor color, HGUInt fontSize, HGBool bold, HGBool italic);
private:
FT_Library m_library;

View File

@ -82,7 +82,12 @@ HGResult HGAPI HGImgProc_ResizeImage(HGImage image, HGImage destImage, HGUInt in
channels = 4;
uint8_t* p = data + roi.top * imgInfo.widthStep + roi.left * channels;
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
p = data + (imgInfo.height - roi.bottom) * imgInfo.widthStep + roi.left * channels;
uint8_t* pDest = destData + destRoi.top * destImgInfo.widthStep + destRoi.left * channels;
if (HGBASE_IMGORIGIN_BOTTOM == destImgInfo.origin)
pDest = destData + (destImgInfo.height - destRoi.bottom) * destImgInfo.widthStep + destRoi.left * channels;
IplImage* pImg = cvCreateImageHeader(cvSize(roiWidth, roiHeight), IPL_DEPTH_8U, channels);
IplImage* pImgDest = cvCreateImageHeader(cvSize(destRoiWidth, destRoiHeight), IPL_DEPTH_8U, channels);
@ -148,8 +153,10 @@ HGResult HGAPI HGImgProc_ImageAdjustColors(HGImage image, HGImage destImage,
}
uint8_t* p = data + roi.top * imgInfo.widthStep + roi.left * channels;
cv::Mat img(roiHeight, roiWidth, CV_8UC(channels), p, imgInfo.widthStep);
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
p = data + (imgInfo.height - roi.bottom) * imgInfo.widthStep + roi.left * channels;
cv::Mat img(roiHeight, roiWidth, CV_8UC(channels), p, imgInfo.widthStep);
CImageApplyAdjustColors imgApply(brightness, contrast, gamma);
imgApply.apply(img, 0);
}
@ -197,8 +204,10 @@ HGResult HGAPI HGImgProc_ImageAdjustColors(HGImage image, HGImage destImage,
HGBase_CopyImage(image, destImage);
uint8_t* pDest = destData + destRoi.top * destImgInfo.widthStep + destRoi.left * channels;
cv::Mat destImg(destRoiHeight, destRoiWidth, CV_8UC(channels), pDest, destImgInfo.widthStep);
if (HGBASE_IMGORIGIN_BOTTOM == destImgInfo.origin)
pDest = destData + (destImgInfo.height - destRoi.bottom) * destImgInfo.widthStep + destRoi.left * channels;
cv::Mat destImg(destRoiHeight, destRoiWidth, CV_8UC(channels), pDest, destImgInfo.widthStep);
CImageApplyAdjustColors imgApply(brightness, contrast, gamma);
imgApply.apply(destImg, 0);
}
@ -268,6 +277,9 @@ HGResult HGAPI HGImgProc_ImageAutoCrop(HGImage image, HGBool autoCrop, HGBool de
channels = 3;
uint8_t* p = data + roi.top * imgInfo.widthStep + roi.left * channels;
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
p = data + (imgInfo.height - roi.bottom) * imgInfo.widthStep + roi.left * channels;
cv::Mat img(roiHeight, roiWidth, CV_8UC(channels), p, imgInfo.widthStep);
cv::Mat destImg;
@ -344,6 +356,9 @@ HGResult HGAPI HGImgProc_ImageBlankCheck(HGImage image, const HGImgBlankCheckPar
channels = 3;
uint8_t* p = data + roi.top * imgInfo.widthStep + roi.left * channels;
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
p = data + (imgInfo.height - roi.bottom) * imgInfo.widthStep + roi.left * channels;
cv::Mat img(roiHeight, roiWidth, CV_8UC(channels), p, imgInfo.widthStep);
bool ret = CImageApplyDiscardBlank::apply(img, threshold, edge, blockSize, devTh, meanTh);
@ -351,44 +366,73 @@ HGResult HGAPI HGImgProc_ImageBlankCheck(HGImage image, const HGImgBlankCheckPar
return HGBASE_ERR_OK;
}
static HGResult MeasureString(const HGChar* text, HGInt x, HGInt y, const HGImgWatermarkFontParam* fontParam, HGRect* pos)
HGResult HGAPI HGImgProc_ImageDrawLine(HGImage image, HGInt x1, HGInt y1, HGInt x2, HGInt y2, HGUInt lineWidth, HGColor color)
{
assert(NULL != text && '\0' != *text);
assert(NULL != pos);
std::string fontName = "宋体";
HGUInt fontSize = 10;
HGBool bold = HGFALSE;
HGBool underline = HGFALSE;
HGBool italic = HGFALSE;
HGBool strikeout = HGFALSE;
if (NULL != fontParam)
if (NULL == image || 0 == lineWidth)
{
fontName = fontParam->foneName;
fontSize = fontParam->fontSize;
bold = fontParam->bold;
underline = fontParam->underline;
italic = fontParam->italic;
strikeout = fontParam->strikeout;
return HGBASE_ERR_INVALIDARG;
}
HGChar moduleName[256];
HGBase_GetModuleName(HGImgProc_AddImageWatermark, moduleName, 256);
HGChar modulePath[256];
HGBase_GetFilePath(moduleName, modulePath, 256);
HGChar fontPath[256];
sprintf(fontPath, "%s%s.ttf", modulePath, fontName.c_str());
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
uint32_t type = imgInfo.type;
CvxText cvxText;
bool ret = cvxText.Create(fontPath);
if (ret)
HGByte* data = NULL;
HGBase_GetImageData(image, &data);
HGImageRoi roi;
HGBase_GetImageROI(image, &roi);
HGUInt roiWidth = roi.right - roi.left;
HGUInt roiHeight = roi.bottom - roi.top;
if (HGBASE_IMGTYPE_BINARY == type || HGBASE_IMGTYPE_GRAY == type)
{
ret = cvxText.MeasureString(text, x, y, fontSize, bold, underline, italic, strikeout, pos);
cvxText.Destroy();
HGImage imageTmp = NULL;
HGResult ret = HGBase_CloneImage(image, HGBASE_IMGTYPE_RGB, 0, &imageTmp);
if (HGBASE_ERR_OK == ret)
{
ret = HGImgProc_ImageDrawLine(imageTmp, x1, y1, x2, y2, lineWidth, color);
if (HGBASE_ERR_OK == ret)
{
ret = HGBase_CopyImage(imageTmp, image);
}
HGBase_DestroyImage(imageTmp);
}
return ret;
}
return ret ? HGBASE_ERR_OK : HGBASE_ERR_FAIL;
uint32_t channels = 3;
if (HGBASE_IMGTYPE_BGRA == type || HGBASE_IMGTYPE_RGBA == type)
channels = 4;
uint8_t* p = data + roi.top * imgInfo.widthStep + roi.left * channels;
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
p = data + (imgInfo.height - roi.bottom) * imgInfo.widthStep + roi.left * channels;
IplImage* pImg = cvCreateImageHeader(cvSize(roiWidth, roiHeight), IPL_DEPTH_8U, channels);
cvSetData(pImg, p, imgInfo.widthStep);
if (HGBASE_IMGORIGIN_BOTTOM == imgInfo.origin)
{
y1 = roiHeight - y1 - 1;
y2 = roiHeight - y2 - 1;
}
HGUInt r = HG_GETCOLOR_R(color);
HGUInt g = HG_GETCOLOR_G(color);
HGUInt b = HG_GETCOLOR_B(color);
if (HGBASE_IMGTYPE_BGR == type || HGBASE_IMGTYPE_BGRA == type)
{
r = HG_GETCOLOR_B(color);
g = HG_GETCOLOR_G(color);
b = HG_GETCOLOR_R(color);
}
cvLine(pImg, cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(r, g, b), lineWidth);
cvReleaseImageHeader(&pImg);
return HGBASE_ERR_OK;
}
HGResult HGAPI HGImgProc_AddImageWatermark(HGImage image, const HGChar* text, HGColor color, HGUInt posType,
@ -400,102 +444,42 @@ HGResult HGAPI HGImgProc_AddImageWatermark(HGImage image, const HGChar* text, HG
return HGBASE_ERR_INVALIDARG;
}
HGImageRoi imgRoi;
HGBase_GetImageROI(image, &imgRoi);
HGInt roiWidth = imgRoi.right - imgRoi.left;
HGInt roiHeight = imgRoi.bottom - imgRoi.top;
std::string fontName = "宋体";
HGUInt fontSize = 20;
HGBool bold = HGFALSE;
HGBool underline = HGFALSE;
HGBool italic = HGFALSE;
HGBool strikeout = HGFALSE;
if (HGIMGPROC_WMPOSTYPE_LOCATION == posType)
if (NULL != fontParam)
{
std::string fontName = "宋体";
HGUInt fontSize = 10;
HGBool bold = HGFALSE;
HGBool underline = HGFALSE;
HGBool italic = HGFALSE;
HGBool strikeout = HGFALSE;
if (NULL != fontParam)
if (0 == fontParam->fontSize)
{
fontName = fontParam->foneName;
fontSize = fontParam->fontSize;
bold = fontParam->bold;
underline = fontParam->underline;
italic = fontParam->italic;
strikeout = fontParam->strikeout;
return HGBASE_ERR_INVALIDARG;
}
HGChar moduleName[256];
HGBase_GetModuleName(HGImgProc_AddImageWatermark, moduleName, 256);
HGChar modulePath[256];
HGBase_GetFilePath(moduleName, modulePath, 256);
HGChar fontPath[256];
sprintf(fontPath, "%s%s.ttf", modulePath, fontName.c_str());
CvxText cvxText;
bool ret = cvxText.Create(fontPath);
if (ret)
{
ret = cvxText.DrawString(image, text, locationX, locationY, color, fontSize, bold, underline, italic, strikeout);
cvxText.Destroy();
}
return ret ? HGBASE_ERR_OK : HGBASE_ERR_FAIL;
fontName = fontParam->foneName;
fontSize = fontParam->fontSize;
bold = fontParam->bold;
underline = fontParam->underline;
italic = fontParam->italic;
strikeout = fontParam->strikeout;
}
HGRect pos;
HGResult ret = MeasureString(text, 0, 0, fontParam, &pos);
HGChar moduleName[256];
HGBase_GetModuleName((void *)HGImgProc_AddImageWatermark, moduleName, 256);
HGChar modulePath[256];
HGBase_GetFilePath(moduleName, modulePath, 256);
HGChar fontPath[256];
sprintf(fontPath, "%s%s.ttf", modulePath, fontName.c_str());
CvxText cvxText;
HGResult ret = cvxText.Create(fontPath);
if (HGBASE_ERR_OK != ret)
{
return ret;
HGInt stringWidth = pos.right - pos.left;
HGInt stringHeight = pos.bottom - pos.top;
HGInt x, y;
if (HGIMGPROC_WMPOSTYPE_LEFT == posType)
{
x = 0;
y = (roiHeight - stringHeight) / 2;
}
else if (HGIMGPROC_WMPOSTYPE_TOP == posType)
{
x = (roiWidth - stringWidth) / 2;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHT == posType)
{
x = roiWidth - stringWidth;
y = (roiHeight - stringHeight) / 2;
}
else if (HGIMGPROC_WMPOSTYPE_BOTTOM == posType)
{
x = (roiWidth - stringWidth) / 2;
y = roiHeight - stringHeight;
}
else if (HGIMGPROC_WMPOSTYPE_LEFTTOP == posType)
{
x = 0;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHTTOP == posType)
{
x = roiWidth - stringWidth;
y = 0;
}
else if (HGIMGPROC_WMPOSTYPE_LEFTBOTTOM == posType)
{
x = 0;
y = roiHeight - stringHeight;
}
else if (HGIMGPROC_WMPOSTYPE_RIGHTBOTTOM == posType)
{
x = roiWidth - stringWidth;
y = roiHeight - stringHeight;
}
else
{
x = (roiWidth - stringWidth) / 2;
y = (roiHeight - stringHeight) / 2;
}
return HGImgProc_AddImageWatermark(image, text, color, HGIMGPROC_WMPOSTYPE_LOCATION, x, y, fontParam);
ret = cvxText.DrawString(image, text, color, posType, locationX, locationY, fontSize, bold, underline, italic, strikeout);
return ret;
}

View File

@ -112,6 +112,10 @@ HGEXPORT HGResult HGAPI HGImgProc_ImageAutoCrop(HGImage image, HGBool autoCrop,
*/
HGEXPORT HGResult HGAPI HGImgProc_ImageBlankCheck(HGImage image, const HGImgBlankCheckParam *param, HGBool *blank);
/* 画线
*/
HGEXPORT HGResult HGAPI HGImgProc_ImageDrawLine(HGImage image, HGInt x1, HGInt y1, HGInt x2, HGInt y2, HGUInt lineWidth, HGColor color);
/* 添加图像水印
* 1) image: in,
* 2) text: , windows上为GBK编码, linux上为UTF8编码