Move QR code decoding to c++ module
This commit is contained in:
parent
7a13431dfa
commit
a11fee5ff1
8 changed files with 78 additions and 487 deletions
|
@ -21,6 +21,7 @@
|
|||
#include "mirror.h"
|
||||
#include "misc.h"
|
||||
#include "motivate.h"
|
||||
#include "qr.h"
|
||||
#include "resize.h"
|
||||
#include "reverse.h"
|
||||
#include "scott.h"
|
||||
|
@ -56,6 +57,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
|
|||
exports.Set(Napi::String::New(env, "meme"), Napi::Function::New(env, Meme));
|
||||
exports.Set(Napi::String::New(env, "mirror"), Napi::Function::New(env, Mirror));
|
||||
exports.Set(Napi::String::New(env, "motivate"), Napi::Function::New(env, Motivate));
|
||||
exports.Set(Napi::String::New(env, "qrread"), Napi::Function::New(env, QrRead));
|
||||
exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
|
||||
exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse));
|
||||
exports.Set(Napi::String::New(env, "scott"), Napi::Function::New(env, Scott));
|
||||
|
|
52
natives/qr.cc
Normal file
52
natives/qr.cc
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <napi.h>
|
||||
#include <iostream>
|
||||
#include <ZXing/ReadBarcode.h>
|
||||
#include <ZXing/TextUtfEncoding.h>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace ZXing;
|
||||
|
||||
class QrReadWorker : public Napi::AsyncWorker {
|
||||
public:
|
||||
QrReadWorker(Napi::Function& callback, string in_path)
|
||||
: Napi::AsyncWorker(callback), in_path(in_path) {}
|
||||
~QrReadWorker() {}
|
||||
|
||||
void Execute() {
|
||||
int width, height, channels;
|
||||
unique_ptr<stbi_uc, void(*)(void*)> buffer(stbi_load(in_path.c_str(), &width, &height, &channels, 4), stbi_image_free);
|
||||
auto result = ReadBarcode(width, height, buffer.get(), width * 4, 4, 0, 1, 2, { BarcodeFormat::QR_CODE });
|
||||
if (result.isValid()) {
|
||||
final = TextUtfEncoding::ToUtf8(result.text());
|
||||
missing = false;
|
||||
} else {
|
||||
final = "";
|
||||
}
|
||||
}
|
||||
|
||||
void OnOK() {
|
||||
Napi::Object object = Napi::Object::New(Env());
|
||||
object.Set("qrText", final);
|
||||
object.Set("missing", missing);
|
||||
Callback().Call({Env().Undefined(), object});
|
||||
}
|
||||
|
||||
private:
|
||||
string in_path, final;
|
||||
bool missing = true;
|
||||
};
|
||||
|
||||
Napi::Value QrRead(const Napi::CallbackInfo &info)
|
||||
{
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
string in_path = info[0].As<Napi::String>().Utf8Value();
|
||||
Napi::Function cb = info[1].As<Napi::Function>();
|
||||
|
||||
QrReadWorker* qrReadWorker = new QrReadWorker(cb, in_path);
|
||||
qrReadWorker->Queue();
|
||||
return env.Undefined();
|
||||
}
|
8
natives/qr.h
Normal file
8
natives/qr.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef ESMBOT_NATIVES_QR_H_
|
||||
#define ESMBOT_NATIVES_QR_H_
|
||||
|
||||
#include <napi.h>
|
||||
|
||||
Napi::Value QrRead(const Napi::CallbackInfo& info);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue