c++node.jsopencvnode.js-addon

send and format image data form node.js to node c++ addon problem?


how to convert info[0] to uchar array??

js "uint8clampedarray"

->

info Nan::FunctionCallbackInfo<v8::Value>

info[0] class v8::Local<class v8::Value>

->

uchar data[]

->

cv::Mat or ZXing::ImageView

need use image uchar data[];

->

DecodeBarcodes(cv::Mat) or ZXing::ReadBarcodes(ZXing::ImageView, {})

get qrcode detect result

ZXing::Result Convert Json and return

let addon = require("bindings")("addon.node");
// uint8clampedarray from front end browser
let imageData = [255,255,0,0,255,255.....]; // uint8clampedarray
addon.decodeQRcode(imageData )
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "ReadBarcode.h" // ZXing-cpp

// return ZXing::ImageView
inline ZXing::ImageView ImageViewFromMat(const cv::Mat& image)
{
    using ZXing::ImageFormat;
    auto fmt = ImageFormat::None;
    switch (image.channels()) {
        case 1: fmt = ImageFormat::Lum; break;
        case 3: fmt = ImageFormat::BGR; break;
        case 4: fmt = ImageFormat::BGRX; break;
    }
    if (image.depth() != CV_8U || fmt == ImageFormat::None)
        return { nullptr, 0, 0, ImageFormat::None };
    return { image.data, image.cols, image.rows, fmt };
}

// return ZXing::Results
inline ZXing::Results DecodeBarcodes(const cv::Mat& image, const ZXing::DecodeHints& hints = {})
{
    return ZXing::ReadBarcodes(ImageViewFromMat(image), hints);
}

void DecodeQRcode(const Nan::FunctionCallbackInfo<v8::Value> &info)
{
  Isolate *isolate = info.GetIsolate();

  Local<Array> arr = Local<Array>::Cast(info[0]);
  printf("size %d\n", arr->Length());
  
  // convert info[0] to uchar array
  // uchar data[] = {}; <- from node js uint8clampedarray data convert ??
  // cv::Mat QRCodeImage(imgHeight,imgWidth, CV_8UC4, data);
  // auto results = ReadBarcodes(QRCodeImage);
  // convert ZXing::Results to js object?

  info.GetReturnValue().Set(results );
}
void Init(v8::Local<v8::Object> exports)
{
  exports->Set(context,
               Nan::New("decodeQRcode").ToLocalChecked(),
               Nan::New<v8::FunctionTemplate>(DecodeQRcode)
                   ->GetFunction(context)
                   .ToLocalChecked());
}
NODE_MODULE(addon, Init)

Solution

  • Nan::TypedArrayContents<uchar> contents(info[0]);
    if (contents.length() < min_length) printf("not good\n");
    uchar *data = *contents;
    

    And of course you will need to protect it from the garbage collector if you are going to be using this data after you have returned to your JS caller.