扫描识别工具Dynamic Web TWAIN使用教程:OCR(下)
在Web应用程序中快速实现文本识别
上一篇文章与大家分享了在Web应用程序中快速实现文本识别的环境和步骤,本文将给大家介绍如何通过代码来实现这一功能。
如何实现
在文本编辑器中打开 OCRADocument.html
对Core JavaScript文件的引用
<script type="text/javascript" src="../dist/dynamsoft.webtwain.initiate.js"></script> <script type="text/javascript" src="../dist/dynamsoft.webtwain.config.js"></script> <script type="text/javascript" src="../dist/addon/dynamsoft.webtwain.addon.ocr.js"></script> <script type="text/javascript" src="../dist/addon/dynamsoft.webtwain.addon.pdf.js"></script>
这里引用的文件是
用于核心SDK Dynamic Web TWAIN的JS库
-
node_modules\dwt\dis\dynamsoft.webtwain.initiate.js
-
node_modules\dwt\dis\dynamsoft.webtwain.config.js
Dynamsoft OCR Basic的JS库
-
node_modules\dwt\dist\addon\dynamsoft.webtwain.addon.ocr.js
-
node_modules\dwt\dist\addon\dynamsoft.webtwain.addon.pdf.js
如果你以前在本地安装了Dynamic Web TWAIN,则还可以在以下目录中找到相同的文件(dynamsoft.webtwain.addon.pdf.js除外)。
Dynamsoft OCR Basic runtime安装代码
function downloadOCRBasic(bDownloadDLL) {
var strOCRPath = Dynamsoft.WebTwainEnv.ResourcesPath + "/OCRResources/OCR.zip",
strOCRLangPath = Dynamsoft.WebTwainEnv.ResourcesPath + '/OCRResources/OCRBasicLanguages/English.zip';
if (bDownloadDLL) {
DWObject.Addon.OCR.Download(
strOCRPath,
function () {/*console.log('OCR dll is installed');*/
downloadOCRBasic(false);
},
function (errorCode, errorString) {
console.log(errorString);
}
);
} else {
DWObject.Addon.OCR.DownloadLangData(
strOCRLangPath,
function () {
}, function (errorCode, errorString) {
console.log(errorString);
});
}
}
如上面的代码所示,Dynamsoft OCR Basic安装需要两个步骤。第一步是使用 DWObject.Addon.OCR.Download 接口安装核心DLL(来自“/OCRResources/OCR.zip”的DynamicOCR.dll)。 第二步是使用DWObject.Addon.OCR.DownloadLangData 接口安装OCR语言包或识别字典('/ OCRResources / OCRBasicLanguages / English.zip')。此处仅安装英语词典,因此该程序只能识别英语。 如果你需要识别其他语言(总共27种主要语言),你可以下载完整的示例或参考此在线示例。
支持的语言:Arabic, Bengali, Chinese_Simplified, Chinese_Traditional, English, French, German, Hindi, Indonesian, Italian, Japanese, Javanese, Korean, Malay, Marathi, Panjabi, Persian, Portuguese, Russian, Spanish, Swahili, Tamil, Telugu, Thai, Turkish, Vietnamese, Urdu.
使用插件
function DoOCR() {
if (DWObject) {
if (DWObject.HowManyImagesInBuffer == 0) {
alert("Please scan or load an image first.");
return;
}
DWObject.Addon.OCR.SetLanguage('eng');
DWObject.Addon.OCR.SetOutputFormat(EnumDWT_OCROutputFormat.OCROF_TEXT);
DWObject.Addon.OCR.Recognize(
DWObject.CurrentImageIndexInBuffer,
function (sImageIndex, result) {
if (result == null)
return null;
var _textResult = (Dynamsoft.Lib.base64.decode(result.Get())).split(/\r?\n/g), _resultToShow = [];
for (var i = 0; i < _textResult.length; i++) {
if (i == 0 && _textResult[i].trim() == "")
continue;
_resultToShow.push(_textResult[i] + '<br />');
}
_resultToShow.splice(0, 0, '<p style="padding:5px; margin:0;">');
_resultToShow.push('</p>');
document.getElementById('divNoteMessage').innerHTML = _resultToShow.join('');
},
function (errorcode, errorstring, result) {
alert(errorstring);
}
);
核心代码是
DWObject.Addon.OCR.SetLanguage('eng'); //Set the language to be recognized
DWObject.Addon.OCR.SetOutputFormat(EnumDWT_OCROutputFormat.OCROF_TEXT); //Set the output format
DWObject.Addon.OCR.Recognize(... //Start Reconizing
查看支持的输出格式。
相关方法是 SetLanguage( ), SetOutputFormat( ),Recognize( ), RecognizeFile( ), RecognizeRect( ), RecognizeSelectedImages( )。
关于Dynamic Web TWAIN使用OCR插件的教程就到此结束啦,希望对你有所帮助~


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线