Perl Sample: Acquire and save to BMP file

Top  Previous  Next

The following Perl 5.x example script is all that is required to do the following:

 

Check if TWAIN is installed.

Initialize the DTWAIN Dynamic Link Library.

Select a Source

Acquire a page using the Native transfer mode

Save page to a BMP file called "test.bmp"

Shut down any open TWAIN Source's and DTWAIN itself

 


#!/usr/bin/perl

use strict;

use warnings;

$|++;

use Win32::API;

 

# DTWAIN Constants.  See DTWAIN32.PL file for these values

use constant DTWAIN_BMP => 100;

use constant DTWAIN_USELONGNAME => 16;

use constant DTWAIN_USENATIVE => 1;

use constant DTWAIN_PT_DEFAULT => 1000;

 

# DTWAIN function definitions.  See DTWAIN32.PL for these settings

my $DTWAIN_IsTwainAvailable = new Win32::API('DTWAIN32.DLL','DTWAIN_IsTwainAvailable','','N');

my $DTWAIN_SysInitialize = new Win32::API('DTWAIN32.DLL','DTWAIN_SysInitialize','','N');

my $DTWAIN_SelectSource = new Win32::API('DTWAIN32.DLL','DTWAIN_SelectSource','','N');

my $DTWAIN_SysDestroy = new Win32::API('DTWAIN32.DLL','DTWAIN_SysDestroy','','N');

my $DTWAIN_AcquireFile = new Win32::API('DTWAIN32.DLL','DTWAIN_AcquireFile','NPNNNNNNP','N');

 

# Start to use the DTWAIN functions

my $isAvail = $DTWAIN_IsTwainAvailable->Call();

if ( $isAvail == 1 )

{

    my $return = $DTWAIN_SysInitialize->Call();

    if ( $return != 0 )

    {

       my $TwainSource = $DTWAIN_SelectSource->Call();

       if ( $TwainSource != 0 )

       {

              $DTWAIN_AcquireFile->Call($TwainSource, 'test.bmp', DTWAIN_BMP, DTWAIN_USENATIVE + DTWAIN_USELONGNAME,

                                                                   DTWAIN_PT_DEFAULT, 1, 1, 1, 0);

       }

   }

   $DTWAIN_SysDestroy->Call();

}


Note that the Win32::API Perl package must be installed for this example to work, as  well as DTWAIN32.DLL available on an accessible directory (a directory specified on your system PATH).  Also note that this program doesn't test if the DTWAIN32.DLL file exists or if the functions within the DLL exists, so an appropriate Perl die or warn should be used in a production program to ensure that there are no DLL or functional issues.

 

The constants and function prototypes are included in the file DTWAIN32.PL.  Since Perl programmers have different preferences in terms of how to include files, define constants, etc., it is recommended that you copy and paste the constants and functions you need from DTWAIN32.PL into your Perl program and change the method that these constants and functions are included according to your requirements.