« MBS FileMaker Advent … | Home | MBS FileMaker Advent … »

MBS FileMaker Advent calendar - Door 20 - Scan and Recognize

candy cane Monkeybread Monkey as an elf candy cane
Day 20 - Scan and Recognize

In this day and age, there are still people who like to receive things in printed form, and Christmas is no exception. Some people still fill out their wish lists and send them by mail. But we also have ways to make this easier. Here I would like to make a distinction between Mac and Windows. Today I'll show you the Windows part. First, we want to digitalize the piece of paper. To do this, we first scan it with functions from the WIA component and then run the Windows text recognition over it.

So let's start with our script. First we check whether we are really on a Windows system. If this is the case, we initialize the WIA service for scanning with WIA.Initialize. Then we want to display a dialog with WIA.SelectDeviceDialog with which we can select the scanner with which we can scan the paper. If we always want to specify a fixed scanner to scan the document, we can also set this scanner with WIA.SetCurrentDevice. Now we want to scan our document and call WIA.Scan. Here we specify whether the document is to be scanned via a flatbed or feeder. In the next step, we then check whether the function was executed without errors and returns OK. If this is the case, we would now like to have the scanned document. To do this, we use the WIA.Image function and specify the number of images we want in the parameters. In our case, we have only scanned one image and therefore we want to work with the first one. Here again, the count starts from 0 and we write a 0. However, the function does not return an image, but a path where the image was saved. With Files.ReadFile we can read this file, specifying the path as a container value, and write it to the WishlistIn field.

# Scann and Read

If [ Get(SystemPlatform)= -2 ]
	Set Variable [ $r ; Value: MBS( "WIA.Initialize" ) ]
	Set Variable [ $Printer ; Value: MBS( "WIA.SelectDeviceDialog"; "Sanner") ]
	Set Variable [ $r ; Value: MBS("WIA.Scan"; "Flatbed") ]
	If [ $r="OK" ]
		Set Variable [ $paths ; Value: MBS( "WIA.Image"; 0) ]
		Set Variable [ $img ; Value: MBS("Files.ReadFile"; $paths) ]
		Set Field [ Giftee::WishlistIn ; $img ]
	Else
		Show Custom Dialog [ "Scan Result" ; $r ]
		Exit Script [ Text Result:    ]
	End If

Now, of course, we also want to know what is in the document. We can use Windows text recognition for this. This is built into Windows 10 and 11 by default and can be used with the WindowsOCR component using the plugin. With WindowsOCR.Available we can use the text recognition with the currently used operating system. With WindowsOCR.New we then create a new working environment and receive a reference again. Next, we call the WindowsOCR.Recognize function, which searches for text on an image. In the parameters we enter the reference of the working environment and the container with the image. If we need the text from an image that is located in a container instead of an image that is located as a file on a path, we use the WindowsOCR.RecognizeFile function. Now we have several possibilities to retrieve our result. On the one hand, we can use WindowsOCR.Result to return a JSON that contains information about the text found, such as the positions of the individual words. However, the simple text is sufficient for us. To do this, we call the WindowsOCR.Text function. We can then write the text in our Presents field.

If [ MBS("WindowsOCR.Available") ]
	Set Variable [ $ocr ; Value: MBS("WindowsOCR.New") ]
	Set Variable [ $r ; Value: MBS("WindowsOCR.Recognize"; $ocr; Giftee::WishlistIn) ]
	Set Variable [ $text ; Value: MBS("WindowsOCR.Text"; $ocr) ]
	Set Field [ Giftee::Presents ; $text ]
Else
	Show Custom Dialog [ "Windows OCR not available" ; "You need Windows 10 or 11" ]
End If

That brings us to the end of this door and I'll see you again tomorrow for another door.


Monkeybread Software Logo with Monkey with Santa hat
19 👈 20 of 24 👉 21
Claris FileMaker Plugin
20 12 24 - 12:28