UNPKG

dwt

Version:

Dynamic Web TWAIN is a TWAIN/ICA/SANE-based scanning SDK software specifically designed for web applications running on Windows/macOS/Linux. With just a few lines of code, you can develop robust applications to scan documents from TWAIN/ICA/SANE-compatibl

142 lines (130 loc) 6.64 kB
<!DOCTYPE html> <html> <head> <title>OCR scanned or loaded documents</title> <script type="text/javascript" src="../dist/dynamsoft.webtwain.min.js"></script> </head> <body> <h1>Scan or Load documents and OCR them (English as an example)</h1> <h4>Other languages are also supported, contact support@dynamsoft.com for more info.</h4> <input type="button" value="Scan Documents" onclick="AcquireImage();" /> <input type="button" value="Load Images or PDFs" onclick="LoadImages();" style="margin-right: 20px;" /> <input type="button" value="OCR An Image with English" onclick="DoOCR();" /> <br /> <br /> <div id="dwtcontrolContainer" style="float:left;"></div> <div id="divNoteMessage" ondblclick="this.innerHTML='';" style="margin:0px 20px; float:left;width:300px; height:350px; overflow: auto;background-color:#e7f2fd;border:solid 1px black;"></div> <br /> <script type="text/javascript"> window.onload = function () { if (Dynamsoft && (!Dynamsoft.Lib.env.bWin || !Dynamsoft.Lib.product.bChromeEdition)) { var ObjString = []; ObjString.push('<div style="padding:0 20px;">'); ObjString.push( "Please note that your current browser doesn't support the OCR add-on, please use modern browsers like Chrome, Firefox, Edge or IE 11." ); ObjString.push('</div>'); Dynamsoft.WebTwainEnv.ShowDialog(400, 180, ObjString.join('')); if (document.getElementsByClassName("dynamsoft-dialog-close")) document.getElementsByClassName("dynamsoft-dialog-close")[0].style.display = "none"; } else { Dynamsoft.WebTwainEnv.Load(); } }; Dynamsoft.WebTwainEnv.ProductKey = 't0140cQMAAGnOvWTyoOR4HEFckJJmzMWpZcPSHyXGAvYGxgEkg5fBnRoFPslaAayuNOe5B/gp7plUCIUAtf6Ttb98d7Ifv/3A6Mxsu7CZLJhKHUuMorfuu/E/ZrOfuSyoMz7zjXKjgvHcMO1HiGbvyHv+GBWM54ZpP4Wej2RorGBUMJ4b4tx40yqnXlIiqvs='; //2020-04-24 Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady); // Register OnWebTwainReady event. This event fires as soon as Dynamic Web TWAIN is initialized and ready to be used var DWObject; function Dynamsoft_OnReady() { DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); // Get the Dynamic Web TWAIN object that is embeded in the div with id 'dwtcontrolContainer' if (DWObject) { downloadOCRBasic(true); } } function downloadOCRBasic(bDownloadDLL) { var strOCRPath = Dynamsoft.WebTwainEnv.ResourcesPath + "OCRResources/OCR.zip", strOCRLangPath = Dynamsoft.WebTwainEnv.ResourcesPath + 'OCRResources/OCRBasicLanguages/English.zip'; if (bDownloadDLL) { if (DWObject.Addon.OCR.IsModuleInstalled()) { /*console.log('OCR dll is installed');*/ downloadOCRBasic(false); } else { 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); }); } } function AcquireImage() { if (DWObject) { DWObject.SelectSource(function () { var OnAcquireImageSuccess, OnAcquireImageFailure; OnAcquireImageSuccess = OnAcquireImageFailure = function () { DWObject.CloseSource(); }; DWObject.OpenSource(); DWObject.IfDisableSourceAfterAcquire = true; DWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure); }, function () { console.log('SelectSource failed!'); }); } } function LoadImages() { if (DWObject) { if (DWObject.Addon && DWObject.Addon.PDF) { DWObject.Addon.PDF.SetResolution(300); DWObject.Addon.PDF.SetConvertMode(EnumDWT_ConvertMode.CM_RENDERALL); } DWObject.LoadImageEx('', 5, function () {}, function (errorCode, errorString) { alert('Load Image:' + errorString); } ); } } 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); } ); } } </script> </body> </html>