下面是一个端到端的提交的例子ExtractCheck使用节点请求。js编程语言:
/ /简单的脚本,过程结果ExtractCheck LEADTOOLS云服务请求。/ /为了运行此脚本,以下更改需要补充道:/ / 1)把你的应用程序ID在用户getRequestOptions方法的一部分。/ / 2)把你的应用程序密码的密码部分getRequestOptions方法。/ / / /脚本将执行以下操作顺序:/ / 1)执行ExtractCheck LEADTOOLS云服务请求。一个成功的请求将返回一个惟一的标识符。/ / 2)我们将使用GUID查询服务,如果文件完成处理,身体将包含所有的/ /请求数据。/ / 3)我们将返回的json数据,解析它,显示返回的所有信息。/ / / /这个脚本使用了以下NodeJS库:/ / 1)请求-关于这个库的更多信息可以在这里找到:https://github.com/request/request / / 2)文件系统——关于这个库的更多信息可以在这里找到:https://nodejs.org/api/fs.html const请求=要求(“请求”);/ /如果上传一个文件作为多部分的内容,我们需要安装的文件系统库。/ / const fs =要求(fs); var servicesUrl = "https://azure.leadtools.com/api/"; //The first page in the file to mark for processing var firstPage = 1; //Sending a value of -1 will indicate to the services that the rest of the pages in the file should be processed. var lastPage = -1; //We will be uploading the file via a URl. Files can also be passed by adding a PostFile to the request. Only 1 file will be accepted per request. //The services will use the following priority when determining what a request is trying to do GUID > URL > Request Body Content var fileURL = 'http://demo.leadtools.com/images/cloud_samples/micr_sample.jpg'; var recognitionUrl = servicesUrl + 'Recognition/ExtractCheck?firstPage=' + firstPage + '&lastPage=' + lastPage + '&fileurl=' + fileURL; request.post(getRequestOptions(recognitionUrl), recognitionCallback); //If uploading a file as multi-part content: //var recognitionUrl = servicesUrl + 'Recognition/ExtractCheck?firstPage=' + firstPage + '&lastPage=' + lastPage; //var req = request.post(getRequestOptions(recognitionUrl), recognitionCallback); //var form = req.form(); //form.append('file', fs.createReadStream('path\to\inputFile')); function recognitionCallback(error, response, body) { if (!error && response.statusCode === 200) { var guid = body; console.log("Unique ID returned by the Services: " + guid); queryServices(guid); } } function queryServices(guid) { //Function to query the status of a request. If the request has not yet finished, this function will recursively call itself until the file has finished. var queryUrl = servicesUrl + "Query?id=" + guid; request.post(getRequestOptions(queryUrl), async function (error, response, body) { var results = JSON.parse(body); if (!error && (results['FileStatus'] !== 100 && results['FileStatus'] !== 123)) { console.log("File finished processing with return code: " + response.statusCode); console.log(results['FileStatus']); if (results['FileStatus'] !== 200) { return; } console.log("Results: \n"); parseJson(results['RequestData']); } else { //The file has not yet finished processing. await function () { return new Promise(resolve => setTimeout(resolve, 5000)); //Sleep for 5 seconds before trying again }; queryServices(guid); //Call the method again. } }); } function parseJson(jsonObject) { //Function to decode the JSON object that was returned by the LEADTOOLS CloudServices. for (let i = 0; i < jsonObject.length; i++) { var currentRequest = jsonObject[i]; console.log("Service Type: " + currentRequest['ServiceType']); if (currentRequest['ServiceType'] === 'Recognition' && currentRequest['RecognitionType'] === 'MICR') { console.log("Recognition Method: " + currentRequest['RecognitionType']); var data = currentRequest['data']; for(let k = 0; k < jsonObject.length; k++){ var currentMicrResult = data[k]; for (var key in currentMicrResult) { console.log(key + ":"); console.log("Text: " + currentMicrResult[key]['Text']); console.log("Bounds: " + currentMicrResult[key]['Bounds']); console.log("------------------------------------") } } } } } function getRequestOptions(url) { //Function to generate and return HTTP request options. var requestOptions = { url: url, //If uploading a file as multi-part content, remove the Content-Length header. headers: { 'Content-Length': 0 }, auth: { user: "Replace with Application ID", password: "Replace with Application Password" } }; return requestOptions; }