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
162 lines (148 loc) • 7.46 kB
HTML
<html>
<head>
<title>Acquire Images From Scanners and Cameras</title>
<script type="text/javascript" src="../dist/dynamsoft.webtwain.min.js"></script>
</head>
<body>
<h1>Acquire Images From Scanners and Cameras</h1>
<select id="source"></select><br />
<input type="button" id="btn-switch" value="Hide Video" onclick="SwitchViews();" />
<input type="button" id="btn-grab" value="Acquire From a Scanner" onclick="CaptureImage();" /> Or
<input type="button" value="Load Images or PDFs" onclick="LoadImages();" />
<br />
<br />
<div id="dwtcontrolContainer"></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 webcam SDK 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
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 webCamStartingIndex; //This is used to separate scanners and webcams
var DWObject;
var isVideoOn = true;
function Dynamsoft_OnReady() {
DWObject = Dynamsoft.WebTwainEnv.GetWebTwain(
'dwtcontrolContainer'
); // Get the Dynamic Web TWAIN object that is embeded in the div with id '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;
}
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 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);
}
}
}
function LoadImages() {
if (DWObject) {
if (DWObject.Addon && DWObject.Addon.PDF) {
DWObject.Addon.PDF.SetResolution(300);
DWObject.Addon.PDF.SetConvertMode(EnumDWT_ConvertMode.CM_RENDERALL);
}
DWObject.IfShowFileDialog = true;
DWObject.LoadImageEx("", 5,
function () {},
function (errorCode, errorString) {
alert('Load Image:' + errorString);
}
);
}
}
</script>
</body>
</html>