From 60a356666449d2187d254c3e5f56f79babb12b27 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Sun, 29 Mar 2015 16:23:29 -0500 Subject: [PATCH] Starting Bitmap module, #13. --- index.js | 25 ++++++++++++++++++++- src/robotjs.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e761fb6b..8059029d 100644 --- a/index.js +++ b/index.js @@ -1 +1,24 @@ -module.exports = require('./build/Release/robotjs.node'); +var robotjs = require('./build/Release/robotjs.node'); + +module.exports = robotjs; + +module.exports.screen = {}; + +function bitmap (imagedata, width, height) +{ + this.imagedata = imagedata; + this.width = width; + this.height = height; +} + +bitmap.prototype.save = function(path) +{ + console.log("saving"); + //robotjs.saveBitmap(this.imagedata, path); +}; + +module.exports.screen.capture = function() +{ + b = robotjs.captureScreen(); + return new bitmap(b.image, b.width, b.height); +} \ No newline at end of file diff --git a/src/robotjs.cc b/src/robotjs.cc index 06e150d1..49bab987 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -7,6 +7,7 @@ #include "keypress.h" #include "screen.h" #include "screengrab.h" +#include "str_io.h" #include "MMBitmap.h" using namespace v8; @@ -288,6 +289,59 @@ NAN_METHOD(getPixelColor) NanReturnValue(NanNew(hex)); } +/* + ____ _ _ +| __ )(_) |_ _ __ ___ __ _ _ __ +| _ \| | __| '_ ` _ \ / _` | '_ \ +| |_) | | |_| | | | | | (_| | |_) | +|____/|_|\__|_| |_| |_|\__,_| .__/ + |_| +*/ + +NAN_METHOD(captureScreen) +{ + NanScope(); + + MMRect rect; + MMBitmapRef bitmap; + MMBMPStringError err; + MMSize displaySize = getMainDisplaySize(); + + rect = MMRectMake(0, 0, displaySize.width, displaySize.height); + + bitmap = copyMMBitmapFromDisplayInRect(rect); + + Local obj = NanNew(); + obj->Set(NanNew("width"), NanNew(displaySize.width)); + obj->Set(NanNew("height"), NanNew(displaySize.height)); + obj->Set(NanNew("image"), NanNew(createStringFromMMBitmap(bitmap, &err))); + + destroyMMBitmap(bitmap); + + NanReturnValue(obj); +} + +NAN_METHOD(getColor) +{ + NanScope(); + + MMBitmapRef bitmap; + MMBMPStringError err; + + uint8_t *str = (*v8::String::Utf8Value(args[0]->ToString())); + size_t len = strlen(str); + + bitmap = createMMBitmapFromString(str, len, &err)); + + /*Local obj = NanNew(); + obj->Set(NanNew("width"), NanNew(displaySize.width)); + obj->Set(NanNew("height"), NanNew(displaySize.height)); + obj->Set(NanNew("image"), NanNew(createStringFromMMBitmap(bitmap, &err))); + NanReturnValue(obj);*/ + + NanReturnValue(NanNew("1")); +} + void init(Handle target) { @@ -314,7 +368,12 @@ void init(Handle target) target->Set(NanNew("getPixelColor"), NanNew(getPixelColor)->GetFunction()); - + + target->Set(NanNew("captureScreen"), + NanNew(captureScreen)->GetFunction()); + + target->Set(NanNew("getColor"), + NanNew(getColor)->GetFunction()); } NODE_MODULE(robotjs, init)