掃描識(shí)別工具Dynamic Web TWAIN使用教程:桌面瀏覽器捕獲(下)
本文將繼續(xù)與大家分享如何在Web應(yīng)用程序中使用桌面相機(jī)。
【Dynamic Web TWAIN最新版免費(fèi)下載>>>】
如何實(shí)現(xiàn)?
在文本編輯器中打開ScanOrCapture.html
對(duì)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.pdf.js"></script> <script type="text/javascript" src="../dist/addon/dynamsoft.webtwain.addon.webcam.js"></script>
這里引用的文件是
用于核心SDK Dynamic Web TWAIN的JS庫(kù)
node_modules\dwt\dis\dynamsoft.webtwain.initiate.js
node_modules\dwt\dis\dynamsoft.webtwain.config.js
和用于網(wǎng)絡(luò)攝像頭附加組件(Webcam Add-on)的JS庫(kù)
node_modules\dwt\dist\addon\dynamsoft.webtwain.addon.webcam.js
PDF Rasterizer不是必需的,但也可查看PDF Rasterizer
node_modules\dwt\dist\addon\dynamsoft.webtwain.addon.pdf.js
如果以前在本地安裝了Dynamic Web TWAIN,則也可以在以下目錄中找到相同的文件。
C:\Program Files (x86)\Dynamsoft\Dynamic Web TWAIN SDK {version number} {Trial}\Sample\Scan+Webcam\Resources\
C:\Program Files (x86)\Dynamsoft\Dynamic Web TWAIN SDK {version number} {Trial}\Sample\Scan+Webcam\Resources\addon\
運(yùn)行時(shí)初始化代碼
<div id="dwtcontrolContainer"></div> function Dynamsoft_OnReady() { DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); document.getElementById('source').onchange = function () { if (document.getElementById('source').selectedIndex < webCamStartingIndex) { DWObject.Addon.Webcam.StopVideo(); isVideoOn = false; document.getElementById("btn-grab").style.backgroundColor = ""; document.getElementById('btn-grab').value = 'Acquire From a Scanner'; document.getElementById("btn-switch").style.display = 'none'; } else { DWObject.Addon.Webcam.SelectSource(document.getElementById("source").options[document.getElementById("source").selectedIndex].text); SetIfWebcamPlayVideo(true); document.getElementById('btn-grab').value = 'Acquire From a Webcam'; document.getElementById("btn-switch").style.display = ''; } document.getElementById("btn-grab").disabled = ""; } if (DWObject) { if (!Dynamsoft.Lib.product.bChromeEdition) { DWObject.Height = 350; DWObject.Width = 270; } if (Dynamsoft.Lib.detect.ssl) { DWObject.IfSSL = true; DWObject.HTTPPort = 443; } DWObject.Addon.Webcam.Download(Dynamsoft.WebTwainEnv.ResourcesPath + '/dist/DynamicWebcam.zip', function () { document.getElementById('source').options.length = 0; var count = DWObject.SourceCount; for (var i = 0; i < count; i++) { document.getElementById('source').options.add(new Option(DWObject.GetSourceNameItems(i), i)); } webCamStartingIndex = i; var arySource = DWObject.Addon.Webcam.GetSourceList(); for (var i = 0; i < arySource.length; i++) document.getElementById("source").options.add(new Option(arySource[i]), i + webCamStartingIndex); // Get Webcam Source names and put them in a drop-down box document.getElementById('source').onchange(); }, function (errCode, errString) { console.log('Webcam DLL failed to download with error: ' + errString); }); } }
如上面的代碼所示,在初始化期間頁(yè)面上只有一個(gè)container(DIV元素)。它顯示視頻流以及來(lái)自網(wǎng)絡(luò)攝像頭或掃描儀的捕獲圖像。
在初始化期間,方法Addon.Webcam.GetSourceList()用于獲取所有可用網(wǎng)絡(luò)攝像頭的列表。選擇網(wǎng)絡(luò)攝像頭源時(shí),使用方法Addon.Webcam.SelectSource(strWebcamName)。
使用插件
function SetIfWebcamPlayVideo(bShow) { if (bShow) { DWObject.Addon.Webcam.StopVideo(); setTimeout(function () { DWObject.Addon.Webcam.PlayVideo(DWObject, 80, function () {}); isVideoOn = true; document.getElementById("btn-grab").style.backgroundColor = ""; document.getElementById("btn-grab").disabled = ""; document.getElementById("btn-switch").value = "Hide Video"; }, 30); } else { DWObject.Addon.Webcam.StopVideo(); isVideoOn = false; document.getElementById("btn-grab").style.backgroundColor = "#aaa"; document.getElementById("btn-grab").disabled = "disabled"; document.getElementById("btn-switch").value = "Show Video"; } } function SwitchViews() { if (isVideoOn == false) { // continue the video SetIfWebcamPlayVideo(true); } else { // stop the video SetIfWebcamPlayVideo(false); } } function CaptureImage() { if (DWObject) { if (document.getElementById('source').selectedIndex < webCamStartingIndex) { DWObject.IfShowUI = true; DWObject.IfDisableSourceAfterAcquire = true; DWObject.SelectSourceByIndex(document.getElementById('source').selectedIndex); DWObject.CloseSource(); DWObject.OpenSource(); DWObject.AcquireImage(); } else { var funCaptureImage = function () { setTimeout(function () { SetIfWebcamPlayVideo(false); }, 50); }; DWObject.Addon.Webcam.CaptureImage(funCaptureImage, funCaptureImage); } } }
選擇網(wǎng)絡(luò)攝像頭后,將調(diào)用Addon.Webcam.PlayVideo(DWObject,nQuality,onFrameCaptured)方法在container(容器)中播放視頻流。然后,你可以使用Addon.Webcam.CaptureImage(OnCaptureSuccess,OnCaptureError)捕獲幀, 并調(diào)用Addon.Webcam.StopVideo()來(lái)停止視頻流,以便捕獲的幀/圖像顯示出來(lái)。如果視頻流仍然顯示,則捕獲的幀/圖像將不可見,這是由于樣本對(duì)視頻流和圖像使用相同的container。你還可以將兩個(gè)containers放在同一頁(yè)面上,并使一個(gè)用于視頻流,另一個(gè)用于捕獲的圖像。
你還可查看在線演示。