![]() |
![]() |
|
![]() |
TWAIN Support in ObjectPAL® TWAIN Support Overview Part 2 © 2001 Paul Cronk Checking for Twain Support To check for TWAIN support, the application would typically call isTwainAvailable() on the startup of the application, and store the result for later reference. #FormData1::var var loTwain Logical endvar #FormData1::init method init(var eventInfo Event) loTwain = isTwainAvailable() endMethodWhenever the application is about to display user-interface items regarding acquisition, the variable loTwain is referenced. If this value returns FALSE, then the user-interface items are disabled, hidden, or removed. This allows applications to take advantage of workstations that have TWAIN-compliant devices attached and still function with those workstations with no TWAIN-compliant devices. Each developer/application will have their own methods of handling application settings. This example is not to point out the best method, but to suggest that storing the return value of isTwainAvailable() for later reference is a good idea. Opening a Session As stated previously, communication between the source manager and the TWAIN variable type is handled by a session. Typically, this is known as 'opening a TWAIN session'. The session is the only means to talk to the source manager. To establish a session, the function open() is called. While not efficient, this does provide another means to verify that TWAIN support is available. If the return code from open() is false, then the TWAIN session could not be established. Lastly, developers can code an isAssigned() call to determine if the session was established. Normally, conditional code would be wrapped around the open() call. Like most session style objects, the close() function will terminate the session. Like the TCursor object, when the variable goes out of scope, the variable is unassigned, and the session is terminated. A simple example is described below. btnExample::pushbutton method pushbutton(var eventInfo Event) var twSes Twain endvar ;// open a session to the source manager, and report ;// an error if one is returned. if not twSes.open() then errorShow() return endif if twSes.isAssigned() then ;// acquire, select source, etc... endif ;// close the session. twSes.close() endMethod Default Sources The term default source refers to the system-wide TWAIN-compliant data source that is used by default for acquiring images. The only supported way of changing the system default data source is through the Select Source dialog. If the source for a TWAIN session is not explicitly set, the session inherits the system's default data source for the acquisition. The following example describes how to get the default source for the machine. This information is useful if you are coding your own Select Source dialog. When coding your own Select Source dialog, the application is responsible for saving, and retrieving the selected data source. The application is also responsible for ensuring the data source still exists on the system. btnExample::pushbutton method pushbutton(var eventInfo Event) var twSes Twain strSource String endvar if not twSes.open() then errorShow() return endif strSource = twSes.getDefaultSource() msgInfo ( "Default Source", strSource ) twSes.close() endMethod Showing the Select Source Dialog The Select Source dialog is used to select a data source to acquire from. The data source that is selected from the Select Source dialog becomes the default data source for the machine. btnExample::pushbutton method pushbutton(var eventInfo Event) var twSes Twain strOldSource String strNewSource String endvar if not twSes.open() then errorShow() return endif strOldSource = twSes.getDefaultSource() twSes.ShowSelectSourceDlg() strNewSource = twSes.getDefaultSource() if strOldSource = strNewSource then msgInfo ( "Twain", "The data source has not changed." ) else msgInfo ( "Twain", "The data source has changed." ) endif twSes.close() endMethod Enumerating Data Sources A TWAIN session can enumerate all the data sources on a machine. This is as simple as opening a TWAIN session and calling enumSourceNames(). This information returns the source names of the data sources located on the machine. The index of each data source is stored in the key of the returned dynArray entry. The index should only be used as a means to retrieve the data source name, and should not be stored for later use. btnExample::pushbutton method pushbutton(var eventInfo Event) var twSes Twain strOldSource String dynSourceNames DynArray[] String endvar if not twSes.open() then errorShow() return endif twSes.enumSourceNames( dynSourceNames ) dynSourceNames.view() twSes.close() endMethod Setting the source for a session To set a data source for the TWAIN session, the caller would use setSource(). If the source is not found in the source list, then the setSource() returns FALSE. The following example will select the first source name in the source name list. BtnExample::pushbutton Method pushbutton(var eventInfo Event) var twSes Twain strOldSource String dynSourceNames DynArray[] String endvar if not twSes.open() then errorShow() return endif twSes.enumSourceNames ( dynSourceNames ) ;// if there was at least one source name, then set it. if dynSourceNames.size() >= 1 then twSes.setSource ( dynSourceNames[ 1 ] ) else msgInfo ( "Twain", "There are no sources available." ) endif twSes.close() ;// ... endMethod Getting the source for the session The source for the current session can be set by calling setSource(). Likewise, to retrieve the selected source use getSource(). The difference between getSource(), and getDefaultSource(), is that in the latter we are retrieving the default source for the system, and getSource() retrieves the source for the session. In the event that the source has explicitly been set by the developer, calling getSource() without setting the session source will return the same result as getDefaultSource(). For this example, we'll assume we always want to acquire from the camera, regardless of what they have selected. BtnExample::pushbutton Method pushbutton(var eventInfo Event) var twSes Twain strSourceName String dynSourceNames DynArray[] String endvar if not twSes.open() then errorShow() return endif strSourceName = twSes.getSource() if strSourceName <> "QV QuickCam 2.33 32bit" then if not twSes.setSource ( "QV QuickCam 2.33 32bit" ) then msgInfo ( "Twain", "The camera isn't installed." ) return endif endif twSes.close() endMethod Getting the number of sources In Setting the source for a session, the example set the source based on results from the enumSourceNames() method of the Twain type. The code relies on the size() function of the DynArray type to return the number of sources available in the array. By using getSourceCount(), the number of TWAIN-compliant data sources is retrieved. Since at least one TWAIN-compliant device is required for TWAIN support to be available, this function should never return 0. Thus, we can safeguard our code by getting the source count after opening the TWAIN session. The example below clearly illustrates how we can produce more efficient code: BtnExample::pushbutton Method pushbutton(var eventInfo Event) var twSes Twain strOldSource String dynSourceNames DynArray[] String endvar if not twSes.open() then errorShow() return endif if twSes.getSourceCount() = 0 then errorShow() return endif twSes.enumSourceNames ( dynSourceNames ) twSes.setSource ( dynSourceNames[ 1 ] ) twSes.close() endMethod Next: TWAIN Support Overview Part 3 Previous: TWAIN Support Overview Discussion of this article |
![]() Feedback | Paradox Day | Who Uses Paradox | I Use Paradox | Downloads ![]() |
|
![]() The information provided on this Web site is not in any way sponsored or endorsed by Corel Corporation. Paradox is a registered trademark of Corel Corporation. ![]() |
|
![]() Modified: 15 May 2003 Terms of Use / Legal Disclaimer ![]() |
![]() Copyright © 2001- 2003 Paradox Community. All rights reserved. Company and product names are trademarks or registered trademarks of their respective companies. Authors hold the copyrights to their own works. Please contact the author of any article for details. ![]() |
![]() |
|