Input
This section introduces different ways to get data into the Dynamic Web TWAIN
buffer.
Scanners
Scan From a Local Scanner
Supported on desktop.
A local scanner refers to a scanner that is plugged in the same desktop via USB or is available on the local network and is accessible on the local desktop. The latter is generally known as a network scanner. A network scanner is connected to the LAN itself (cable or WI-FI) and is assigned a static IP. The scanner driver from the device vendor then configures the IP to connect to the scanner.
As far as
Dynamic Web TWAIN
is concerned, a network scanner is just like a local scanner because its driver has taken care of the network connection behind the scene.
Capture from cameras
Use DirectShow Cameras
To include the Webcam add-on, simply add a reference to the corresponding webcam JS file which is included in the resources folder.
<script src="Resources/addon/dynamsoft.webtwain.addon.webcam.js"></script>
The following code snippet shows how to use a camera through DirectShow
.
function PlayVideo(bShow) {
if (DWObject) {
DWObject.Addon.Webcam.StopVideo();
DWObject.Addon.Webcam.SelectSource("CAMERA-NAME");
DWObject.Addon.Webcam.PlayVideo(DWObject, 80, function() {});
}
}
function CaptureImage() {
if (DWObject) {
DWObject.Addon.Webcam.SelectSource("CAMERA-NAME");
var funCaptureImage = function() {
setTimeout(function() {
DWObject.Addon.Webcam.StopVideo();
}, 50);
};
DWObject.Addon.Webcam.CaptureImage(funCaptureImage, funCaptureImage);
}
}
Load files
Load in this context means to open files which are accessible on the file system. These files can reside on the local disk or shared on the network.
As a lightweight library,
Dynamic Web TWAIN
only supports the following file types: BMP, JPG, TIF, PNG, and PDF. However, not all files of these types can be loaded correctly. Learn more here. If you come across a file that is of one of the supported file types but fails to load, please contact Dynamsoft Support.
[Recommended] Show the Open File
dialog and select files to load.
The Open File
dialog is only displayed on desktop. On mobile browsers, users are presented with the option to take a picture, use an existing image on the device, or open an existing file stored on the device.
var onSuccess = function() {
console.log("Loaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.IfShowFileDialog = true;
// PDF Rasterizer Addon is used here to ensure PDF support
DWObject.Addon.PDF.SetResolution(200);
DWObject.Addon.PDF.SetConvertMode(Dynamsoft.DWT.EnumDWT_ConvertMode.CM_RENDERALL);
DWObject.LoadImageEx("", Dynamsoft.DWT.EnumDWT_ImageType.IT_ALL, onSuccess, onFailure);
Open an existing file using its absolute path
Supported on desktop only.
var onSuccess = function() {
console.log("Loaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.IfShowFileDialog = false;
// PDF Addon is used here to ensure PDF support
DWObject.Addon.PDF.SetResolution(200);
DWObject.Addon.PDF.SetConvertMode(Dynamsoft.DWT.EnumDWT_ConvertMode.CM_RENDERALL);
/* "YOUR-FILE-PATH" - example path
* Windows:
* Local: "D:\\Files\\sample.pdf"
* LAN: "\\\\192.168.1.45\\sme\\sample.tif"
* macOS: "/Applications/Images/sample.pdf"
* Linux: "/home/userA/Files/sample.pdf"
*/
DWObject.LoadImage("YOUR-FILE-PATH", onSuccess, onFailure);
Drag and drop files onto the viewer area of a WebTwain
instance
Only supported on desktop platform.
Download
Download
is another type ofLoad
. The only difference between aDownload
with what’s written above is that the file to load needs to be transferred to the local device via the network first.Dynamic Web TWAIN
takes care of the whole process and supports bothHTTP
andFTP
.
HTTP
or HTTPS
Supported on desktop and mobile platforms. For this option, a URL is used to fetch the file. The URL either specifies the path of the file itself or points to a server-side script that eventually returns a file. Check out the code snippets below demonstrating both scenarios.
The URL specifies a file
/* The sample file path is:
* "http://localhost:300/files/sample.tif"
*/
var onSuccess = function() {
console.log("Downloaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.HTTPPort = 300;
DWObject.HTTPDownload("localhost", "/files/sample.tif", onSuccess, onFailure);
The URL points to a server-side script
The method
HTTPDownloadEx
is used because the file type needs to be specified.
JavaScript
/* The sample file path is:
* "http://localhost:300/files/sample.tif"
*/
var onSuccess = function() {
console.log("Downloaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.HTTPPort = 300;
DWObject.HTTPDownloadEx("localhost", "/getFile.aspx", Dynamsoft.DWT.EnumDWT_ImageType.IT_TIF, onSuccess, onFailure);
Server-side script
The server-side script can be written in any language and in any logic as long as it returns a file.
<%@ Page Language="C#"%>
<%
try
{
String fileName = "sample.tif";
String filePath = Server.MapPath(".") + "\\files\\" + fileName;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
String fileNameEncode;
fileNameEncode = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
fileNameEncode = fileNameEncode.Replace("+", "%20");
String appendedheader = "attachment;filename=" + fileNameEncode;
Response.AppendHeader("Content-Disposition", appendedheader);
Response.WriteFile(fileInfo.FullName);
}
catch (Exception)
{ }
%>
FTP
Supported on desktop platform.
The following code shows how to download a file from FTP.
/* The sample file path is:
* "ftp://192.168.8.20/files/sample.pdf"
*/
var onSuccess = function() {
console.log("Downloaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.FTPPort = 21;
DWObject.FTPUserName = "FTPUser";
DWObject.FTPPassword = "SomePassword";
DWObject.FTPDownloadEx("192.168.8.20", "/files/sample.pdf", Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF, onSuccess, onFailure);
Load files in binary or base64 string format
Dynamic Web TWAIN
is capable of reading files in binary or base64 string format.
var onSuccess = function() {
console.log("Loaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
/**
* The following function loads a binary/blob.
* The method ConvertToBlob() is used to create
* the Blob as an example but the actual Blob can
* come from anywhere.
*/
function loadFileFromBinary() {
if (DWObject) {
DWObject.ConvertToBlob(
[0],
Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF,
function(result) {
DWObject.LoadImageFromBinary(
result, onSuccess, onFailure);
}, onFailure);
}
}
/**
* The following function loads a base64 string.
* The method ConvertToBase64() is used to create
* the string as an example but the string can
* come from anywhere.
* NOTE: the prefix such as "data:image/png;base64,"
* should NOT be included in the data to load.
*/
function loadFileFromBase64() {
if (DWObject) {
DWObject.ConvertToBase64(
[0],
Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF,
function(result) {
DWObject.LoadImageFromBase64Binary(
result.getData(0, result.getLength()),
Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF,
onSuccess, onFailure);
}, onFailure);
}
}
Load files from the system clipboard
Supported on desktop only.
DWObject.LoadDibFromClipboard()
Related KBs
What physical document scanners does the Dynamic Web TWAIN SDK support?
How to use TWACKER to check if your device is TWAIN Compliant?
How to test if your scanner supports ICA scanning on Mac OS?
How to test if your device is SANE compliant?
Can I set scanning settings without using the default scanner’s UI? What pre-scanning settings do you support?
Can I hide offline scanner devices from the select source list?
Can I hide webcam devices from the select source list?