[英]Node.js: IMPOSSIBLE to Use Callback Result in Global Variable?
我知道这个问题已经被问过很多次了,但是我现在在这个问题上已经花了19.5个小时,没有任何结果,而且我不能指望自己能做到这一点。 我已经在互联网上搜索了指南和提示,但这似乎是不可能的。 我已经尝试过Promises和所有方法,但这是行不通的。
数以百万计的互联网提供给帮助者。
我的脚本(auto.js)读取图片(其中包含文本“A”或“B”)的正方体 ,然后使用结果文本在我的脚本作为一个全局变量从对象数组检查数量和做其他事( 这我不能在node-tesseract里面做 ,但是我什么也做不了,但是将结果记录在控制台上。 如果我尝试以其他方式访问它,则它不允许我这样做。
PS对于beta测试,我仅使用Node Command Prompt(“ node auto”)运行代码。
auto.js使用以下命令调用node-tesseract模块:
var tesseract = require('node-tesseract');
auto.js接收Tesseract的结果,并使用JQuery将其转换为数字:
require("jsdom").env("", function(err, window) {
var $ = require("jquery")(window);
// Char is Tesseract's result, let's convert it to numbers.
var charNums = [
{ char: 'A', nums: '1234'},
{ char: 'B', nums: '5678'}
];
function getNum(char)
{
var result = null;
var chars = $.grep(charNums, function(e){ return e.char === char; });
if (chars.length == 0) {
// not found
} else {
result = chars[0].nums;
}
return result;
}
// 2.jpg is the character's picture, let's convert it to text.
tesseract.process('2.jpg', function(err, text) {
var foo = "asd"; // For a test.
foo = "bef"; // For a test.
console.log("1: "+foo); // This works.
console.log("2: "+text); // This works.
foo = ""+text; // For a test.
console.log("3: "+foo); // This works.
foo = ""+getNum(text); //Important.
console.log("4: "+foo); //This does not work.
});
});
'use strict';
/**
* Module dependencies.
*/
var utils = require('./utils');
var exec = require('child_process').exec;
var fs = require('fs');
var tmpdir = require('os').tmpdir(); // let the os take care of removing zombie tmp files
var uuid = require('node-uuid');
var path = require('path');
var glob = require("glob");
var Tesseract = {
tmpFiles: [],
/**
* options default options passed to Tesseract binary
* @type {Object}
*/
options: {
'l': 'eng',
'psm': 10,
'config': null,
'binary': 'tesseract'
},
/**
* outputEncoding
* @type {String}
*/
outputEncoding: 'UTF-8',
/**
* Runs Tesseract binary with options
*
* @param {String} image
* @param {Object} options to pass to Tesseract binary
* @param {Function} callback
*/
process: function(image, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = utils.merge(Tesseract.options, options);
// generate output file name
var output = path.resolve(tmpdir, 'node-tesseract-' + uuid.v4());
// add the tmp file to the list
Tesseract.tmpFiles.push(output);
// assemble tesseract command
var command = [options.binary, image, output];
if (options.l !== null) {
command.push('-l ' + options.l);
}
if (options.psm !== null) {
command.push('-psm ' + options.psm);
}
if (options.config !== null) {
command.push(options.config);
}
command = command.join(' ');
var opts = options.env || {};
// Run the tesseract command
exec(command, opts, function(err) {
if (err) {
// Something went wrong executing the assembled command
callback(err, null);
return;
}
// Find one of the three possible extension
glob(output + '.+(html|hocr|txt)', function(err, files){
if (err) {
callback(err, null);
return;
}
fs.readFile(files[0], Tesseract.outputEncoding, function(err, data) {
if (err) {
callback(err, null);
return;
}
var index = Tesseract.tmpFiles.indexOf(output);
if (~index) Tesseract.tmpFiles.splice(index, 1);
fs.unlink(files[0]);
callback(null, data)
});
})
}); // end exec
}
};
function gc() {
for (var i = Tesseract.tmpFiles.length - 1; i >= 0; i--) {
try {
fs.unlinkSync(Tesseract.tmpFiles[i] + '.txt');
} catch (err) {}
var index = Tesseract.tmpFiles.indexOf(Tesseract.tmpFiles[i]);
if (~index) Tesseract.tmpFiles.splice(index, 1);
};
}
var version = process.versions.node.split('.').map(function(value) {
return parseInt(value, 10);
});
if (version[0] === 0 && (version[1] < 9 || version[1] === 9 && version[2] < 5)) {
process.addListener('uncaughtException', function _uncaughtExceptionThrown(err) {
gc();
throw err;
});
}
// clean up the tmp files
process.addListener('exit', function _exit(code) {
gc();
});
/**
* Module exports.
*/
module.exports.process = Tesseract.process;
});
var getChar = function(callback) {
tesseract.process('2.jpg', function(err, text) {
var foo = "asd"; // For a test.
foo = "bef"; // For a test.
console.log("1: "+foo); // This works.
console.log("2: "+text); // This works and gives right result. Why only this?
text = text.trim();
text = text.toUpperCase();
callback(text);
});
};
var getNums = function (callback) {
getChar(function(data){
/* This data stack 2 */
callback(data);
});
};
function getNum(){
getNums(function(data){
var $ = require("jquery")(window);
var charNums = [
{ char: 'A', nums: '1234'},
{ char: 'B', nums: '2345'},
{ char: 'C', nums: '3456'},
{ char: 'D', nums: '4567'},
{ char: 'E', nums: '5678'},
{ char: 'F', nums: '6789'},
{ char: 'G', nums: '7890'},
{ char: 'H', nums: '0987'},
{ char: 'J', nums: '8765'},
{ char: 'K', nums: '7654'},
{ char: 'L', nums: '6543'},
{ char: 'M', nums: '5432'},
{ char: 'N', nums: '4321'},
{ char: 'P', nums: '3210'},
{ char: 'R', nums: '2109'},
{ char: 'S', nums: '1098'}
];
var chars = $.grep(charNums, function(e){ return e.char === data; });
if (chars.length == 0) {
} else {
var result = chars[0].nums;
}
console.log("Character: ", data);
console.log("Number:", result);
// DO SOMETHING
});
}
getNum();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.