@autojs/opencv
Version:
Asynchronous OpenCV 3.x nodejs bindings with JavaScript and TypeScript API for Auto.js Pro.
63 lines (53 loc) • 2 kB
JavaScript
const fs = require('fs')
const path = require('path')
if (typeof (process.env.AUTOJS_NATIVE_LIBRARY_PATH) === 'undefined') {
throw new Error('Not supported Auto.js Pro version. Sees https://g.pro.autojs.org/docs/v9 .')
}
const targetBinDir = path.join(process.env.AUTOJS_NATIVE_LIBRARY_PATH, 'opencv4autojs')
const package = require('../package.json')
const version = package.prebuiltVersion
const targetBinPath = path.join(targetBinDir, `${version}.${process.arch}.node`)
const hasBinaryFile = fs.existsSync(targetBinPath) || copyBin(targetBinDir, targetBinPath);
function copyBin(binDir, binPath) {
if (fs.existsSync(binDir)) {
fs.readdirSync(binDir).forEach(file => fs.unlinkSync(path.join(binDir, file)))
} else {
fs.mkdirSync(binDir, { recursive: true })
}
const sourceBinPath = path.join(__dirname, `../prebuilt/opencv4autojs.${process.arch}.node`)
if (!fs.existsSync(sourceBinPath)) {
return false;
}
fs.copyFileSync(sourceBinPath, binPath)
return true;
}
module.exports = hasBinaryFile ? require(targetBinPath) : missingLibrary({
drawTextBox: missingLibraryFunction(),
drawDetection: missingLibraryFunction(),
calcHist: missingLibraryFunction(),
Mat: missingLibrary({}),
__esModule: null,
});
function missingLibrary(obj) {
return new Proxy(obj, {
get: (target, prop) => {
if (target.hasOwnProperty(prop)) {
return target[prop];
}
return missingLibraryError(prop);
},
});
}
function missingLibraryFunction() {
return function () {
return missingLibraryError();
}
}
class MissingLibraryError extends Error {
constructor(msg) {
super(msg);
}
}
function missingLibraryError(prop) {
throw new MissingLibraryError(`The binary file opencv4autojs.${process.arch}.node does not exist, please ensure the optimization option 'removeOpenCv' is false on building apk.(Access property ${prop})`);
}