From ee25c1f2eebf057eafd436641e270658b8781b95 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Thu, 17 Apr 2014 14:08:02 -0700 Subject: [PATCH] putText with font and color support --- package.json | 1 + src/Matrix.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Matrix.h | 2 ++ test/unit.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/package.json b/package.json index 01824d2..c5784f7 100755 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ }, "license": "MIT", "scripts": { + "build": "node-gyp build", "preinstall": "node-gyp clean rebuild", "test": "vows test/unit.js" }, diff --git a/src/Matrix.cc b/src/Matrix.cc index 58822d7..51d8732 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -97,6 +97,8 @@ Matrix::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor, "pushBack", PushBack); + NODE_SET_PROTOTYPE_METHOD(constructor, "putText", PutText); + NODE_SET_METHOD(constructor, "Eye", Eye); @@ -1650,3 +1652,43 @@ Matrix::PushBack(const v8::Arguments& args) { return scope.Close(args.This()); } + +Handle +Matrix::PutText(const v8::Arguments& args) { + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + + v8::String::AsciiValue textString(args[0]); + char *text = (char *) malloc(textString.length() + 1); + strcpy(text, *textString); + + int x = args[1]->IntegerValue(); + int y = args[2]->IntegerValue(); + + v8::String::AsciiValue fontString(args[3]); + char *font = (char *) malloc(fontString.length() + 1); + strcpy(font, *fontString); + int constFont = cv::FONT_HERSHEY_SIMPLEX; + + if (!strcmp(font, "HERSEY_SIMPLEX")) { constFont = cv::FONT_HERSHEY_SIMPLEX; } + else if (!strcmp(font, "HERSEY_PLAIN")) { constFont = cv::FONT_HERSHEY_PLAIN; } + else if (!strcmp(font, "HERSEY_DUPLEX")) { constFont = cv::FONT_HERSHEY_DUPLEX; } + else if (!strcmp(font, "HERSEY_COMPLEX")) { constFont = cv::FONT_HERSHEY_COMPLEX; } + else if (!strcmp(font, "HERSEY_TRIPLEX")) { constFont = cv::FONT_HERSHEY_TRIPLEX; } + else if (!strcmp(font, "HERSEY_COMPLEX_SMALL")) { constFont = cv::FONT_HERSHEY_COMPLEX_SMALL; } + else if (!strcmp(font, "HERSEY_SCRIPT_SIMPLEX")) { constFont = cv::FONT_HERSHEY_SCRIPT_SIMPLEX; } + else if (!strcmp(font, "HERSEY_SCRIPT_COMPLEX")) { constFont = cv::FONT_HERSHEY_SCRIPT_COMPLEX; } + else if (!strcmp(font, "HERSEY_SCRIPT_SIMPLEX")) { constFont = cv::FONT_HERSHEY_SCRIPT_SIMPLEX; } + + cv::Scalar color(0, 0, 255); + + if(args[4]->IsArray()) { + Local objColor = args[4]->ToObject(); + color = setColor(objColor); + } + + cv::putText(self->mat, text, cv::Point(x, y), constFont, 1, color, 2); + + return scope.Close(Undefined()); +} diff --git a/src/Matrix.h b/src/Matrix.h index 33f723c..0e948a7 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -90,6 +90,8 @@ class Matrix: public node::ObjectWrap { JSFUNC(MinMaxLoc) JSFUNC(PushBack) + + JSFUNC(PutText) /* static Handle Val(const Arguments& args); static Handle RowRange(const Arguments& args); diff --git a/test/unit.js b/test/unit.js index a9c3058..763ae2f 100755 --- a/test/unit.js +++ b/test/unit.js @@ -351,6 +351,39 @@ vows.describe('Smoke Tests OpenCV').addBatch({ } + , "putText": { + topic: function() { + var cv = require('../lib/opencv') + , self = this + + cv.readImage('./examples/coin1.jpg', function(e, im){ + self.callback(null, im); + }); + }, + "fonts": function(im) { + function rnd() { + return Math.round(Math.random() * 255); + }; + + var y = 0; + + ([ + "HERSEY_SIMPLEX", + "HERSEY_PLAIN", + "HERSEY_DUPLEX", + "HERSEY_COMPLEX", + "HERSEY_TRIPLEX", + "HERSEY_COMPLEX_SMALL", + "HERSEY_SCRIPT_SIMPLEX", + "HERSEY_SCRIPT_COMPLEX", + "HERSEY_SCRIPT_SIMPLEX" + ]).forEach(function(font) { + im.putText("Some text", 0, y += 20, font, [rnd(), rnd(), rnd()]); + }); + + im.save("./examples/coin1-with-text.jpg"); + } + } }).export(module);