Acquiring an Image (Visual FoxPro 6)

Take the following steps to start a project and to add some code that acquires an image from a TWAIN source:

Note: This tutorial uses the VFPCOM Utility, which can be found on the Microsoft web site at: http://msdn.microsoft.com/vfoxpro/downloads/updates/

1.

Start Visual FoxPro.

2.

On the Tools menu, select Options….

3.

In the Options window, click the Controls tab, display the ActiveX controls, mark the LEAD Raster View Control, click the Set As Default button, then click the OK button. This registers the Raster View control and makes it available for all projects.

4.

On the File menu, select New….

5.

Select the Project option, and click the New File icon.

6.

In the Create window, select the directory where you want to create a project. Specify your project name, then click the Create button.

7.

In the Project window, expand the Documents icon, select the Forms icon, click the New button, then click the New Form icon. (Do not use the wizard.)

8.

Click the View Classes icon, and select ActiveX Controls.

9.

Select the LEAD Raster View Control icon on the toolbar.

10.

Size and position the control as you want it to appear at run time, and change the name of the control to RasterView1.

11.

In the Project Manager window, expand the Code icon, select the Programs icon and click the New button.

12.

Insert the following code into the newly created PRG file:

public oVFPCOM, oRasterTwain, oTwainCap, TwainEventClass, TheForm

#define C_VFPCOMCLASS  'vfpcom.comutil'
oVFPCOM = CreateObject(C_VFPCOMCLASS)
oRasterTwain = CreateObject("LEADRasterTwain.LEADRasterTwain.145")
oTwainCap = CreateObject("LEADTwainCapability.LEADTwainCapability.145")
TwainEventClass = CreateObject("_LEADRasterTwainEvents")
nError = oVFPCOM.BindEvents(oRasterTwain, TwainEventClass)

Do Form form1.scx

&& Following class definition was created using the VFPCOM utility
DEFINE CLASS _LEADRasterTwainEvents AS custom
   PROCEDURE AcquirePageEvent(pBitmap)
      MessageBox("Acquisition of Image Done")
      TheForm.RasterView1.Raster.InsertBitmapListItem(-1, pBitmap)
   ENDPROC
ENDDEFINE

13.

Add the following code to the form's Init procedure:

oRasterTwain.InitSession(ThisForm.RasterView1.Window)
TheForm = ThisForm

14.

Add the following code to the form's Unload procedure:

oRasterTwain.EndSession()

15.

Add seven Command Buttons to your form and name them as follows:

 

Name

Caption

 

Acquire

Acquire

 

SelectSource

SelectSource

 

Native

Native

 

MemBuf

Memory Buffered

 

File

File

 

SaveTemplate

Save Template File

 

LoadTemplate

Load Template File

16.

Code the Acquire button Click procedure as follows:

#define L_LTWAIN_SHOW_USER_INTERFACE 1
oRasterTwain.Acquire(L_LTWAIN_SHOW_USER_INTERFACE)

17.

Code the SelectSource button Click procedure as follows:

oRasterTwain.SelectSource()

18.

Code the SaveTemplate button Click procedure as follows:

oRasterTwain.SaveTemplate("c:\test.ltt")

19.

Code LoadTemplate button Click procedure as follows:

oRasterTwain.LoadTemplate("c:\test.ltt")

20.

Code the Native button Click procedure as Follows:

#define L_TWSX_NATIVE 0
ThisForm.SetCapabilityXferMech(L_TWSX_NATIVE)

21.

Code the MemBuf button Click Procedure as follows:

#define L_TWSX_MEMORY 2
ThisForm.SetCapabilityXferMech(L_TWSX_MEMORY)

22.

Code the File button Click Procedure as follows:

#define L_TWSX_FILE 1
ThisForm.SetCapabilityXferMech(L_TWSX_FILE)
oRasterTwain.FileTransferName = "c:\twain.bmp"

23.

On the Form menu, choose New Method. Name the new method SetCapabilityXferMech and code its procedure as follows:

Parameter XferType
#define L_ICAP_XFERMECH 259
#define L_TWTY_UINT16 4
#define L_TWON_ONEVALUE 5
#define L_LTWAIN_CAPABILITY_SET 1
#define VALUE_USHORT 4 
CapVal = CreateObject("LEADRasterVariant.LEADRasterVariant.145")
CapVal.Type = VALUE_USHORT
CapVal.LongValue = XferType

oTwainCap.EnableMethodErrors = .F.
oTwainCap.CapInfo.Capability = L_ICAP_XFERMECH
oTwainCap.CapInfo.ConType = L_TWON_ONEVALUE
oTwainCap.CapOneValue.OneValItemType = L_TWTY_UINT16
oTwainCap.CapOneValue.OneValCapValue = CapVal

oRasterTwain.SetCapability2(oTwainCap, L_LTWAIN_CAPABILITY_SET)

24.

Run your PRG program to test it.