Files
opencv/cxx/util.cc
2025-03-17 15:55:43 +08:00

35 lines
1.2 KiB
C++

#include "node.h"
using namespace Napi;
#define ADD_PROC_FUNCTION(name) obj.Set(#name, Napi::Function::New<name>(env));
#define DEFINE_FUNCTION(name) Napi::Value name(Napi::CallbackInfo const &info)
DEFINE_FUNCTION(GetTextSize)
{
auto text = info[0].As<Napi::String>().Utf8Value();
auto fontFace = info[1].As<Napi::Number>().Int32Value();
auto fontScale = info[2].As<Napi::Number>().DoubleValue();
auto thickness = info[3].As<Napi::Number>().Int32Value();
int baseLine;
auto size = cv::getTextSize(text, fontFace, fontScale, thickness, &baseLine);
auto res = Object::New(info.Env());
res.Set("width", Number::New(info.Env(), size.width));
res.Set("height", Number::New(info.Env(), size.height));
res.Set("baseLine", Number::New(info.Env(), baseLine));
return res;
}
DEFINE_FUNCTION(GetFontScaleFromHeight)
{
auto fontFace = info[0].As<Napi::Number>().Int32Value();
auto pixelHeight = info[1].As<Napi::Number>().Int32Value();
auto thickness = info[2].As<Napi::Number>().Int32Value();
return Number::New(info.Env(), cv::getFontScaleFromHeight(fontFace, pixelHeight, thickness));
}
void InitUtilAPI(Napi::Env env, Napi::Object exports)
{
auto obj = Object::New(env);
ADD_PROC_FUNCTION(GetTextSize);
exports.Set("util", obj);
}