Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanner Integration

Status
Not open for further replies.

Scott24x7

Programmer
Jul 12, 2001
2,795
10
38
JP
Hi All,
Has anyone done any scanner integration with VFP? What I'm trying to achieve is, click a button that allows an attached scanner to accept an image when scanned in, save the image (automatically) to a pre-defined directory, and attach it to a table at the same time. Would appreciate any guidance here.

Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
OK, I compiled the VB code from Lumir Mik to a simple VB6 executable that you can call from VFP or any other Windows program as long as you provide it with the correct parameters. If someone is interested in the scanit.exe I can make a download link.

The VFP Example for calling it, shows:
1. How to select Source. This only needs to be done once (in a lifetime). Even if you shutdown and restart the PC it seems to remember it.
2. Scan without interface with any DPI, b/w, gray or colour, with any given scan region to a given BMP
3. Scan with interface to a BMP. Settings are done in the scanner interface
4. Convert the BMP to JPG file with any given quality using irfanview.


Code:
oScript = CREATEOBJECT("wscript.shell")

* select scanner, action=1
cmd1='scanit.exe 1'
ret1=oScript.run(cmd1,3,.t.)

if ret1=1
   messagebox('Cancelled by user')
   return
endif

* scan without interface, action=2, bmp file
* further parameters: dpi, bw/grey/rgb, left, top, right, bottom in inches
cmd2='scanit.exe 2, F:\test2.bmp, 150, 2, 0, 0, 4, 6'
ret2=oScript.run(cmd2,3,.t.)

if ret2=1
   messagebox('An Error occurred.')
   return
endif

Continuing

Code:
* or scan with interface, action=3, bmp file
cmd3='scanit.exe 3, F:\test3.bmp'
ret3=oScript.run(cmd3,3,.t.)

if ret3=1
   messagebox('An Error occurred.')
   return
endif

* convert to jpg, download irfanview, only need i_view32.exe for this
cmd4='i_view32.exe f:\test2.bmp /jpgq=75 /convert=f:\test2.jpg'
cmd5='i_view32.exe f:\test3.bmp /jpgq=75 /convert=f:\test3.jpg'
ret4=oScript.run(cmd4,2,.t.)
ret5=oScript.run(cmd5,2,.t.)

if ret4>0
   messagebox('An Error occurred while converting to JPG.')
   return
endif

if ret5>0
   messagebox('An Error occurred while converting to JPG.')
   return
endif

 
I do it for one of my clients using Windows Image Acquisition (WIA) and it is fantastic. Accesses direct connected or networked scanners without any extra work. You'll probably want to muck around with the functions to get a little control over file size, etc but essentially the code is as follows :

Code:
#define k_WIA_DTSCANNER	1

local oCD, oFile

oCD= createobject("WIA.CommonDialog")
oFile= oCD.ShowAcquireImage(k_WIA_DTSCANNER)
oFile.saveFile("your output file.jpg")

I use it for image format and size adjusting as well via WIA.ImageProcess and you can just get the images directly from an attached camera as well.

Rob Spencer
Caliptor Pty Ltd
 
That's kind of cool, but I have duplex scanner, and it only saved 1 file (front) but not back of card.
It also invoked the WIA interface, which is I guess your intention, but that interface for this scanner is terrible... only allows you to pic the predefined image sizes, surprisingly of which the have crazy coverage for Japanese envelope sized, but for a printer that touts it's capability with duplex business cards, offers 0 options for anything anywhere near that size. (Very disappointing).


I could see this working in some cases, but seems too fiddly for our need. We don't want to do any image processing (ie. cropping) after the scan is done, or image type conversion (though that's not an issue here, just saying...)


Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Scott, I think you are better of by scanning without the default scanners interface.
Business cards have a predefined dimension I think.

Did some testing with WIA automation without an interface (You can built your own).
It's a bit difficult and the information how to is scattered over the internet, but I gathered and combined it into an example.

Code:
ocd=createobject("WIA.CommonDialog")
osc=createobject("WIA.DeviceManager")

* select scanner. parameters: type =0/1/2/3, show it, generate error
Scanner=ocd.ShowSelectDevice(0,.t.,.f.)
* or take the first device
* Scanner=osc.DeviceInfos(1).connect

if isnull(Scanner)
    messagebox('No scanner selected')
    return
endif

With Scanner.Items(1)
    .Properties("6146").Value = 1 &&colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
    .Properties("6147").Value = 150 && dpi 'dots per inch/horizontal
    .Properties("6148").Value = 150 && dpi 'dots per inch/vertical
    .Properties("6149").Value = 0 && x point where to start scan
    .Properties("6150").Value = 0 && y-point where to start scan
    .Properties("6151").Value = 600 && horizextent 'horizontal exent DPI x inches wide
    .Properties("6152").Value = 300 && vertextent 'vertical extent DPI x inches tall
EndWith

filebmp="{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
 
*scans the image without progress bar.
*Img = Scanner.Items(1).Transfer(filebmp) 

* Or scans with progress bar:
Img=ocd.ShowTransfer(Scanner.items(1),filebmp)

if isnull(Img)
    messagebox('No scan made')
    return
endif

* save to a named file
save2='f:\mytest.bmp'
erase (save2)
Img.SaveFile(save2)
Don't know about duplex (haven't such a scanner) but maybe it's like Img2=Scanner.Items(2).Transfer(filebmp)
 
You can try viscomsoft scanner pro for free with nagging reminder. Once satisfied, you could buy for some 60$ or so.

I am not related to this company in any way, except I use their product.

Nasib
 
Jack,
This looks quite cool. I just landed from a long flight, I'll give it a try tomorrow, and let you know how it goes. If this works the way I hope it does, it's my solution!


Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
It seems it blew up when trying to scan, setting up the With Scanner items. The error I get is:

OLE IDispatch exception code 0 from ?: The parameter is incorrect.

Any idea what this is about?

Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Scott, try with different values. Not every device supports all properties although these 7 in the example seem very basic. At least for a flatbed scanner. It worked well with my simple Canon scanner.

First determine which property gave the error: comment (*) 6 out of 7 out until you get the error.
Maybe your scanner doesn't support 150 DPI. Or the vertical DPI cannot be set or something. Than leave that out.
Or it doesn't support color, than use code 2 for gray etc.
When you use a Webcam for example none of these properties can be set. Are you sure you selected a scanner?

Here is a general properties list
 
Thanks Jack,
Actually, I now have it working perfectly for the way that I wanted (which this scanner is duplex and can scan both sides of a document simultaneously). It's not a flatbed. But I'm using EXTWAIN4 and this is just working 100% as I had hoped for.


Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top