Thanks for Downloading Dynamic Web TWAIN 30-Day Trial!
Your download will start shortly. If your download does not begin, click here to retry.
Specifying Scan Settings
Some applications may require images to be scanned with particular scan settings, e.g. a prescribed image size, resolution, color, etc. It may be undesirable to alter these settings, so we may wish to issue these settings to the scanner directly, without alteration by end users. We will demonstrate this method by passing scan settings through the DWT API in the HelloWorld application.
Prerequisites: Hello World - Uploading Images to the Server
Specify Scan Parameters with JSON
The AcquireImagesAsync()
API accepts scan setting arguments in a JSON format (specified by DeviceConfiguration). For this example, we will demonstrate specifying a grayscale image with a resolution of 150 ppi by passing a few of these defined properties:
IfShowUI: false
- this disables the scanner UI. Scan settings issued through the UI would override scan settings passed by JSON, so this prevents end user misuse.PixelType: Dynamsoft.DWT.EnumDWT_PixelType.TWPT_GRAY
- this sets the image color mode to grayscale.Resolution:150
- this sets the image resolution to 150 ppi.
Add all three properties in the JSON argument passed to AcquireImageAsync()
:
function AcquireImage() {
if (DWTObject) {
DWTObject.SelectSourceAsync()
.then(function () {
return DWTObject.AcquireImageAsync({
IfCloseSourceAfterAcquire: true,
IfShowUI: false,
PixelType: Dynamsoft.DWT.EnumDWT_PixelType.TWPT_GRAY,
Resolution: 150,
});
})
.catch(function (exp) {
alert(exp.message);
});
}
}
Review the Code
<html>
<head>
<script src="Resources/dynamsoft.webtwain.initiate.js"></script>
<script src="Resources/dynamsoft.webtwain.config.js"></script>
</head>
<body>
<input type="button" value="Scan" onclick="AcquireImage();" />
<input type="button" value="upload" onclick="upload();" />
<div id="dwtcontrolContainer"></div>
<script type="text/javascript">
var DWTObject;
Dynamsoft.DWT.RegisterEvent("OnWebTwainReady", function () {
DWTObject = Dynamsoft.DWT.GetWebTwain("dwtcontrolContainer");
});
function AcquireImage() {
if (DWTObject) {
DWTObject.SelectSourceAsync()
.then(function () {
return DWTObject.AcquireImageAsync({
IfCloseSourceAfterAcquire: true,
IfShowUI: false,
PixelType: Dynamsoft.DWT.EnumDWT_PixelType.TWPT_GRAY,
Resolution: 150,
});
})
.catch(function (exp) {
alert(exp.message);
});
}
}
function upload() {
if (DWTObject && DWTObject.HowManyImagesInBuffer > 0) {
var strUrl = "https://demo.dynamsoft.com/sample-uploads/";
var imgAry = [DWTObject.CurrentImageIndexInBuffer];
DWTObject.HTTPUpload(
strUrl,
imgAry,
Dynamsoft.DWT.EnumDWT_ImageType.IT_PNG,
Dynamsoft.DWT.EnumDWT_UploadDataFormat.Binary,
"WebTWAINImage.png",
onUploadSuccess,
onUploadFailure,
);
} else {
alert("There is no image in buffer.");
}
}
function onUploadSuccess() {
alert("Upload successful");
}
function onUploadFailure(errorCode, errorString, sHttpResponse) {
alert(sHttpResponse.length > 0 ? sHttpResponse : errorString);
}
</script>
</body>
</html>
Links to API Reference:
SelectSourceAsync()
AcquireImageAsync()
IfCloseSourceAfterAcquire
IfShowUI
PixelType
Dynamsoft.DWT.EnumDWT_PixelType
Resolution
Run the Application
Open the Application in Your Browser
Press the Scan button
The scan should proceed directly after the button press, without displaying the scanner UI.
View the scan
You should receive a grayscale image at 150 ppi:
Previous Articles
If you have yet to acquire the image from the scaner, please review scanning an image.
If this scan is all that you need, you can review uploading images to the server.
Next Article
The next and final step in our guide is editing images after acquisition.