-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs_barcode_detector.cpp
241 lines (178 loc) · 7.52 KB
/
js_barcode_detector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "js_alloc.hpp"
#include "js_size.hpp"
#include "js_point.hpp"
#include "js_mat.hpp"
#include "js_umat.hpp"
#include <opencv2/barcode.hpp>
typedef cv::barcode::BarcodeDetector JSBarcodeDetectorClass;
typedef JSBarcodeDetectorClass JSBarcodeDetectorData;
extern "C" {
thread_local JSValue barcode_detector_proto = JS_UNDEFINED, barcode_detector_class = JS_UNDEFINED;
thread_local JSClassID js_barcode_detector_class_id;
}
extern "C" int js_barcode_detector_init(JSContext*, JSModuleDef*);
static JSValue
js_barcode_detector_wrap(JSContext* ctx, JSBarcodeDetectorData& wb) {
JSBarcodeDetectorData* ptr;
if(!(ptr = js_allocate<JSBarcodeDetectorData>(ctx)))
return JS_ThrowOutOfMemory(ctx);
JSValue ret = JS_NewObjectProtoClass(ctx, barcode_detector_proto, js_barcode_detector_class_id);
*ptr = wb;
JS_SetOpaque(ret, ptr);
return ret;
}
static JSValue
js_barcode_detector_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSValue proto, obj = JS_UNDEFINED;
JSBarcodeDetectorData* bd;
const char *prototxt_path = nullptr, *model_path = nullptr;
if(!(bd = js_allocate<cv::barcode::BarcodeDetector>(ctx)))
return JS_EXCEPTION;
if(argc > 0)
prototxt_path = JS_ToCString(ctx, argv[0]);
if(argc > 1)
model_path = JS_ToCString(ctx, argv[1]);
new(bd) cv::barcode::BarcodeDetector(prototxt_path ? prototxt_path : "", model_path ? model_path : "");
if(prototxt_path)
JS_FreeCString(ctx, prototxt_path);
if(model_path)
JS_FreeCString(ctx, model_path);
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_barcode_detector_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, bd);
return obj;
fail:
js_deallocate(ctx, bd);
fail2:
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
JSBarcodeDetectorData*
js_barcode_detector_data2(JSContext* ctx, JSValueConst val) {
return static_cast<JSBarcodeDetectorData*>(JS_GetOpaque2(ctx, val, js_barcode_detector_class_id));
}
static void
js_barcode_detector_finalizer(JSRuntime* rt, JSValue val) {
JSBarcodeDetectorData* wb = static_cast<JSBarcodeDetectorData*>(JS_GetOpaque(val, js_barcode_detector_class_id));
/* Note: 'wb' can be NULL in case JS_SetOpaque() was not called */
wb->~JSBarcodeDetectorClass();
js_deallocate<JSBarcodeDetectorData>(rt, wb);
}
enum {
METHOD_DETECT,
METHOD_DECODE,
METHOD_DETECT_AND_DECODE,
};
static JSValue
js_barcode_detector_method(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSBarcodeDetectorData* wb = static_cast<JSBarcodeDetectorData*>(JS_GetOpaque2(ctx, this_val, js_barcode_detector_class_id));
JSValue ret = JS_UNDEFINED;
switch(magic) {
case METHOD_DETECT: {
JSInputArray img = js_input_array(ctx, argv[0]);
std::vector<cv::Point2f> points;
// JSOutputArray points = js_cv_outputarray(ctx, argv[1]);
BOOL result = wb->detect(img, points);
ret = JS_NewBool(ctx, result);
js_array_copy(ctx, argv[1], points);
break;
}
case METHOD_DECODE: {
JSInputArray img = js_input_array(ctx, argv[0]);
JSInputArray points = js_input_array(ctx, argv[1]);
std::vector<std::string> decoded_info;
std::vector<cv::barcode::BarcodeType> decoded_type;
BOOL result = wb->decode(img, points, decoded_info, decoded_type);
ret = JS_NewBool(ctx, result);
js_array_copy(ctx, argv[2], decoded_info);
std::vector<int32_t> decoded(decoded_type.size());
std::copy(decoded_type.begin(), decoded_type.end(), decoded.begin());
js_array_copy(ctx, argv[3], decoded);
break;
}
case METHOD_DETECT_AND_DECODE: {
JSInputArray img = js_input_array(ctx, argv[0]);
std::vector<std::string> decoded_info;
std::vector<cv::barcode::BarcodeType> decoded_type;
std::vector<cv::Point2f> points;
BOOL result = wb->detectAndDecode(img, decoded_info, decoded_type, points);
ret = JS_NewBool(ctx, result);
js_array_copy(ctx, argv[1], decoded_info);
std::vector<int32_t> decoded(decoded_type.size());
std::copy(decoded_type.begin(), decoded_type.end(), decoded.begin());
js_array_copy(ctx, argv[2], decoded);
js_array_copy(ctx, argv[3], points);
break;
}
}
return ret;
}
JSClassDef js_barcode_detector_class = {
.class_name = "BarcodeDetector",
.finalizer = js_barcode_detector_finalizer,
};
const JSCFunctionListEntry js_barcode_detector_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("detect", 2, js_barcode_detector_method, METHOD_DETECT),
JS_CFUNC_MAGIC_DEF("decode", 4, js_barcode_detector_method, METHOD_DECODE),
JS_CFUNC_MAGIC_DEF("detectAndDecode", 4, js_barcode_detector_method, METHOD_DETECT_AND_DECODE),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "BarcodeDetector", JS_PROP_CONFIGURABLE),
};
const JSCFunctionListEntry js_barcode_detector_barcode_funcs[] = {
JS_PROP_INT32_DEF("NONE", cv::barcode::NONE, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("EAN_8", cv::barcode::EAN_8, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("EAN_13", cv::barcode::EAN_13, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("UPC_A", cv::barcode::UPC_A, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("UPC_E", cv::barcode::UPC_E, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("UPC_EAN_EXTENSION", cv::barcode::UPC_EAN_EXTENSION, JS_PROP_ENUMERABLE),
};
const JSCFunctionListEntry js_barcode_detector_static_funcs[] = {
JS_OBJECT_DEF("barcode", js_barcode_detector_barcode_funcs, countof(js_barcode_detector_barcode_funcs), JS_PROP_C_W_E),
};
extern "C" int
js_barcode_detector_init(JSContext* ctx, JSModuleDef* bd) {
/* create the BarcodeDetector class */
JS_NewClassID(&js_barcode_detector_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_barcode_detector_class_id, &js_barcode_detector_class);
barcode_detector_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, barcode_detector_proto, js_barcode_detector_proto_funcs, countof(js_barcode_detector_proto_funcs));
JS_SetClassProto(ctx, js_barcode_detector_class_id, barcode_detector_proto);
barcode_detector_class = JS_NewCFunction2(ctx, js_barcode_detector_constructor, "BarcodeDetector", 0, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, barcode_detector_class, barcode_detector_proto);
if(bd) {
JS_SetModuleExport(ctx, bd, "BarcodeDetector", barcode_detector_class);
JS_SetModuleExportList(ctx, bd, js_barcode_detector_static_funcs, countof(js_barcode_detector_static_funcs));
}
return 0;
}
void
js_barcode_detector_constructor(JSContext* ctx, JSValue parent, const char* name) {
if(JS_IsUndefined(barcode_detector_class))
js_barcode_detector_init(ctx, 0);
JS_SetPropertyStr(ctx, parent, name ? name : "BarcodeDetector", barcode_detector_class);
}
#ifdef JS_BarcodeDetector_MODULE
#define JS_INIT_MODULE VISIBLE js_init_module
#else
#define JS_INIT_MODULE js_init_module_barcode_detector
#endif
extern "C" void
js_barcode_detector_export(JSContext* ctx, JSModuleDef* bd) {
JS_AddModuleExport(ctx, bd, "BarcodeDetector");
JS_AddModuleExportList(ctx, bd, js_barcode_detector_static_funcs, countof(js_barcode_detector_static_funcs));
}
extern "C" JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* bd;
bd = JS_NewCModule(ctx, module_name, &js_barcode_detector_init);
if(!bd)
return NULL;
js_barcode_detector_export(ctx, bd);
return bd;
}