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

219 lines (211 loc) 9.07 kB
<!DOCTYPE html> <html> <head> <title>Read Barcode on Scanned or Loaded Documents</title> <script type="text/javascript" src="../dist/dynamsoft.webtwain.min.js"></script> <script type="text/javascript" src="../dist/addon/dynamsoft.barcodereader.initiate.js"></script> <script type="text/javascript" src="../dist/addon/dynamsoft.barcodereader.config.js"></script> </head> <body> <h1>Read Barcode on Scanned or Loaded Documents</h1> <input type="button" value="Scan Documents" onclick="AcquireImage();" /> <input type="button" value="Load Images or PDFs" onclick="LoadImages();" style="margin-right: 20px;" /> <select id="barcodeformat"></select> <input type="button" value="Read Barcode" onclick="ReadBarcode();" /> <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> <script type="text/javascript"> window.onload = function () { if (Dynamsoft && (!Dynamsoft.Lib.env.bWin)) { var ObjString = []; ObjString.push('<div class="p15">'); ObjString.push("Please note that the Barcode Reader currently only works on Windows."); 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 { if (!Dynamsoft.Lib.product.bChromeEdition) { Dynamsoft.WebTwainEnv.Containers[0].Height = '1px;'; Dynamsoft.WebTwainEnv.Containers[0].Width = '1px;'; } Dynamsoft.WebTwainEnv.Load(); } }; Dynamsoft.WebTwainEnv.ProductKey = 't0140cQMAAGnOvWTyoOR4HEFckJJmzMWpZcPSHyXGAvYGxgEkg5fBnRoFPslaAayuNOe5B/gp7plUCIUAtf6Ttb98d7Ifv/3A6Mxsu7CZLJhKHUuMorfuu/E/ZrOfuSyoMz7zjXKjgvHcMO1HiGbvyHv+GBWM54ZpP4Wej2RorGBUMJ4b4tx40yqnXlIiqvs='; //2020-04-24 if (Dynamsoft && Dynamsoft.WebTwainEnv && Dynamsoft.WebTwainEnv.ProductKey) dynamsoft.dbrEnv.productKey = [Dynamsoft.WebTwainEnv.ProductKey, dynamsoft.dbrEnv.productKey].join(";"); Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady); var DWObject, dbrObject, barcodeText = []; var BarcodeInfo = [{ desc: "All", val: -32505857 }, { desc: "1D Barcodes", val: 0x000007FF }, { desc: "Aztec", val: 268435456 }, { desc: "QR Code", val: 67108864 }, { desc: "PDF417", val: 33554432 }, { desc: "DATAMATRIX", val: 134217728 }, { desc: "CODE_39", val: 1 }, { desc: "CODE_128", val: 2 }, { desc: "CODE_93", val: 4 }, { desc: "CODABAR", val: 8 }, { desc: "EAN_13", val: 32 }, { desc: "EAN_8", val: 64 }, { desc: "UPC_A", val: 128 }, { desc: "UPC_E", val: 256 }, { desc: "Interleaved 2 of 5 (ITF)", val: 16 }, { desc: "Industrial 2 of 5", val: 512 } ]; 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) { if (!Dynamsoft.Lib.product.bChromeEdition) { DWObject.Height = 350; DWObject.Width = 270; } for (var index = 0; index < BarcodeInfo.length; index++) document.getElementById("barcodeformat").options.add(new Option(BarcodeInfo[index].desc, index)); dynamsoft.BarcodeReader.initServiceConnection().then(function () { dbrObject = new dynamsoft.BarcodeReader(); }, function (ex) { alert('Init failed: ' + (ex.message || ex)); }); } } 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 ReadBarcode() { if (DWObject) { if (DWObject.HowManyImagesInBuffer == 0) { alert("Please scan or load an image first."); return; } var settings = dbrObject.getRuntimeSettings(); /** * Setting up the barcode reader */ settings.barcodeFormatIds = BarcodeInfo[document.getElementById("barcodeformat").selectedIndex].val; settings.expectedBarcodesCount = 512; settings.scaleDownThreshold = 2147483647; /** End of settings */ dbrObject.updateRuntimeSettings(settings); var index = DWObject.CurrentImageIndexInBuffer; var barcodeImage = DWObject.GetImagePartURL(index); var objDiv = document.getElementById('divNoteMessage'); dbrObject.decode(barcodeImage).then(function ( results) { //This is the function called when barcode is read successfully //Retrieve barcode details var _now = new Date().toLocaleTimeString() + "<br />"; objDiv.innerHTML += _now; if (results.length == 0) { objDiv.innerHTML += "Nothing Found!<br />"; } else { objDiv.innerHTML += "<p style='color: #fe8e14'>Barcode Found!!</p>"; barcodeText = []; for (i = 0; i < results.length; i++) { var result = results[i]; Barcode_text = result.BarcodeText; var loc = result.LocalizationResult; var x = loc.X1; var y = loc.Y1; var format = result.BarcodeFormatString; barcodeText.push("barcode[" + (i + 1) + "]: " + "<br />"); barcodeText.push("<strong>" + Barcode_text + "</strong>"); barcodeText.push("<br />format:" + format + "<br />"); barcodeText.push("x:" + x + "y:" + y + "<br />"); barcodeText.push("------------------------------<br />"); } barcodeText.splice(0, 0, '<p style="padding:5px; margin:0;">'); barcodeText.push('</p>'); objDiv.innerHTML += barcodeText.join(''); } objDiv.scrollTop = objDiv.scrollHeight; }, function (ex) { //This is the function called when barcode reading fails objDiv.innerHTML = ex.message || ex; }); } } </script> </body> </html>