[jawsscripts] OCRSnapObject Failing!

  • From: "Donald Marang" <donald.marang@xxxxxxxxx>
  • To: <jawsscripts@xxxxxxxxxxxxx>
  • Date: Sat, 30 Jan 2010 18:45:01 -0500

I think I have the basic structure and JAWS Script statements I thought should 
work.  Unfortunately it returns nothing!  How do you know if the CreateObject 
function returns the expected object?  I attempt to retrieve the number of 
images (pages), I would expect a value of one, but nothing is returned.  Could 
someone familiar with objects in JAWS look to see if I made a dumb mistake?  
I included a copy of a Visual Basic routine to OCR an image file and writes the 
plain text to a file.  I simplified this as I converted it to JAWS script code. 
 I would think a screenshot image would only be one page.

Following the example is my script.  Some of the necessary lines are commented 
out until I ensure the active code is working.  I also attempted to use my 
constant, MODI_DLL_PATH, directly in the CreateObject statements.  Both with 
single '\' and double '\\' characters.  Doing so caused all three statements to 
fail.  

--- Start of example
Example from Wikipedia and MSDN for "MODI OCR" in Visual Basic .NET 
</wiki/Visual_Basic_.NET> follows:
You can program the MODI object model from any development tool that supports 
the Component Object Model (COM) by setting a reference to the Microsoft Office 
Document Imaging 11.0 (Office 2003) (or 12.0, which is Office 2007,  I saw 
somewhere else) Type Library. 

Dim inputFile As String = "C:\test\multipage.tif"
Dim strRecText As String = ""
Dim Doc1 As MODI.Document
 
Doc1 = New MODI.Document
Doc1.Create(inputFile)
Doc1.OCR()  ' this will ocr all pages of a multi-page tiff file
Doc1.Save() ' this will save the deskewed reoriented images, and the OCR text, 
back to the inputFile
 
For imageCounter As Integer = 0 To (Doc1.Images.Count - 1) ' work your way 
through each page of results
    strRecText &= Doc1.Images(imageCounter).Layout.Text    ' this puts the ocr 
results into a string
Next
 
File.AppendAllText("C:\test\testmodi.txt", strRecText)     ' write the OCR file 
out to disk
 
Doc1.Close() ' clean up
Doc1 = Nothing
--- End of example
Include "hjconst.jsh"

Const
 CRLF = "\13\10", ; Cariage return followed by line feed character
 SCREEN_CAPTURE_FILE = "ScreenCapture.tif", ; Temp file placed in the JAWS user 
settings directory
 NIRCMD = "nircmd.exe", ; Command Line Utility from:  
http://www.nirsoft.net/utils/nircmd.html
 IMAGE_OCR = "MSPVIEW.EXE -o ",
 IMAGE_READ = "MSPVIEW.EXE -r ", 
 ; Microsoft Office Document Imaging Viewer Control 11.0 or 12.0 (MDIVWCTL.DLL) 
 MODI_DLL_PATH = "\"C:\\Program Files\\Common Files\\Microsoft 
Shared\\MOD\\MDIVWCTL.DLL\""

Script OCRSnapshotObject ()
Var
 String sImageFile,
 String sCmdUtility, 
 String sExecute, 
 Object oNull,
 Object oDoc, 
 Object oAllImages, 
 Object oImage, 
 Object oLayout, 
 Int iCount, 
 Int iChars, 
 Int iWords, 
 String sText

; Close virtual buffer if open
If UserBufferIsActive () Then
 UserBufferDeactivate ()
EndIf
; Set temporary image filename
Let sImageFile = GetJAWSSettingsDirectory () + "\\" + SCREEN_CAPTURE_FILE
Let sImageFile = "\"" + sImageFile  + "\"" ; Enclose in quotes in case there is 
a space in the path
; Set Command Line Utility path
Let sCmdUtility = GetJAWSSettingsDirectory () + "\\" + NIRCMD
Let sCmdUtility = "\"" + sCmdUtility + "\"" ; Enclose in quotes in case there 
is a space in the path
; Capture image of foreground active window
Let sExecute = sCmdUtility + " savescreenshotwin " + sImageFile 
; SayString ("Will perform save screenshot to file = \"" + sExecute + "\"")
Run (sExecute)
SayMessage (OT_MESSAGE, "Screen image has been captured.  Please wait for OCR 
conversion.")
Delay (10) ; make sure the file is not in use

; Run Microsoft Office Document Imageing Object Model to perform OCR
; Not sure how to create / open / register ovject as performed by Visual.Net 
Basic in the nnext two lines
; Dim Doc1 As MODI.Document
; Let oDoc = New MODI.Document

; Method used in Homer Jax
Let oDoc = CreateObjectEx ("MODI.Document", False)
If ! oDoc Then
 SayMessage (OT_ERROR, "Could not create object using first call!")
 Let oDoc = CreateObject ("MODI.Document")
EndIf
If ! oDoc Then
 SayMessage (OT_ERROR, "Could not create objectusing second call!")
 ; Running out of possibilities, try the last variety
 Let oDoc = CreateObjectEx ("MODI.Document", True)
EndIf
If ! oDoc Then
 SayMessage (OT_ERROR, "Could not create objectusing third call!")
    Return oNull
EndIf
SayString ("Document object created!")
oDoc.Create(sImageFile) ; loads the image into the object
; SayString ("Image file =" + sImageFile)
Let oAllImages = oDoc.Images
Let iCount = oAllImages.Count
SayString ("pages in file = " + IntToString (iCount))
; oDoc.OCR() ; this will ocr all pages of a multi-page tiff file
; oDoc.Save() ; this will save the deskewed reoriented images, and the OCR 
text, back to the inputFile
; Let oImage = oDoc.Images(0) ; first page of possible multi-page image
SayString ("OCR complete!")
; Let oLayout = oImage.Layout ; OCR information
; Let sText = oLayout.Text ; this puts the ocr results into a string
SayString ("text = \"" + sText + "\"")
; Let iChars = oLayout.NumChars
; Let iWords = oLayout.NumWords
SayMessage (OT_MESSAGE, "Screen image has been converted to text.")
SayMessage (OT_MESSAGE, "Converted " + IntToString (iChars) + " characters, and 
" + IntToString (iWords) + " words")

; View converted text
SayMessage (OT_USER_BUFFER  , sText + CRLF + "Press Escape to close this 
message.") 
oDoc.Close() ; Clean up
Let oLayout = oNull
Let oImage =oNull
Let oAllImages = oNull
Let oDoc =oNull
EndScript

Don Marang
__________ 
Visit and contribute to The JAWS Script Repository http://jawsscripts.com

View the list's information and change your settings at 
http://www.freelists.org/list/jawsscripts

Other related posts: