Advertisement
Guest User

PureBaisc: Windows 10 OCR and Face detection

a guest
Feb 11th, 2022
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.15 KB | None | 0 0
  1. EnableExplicit
  2.  
  3. DeclareModule WinOCR
  4.  
  5. Structure OCR_WordRects
  6. sWord.s
  7. x.l
  8. y.l
  9. w.l
  10. h.l
  11. EndStructure
  12.  
  13. Declare.s get_Languages()
  14.  
  15. Declare.s get_TextFromFile(sFile.s, sLanguage.s = "")
  16. Declare.s get_TextFromImageID(ImageID, sLanguage.s = "")
  17. Declare.s get_TextFromHWND(ImageID, sLanguage.s = "")
  18.  
  19. Declare.l get_WordListFromFile(sFile.s, List rcWords.OCR_WordRects(), sLanguage.s = "")
  20. Declare.l get_WordListFromImageID(ImageID, List rcWords.OCR_WordRects(), sLanguage.s = "")
  21. Declare.l get_WordListFromHWND(ImageID, List rcWords.OCR_WordRects(), sLanguage.s = "")
  22.  
  23. ;
  24. Declare get_FacesFromFile(sFile.s, List rcList.rect())
  25. Declare get_FacesFromImageID(ImageID, List rcList.rect())
  26. Declare get_FacesFromHWND(hWNd, List rcList.rect())
  27. EndDeclareModule
  28.  
  29. Module WinOCR
  30.  
  31.  
  32. ;Credits - Malcev @autohotkey.com
  33.  
  34. ;https://www.autohotkey.com/boards/viewtopic.php?t=72674
  35. ;https://www.autohotkey.com/boards/viewtopic.php?t=72797
  36.  
  37. EnableExplicit
  38.  
  39.  
  40. #MyRoDebug = 0
  41.  
  42. ;- Interfaces
  43.  
  44. Interface IInspectable Extends IUnknown
  45. GetIids(*iidCount, *iids)
  46. GetRuntimeClassName(*className)
  47. GetTrustLevel(*trustLevel)
  48. EndInterface
  49.  
  50. Interface IClosable Extends IInspectable
  51. Close()
  52. EndInterface
  53.  
  54. Interface ILanguageFactory Extends IInspectable
  55. createLanguage(*string, *out)
  56. EndInterface
  57.  
  58. Interface ILanguage Extends IInspectable
  59. get_LanguageTag(*value)
  60. get_DisplayName(*value)
  61. get_NativeName(*value)
  62. get_Script(*value)
  63. EndInterface
  64.  
  65. Interface IBitmapDecoderStatics Extends IInspectable
  66. BmpDecoderId(*value.guid)
  67. JpegDecoderId(*value.guid)
  68. PngDecoderId(*value.guid)
  69. TiffDecoderId(*value.guid)
  70. GifDecoderId(*value.guid)
  71. JpegXRDecoderId(*value.guid)
  72. IcoDecoderId(*value.guid)
  73. GetDecoderInformationEnumerator(*out)
  74. CreateAsync(*in, *out)
  75. CreateWithIdAsync(*decoderId.guid, *in, *out)
  76. EndInterface
  77.  
  78. Interface IBitmapDecoder Extends IInspectable
  79. BitmapContainerProperties(*value)
  80. DecoderInformation(*value)
  81. FrameCount(*value)
  82. GetPreviewAsync(*value)
  83. GetFrameAsync(frameIndex, *value)
  84. EndInterface
  85.  
  86. Interface IBitmapFrameWithSoftwareBitmap Extends IInspectable
  87. GetSoftwareBitmapAsync(*value)
  88. GetSoftwareBitmapConvertedAsync(pixelFormat, alphaMode, *value)
  89. GetSoftwareBitmapTransformedAsync(pixelFormat, alphaMode, transform, exifOrientationMode, colorManagementMode, *value)
  90. EndInterface
  91.  
  92. Interface IBitmapFrame Extends IInspectable
  93. GetThumbnailAsync(*asyncInfo)
  94. BitmapProperties(*value)
  95. BitmapPixelFormat(*value)
  96. BitmapAlphaMode(*value)
  97. DpiX(*value)
  98. DpiY(*value)
  99. PixelWidth(*value)
  100. PixelHeight(*value)
  101. OrientedPixelWidth(*value)
  102. OrientedPixelHeight(*value)
  103. GetPixelDataAsync(*asyncInfo)
  104. GetPixelDataTransformedAsync(pixelFormat, alphaMode, transform, exifOrientationMode, colorManagementMode, *asyncInfo)
  105. EndInterface
  106.  
  107. Interface ISoftwareBitmap Extends IInspectable
  108. get_BitmapPixelFormat(*value)
  109. get_BitmapAlphaMode(*value)
  110. get_PixelWidth(*value)
  111. get_PixelHeight(*value)
  112. get_IsReadOnly(*value)
  113. put_DpiX(*value)
  114. get_DpiX(*value)
  115. put_DpiY(*value)
  116. get_DpiY(*value)
  117. LockBuffer(mode, *value)
  118. CopyTo(*bitmap)
  119. CopyFromBuffer(*buffer)
  120. GetReadOnlyView(*value)
  121. EndInterface
  122.  
  123. Interface IOcrEngineStatics Extends IInspectable
  124. MaxDimensions(*value)
  125. AvailableRecognizerLanguages(*value)
  126. IsLanguageSupported(*Language,*Result)
  127. TryCreateFromLanguage(*Language,*Result)
  128. TryCreateFromUserProfileLanguages(*Result)
  129. EndInterface
  130.  
  131. Interface IOcrEngine Extends IInspectable
  132. RecognizeAsync(*bitmap, *result)
  133. RecognizerLanguage(*value)
  134. EndInterface
  135.  
  136. Interface IOcrResult Extends IInspectable
  137. Lines(*value)
  138. TextAngle(*value)
  139. Text(*value)
  140. EndInterface
  141.  
  142. Interface IRandomAccessStream Extends IInspectable
  143. get_Size(*value)
  144. put_Size(*value)
  145. GetInputStreamAt(position.q, *stream)
  146. GetOutputStreamAt(position.q, *stream)
  147. get_Position(*value)
  148. Seek(position.q)
  149. CloneStream(*stream)
  150. get_CanRead(*value)
  151. get_CanWrite(*value)
  152. EndInterface
  153.  
  154. Interface IAsyncInfo Extends IInspectable
  155. get_Id(*id)
  156. get_Status(*status)
  157. get_ErrorCode(*errorCode)
  158. Cancel()
  159. Close()
  160. EndInterface
  161.  
  162. Interface IAsyncOperationWithProgress Extends IInspectable
  163. Completed(asyncInfo, asyncStatus)
  164. Progress(asyncInfo, progressInfo)
  165. GetResults(*Object)
  166. EndInterface
  167.  
  168.  
  169. Interface IGlobalizationPreferencesStatics Extends IInspectable
  170. get_Calendars(*value)
  171. get_Clocks(*value)
  172. get_Currencies(*value)
  173. get_Languages(*value)
  174. get_HomeGeographicRegion(*value)
  175. get_WeekStartsOn(*value)
  176. EndInterface
  177.  
  178. Interface IReadOnlyList Extends IInspectable
  179. get_Item(index, *hString)
  180. count(*value)
  181. EndInterface
  182.  
  183.  
  184. Interface IFaceDetectorStatics Extends IInspectable
  185. CreateAsync(*returnValue)
  186. GetSupportedBitmapPixelFormats(*result)
  187. IsBitmapPixelFormatSupported(bitmapPixelFormat, *result)
  188. IsSupported(*returnValue)
  189. EndInterface
  190.  
  191. Interface IFaceDetector Extends IInspectable
  192. DetectFacesAsync(image, *returnValue)
  193. DetectFacesWithSearchAreaAsync(image, searchArea, *returnValue)
  194. MinDetectableFaceSize(*value)
  195. MaxDetectableFaceSize(*value)
  196. EndInterface
  197.  
  198. Interface IBitmapEncoderStatics Extends IInspectable
  199. get_BmpEncoderId(*value)
  200. get_JpegEncoderId(*value)
  201. get_PngEncoderId(*value)
  202. get_TiffEncoderId(*value)
  203. get_GifEncoderId(*value)
  204. get_JpegXREncoderId(*value)
  205. GetEncoderInformationEnumerator(*encoderInformationEnumerator)
  206. CreateAsync(encoderId, *stream, *asyncInfo)
  207. CreateWithEncodingOptionsAsync(encoderId, stream, *encodingOptions, *asyncInfo)
  208. CreateForTranscodingAsync(stream, bitmapDecoder, *asyncInfo)
  209. CreateForInPlacePropertyEncodingAsync(stream, bitmapDecoder, *asyncInfo)
  210. EndInterface
  211.  
  212. Interface ISoftwareBitmapStatics Extends IInspectable
  213. Copy(*source, *value)
  214. Convert(*source, format, *value)
  215. ConvertWithAlpha(*source, format, alpha, *value)
  216. CreateCopyFromBuffer(*source, format, width, height, *value)
  217. CreateCopyWithAlphaFromBuffer(*source, format, width, height, alpha, *value)
  218. CreateCopyFromSurfaceAsync(surface, *value)
  219. CreateCopyWithAlphaFromSurfaceAsync(surface, alpha, *value)
  220. EndInterface
  221.  
  222. Interface IBitmapEncoder Extends IInspectable
  223. EncoderInformation(*value)
  224. BitmapProperties(*value)
  225. BitmapContainerProperties(*value)
  226. IsThumbnailGenerated(*value)
  227. GeneratedThumbnailWidth(*value)
  228. GeneratedThumbnailHeight(*value)
  229. BitmapTransform(*value)
  230. SetPixelData(pixelFormat, alphaMode, width, height, dpiX.d, dpiY.d, __pixelsSize, *pixels)
  231. GoToNextFrameAsync(*asyncInfo)
  232. GoToNextFrameWithEncodingOptionsAsync(encodingOptions, *asyncInfo)
  233. FlushAsync(*asyncInfo)
  234. EndInterface
  235.  
  236. Interface IBitmapTransform Extends IInspectable
  237. ScaledWidth(*value)
  238. ScaledHeight(*value)
  239. InterpolationMode(*value)
  240. Flip(*value)
  241. Rotation(*value)
  242. Bounds(*value)
  243. EndInterface
  244.  
  245. Interface IDetectedFace Extends IInspectable
  246. FaceBox(*returnValue)
  247. EndInterface
  248.  
  249. Interface IVectorViewOcrLine Extends IInspectable
  250. GetAt(index, result)
  251. get_Size(result)
  252. IndexOf(value, index, result)
  253. GetMany(startIndex, itemsLength, items, result)
  254. EndInterface
  255.  
  256. Interface IOCRLine Extends IInspectable
  257. GetWords(result)
  258. GetText(result)
  259. EndInterface
  260.  
  261. Interface IOcrWord Extends IInspectable
  262. ;get_BoundingRect(index, result)
  263. get_BoundingRect(result)
  264. get_Text(result)
  265. EndInterface
  266.  
  267. ;-
  268. ;- DataSection
  269.  
  270. DataSection
  271. IID_IBitmapFrame:
  272. Data.l $72A49A1C
  273. Data.w $8081, $438D
  274. Data.b $91, $BC, $94, $EC, $FC, $81, $85, $C6
  275.  
  276. IID_IRandomAccessStream:
  277. Data.l $905A0FE1
  278. Data.w $BC53, $11DF
  279. Data.b $8C, $49, $0, $1E, $4F, $C6, $86, $DA
  280.  
  281. IID_IClosable:
  282. Data.l $30D5A829
  283. Data.w $7FA4, $4026
  284. Data.b $83, $BB, $D7, $5B, $AE, $4E, $A9, $9E
  285.  
  286. IID_IBitmapFrameWithSoftwareBitmap:
  287. Data.l $FE287C9A
  288. Data.w $420C, $4963
  289. Data.b $87, $AD, $69, $14, $36, $E0, $83, $83
  290.  
  291. IID_IAsyncInfo:
  292. Data.l $00000036
  293. Data.w $00, $00
  294. Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  295.  
  296. IID_IPicture:
  297. Data.l $7BF80980
  298. Data.w $BF32, $101A
  299. Data.b $8B, $BB, $00, $AA, $0, $30, $0C, $AB
  300.  
  301. IID_ILanguageFactory:
  302. Data.l $9B0252AC
  303. Data.w $C27, $44F8
  304. Data.b $B7, $92, $97, $93, $FB, $66, $C6, $3E
  305.  
  306. IID_IBitmapDecoderStatics:
  307. Data.l $438CCB26
  308. Data.w $BCEF, $4E95
  309. Data.b $BA, $D6, $23, $A8, $22, $E5, $8D, $01
  310.  
  311. IID_IOcrEngineStatics:
  312. Data.l $5BFFA85A
  313. Data.w $3384, $3540
  314. Data.b $99, $40, $69, $91, $20, $D4, $28, $A8
  315.  
  316. IID_IGlobalizationPreferencesStatics:
  317. Data.l $1BF4326
  318. Data.w $ED37, $4E96
  319. Data.b $B0, $E9, $C1, $34, $0D, $1E, $A1, $58
  320.  
  321. IID_IVectorViewOcrLine:
  322. Data.l $60C76EAC
  323. Data.w $8875, $5DDB
  324. Data.a $A1, $9B, $65, $A3, $93, $62, $79, $EA
  325.  
  326. IID_IOcrWord:
  327. Data.l $3C2A477A
  328. Data.w $5CD9, $3525
  329. Data.a $BA, $2A, $23, $D1, $E0, $A6, $8A, $1D
  330.  
  331. IID_IOcrLine:
  332. Data.l $0043A16F
  333. Data.w $E31F, $3A24
  334. Data.a $89, $9C, $D4, $44, $BD, $08, $81, $24
  335.  
  336. EndDataSection
  337.  
  338.  
  339. Prototype pRoInitialize(initType)
  340. Prototype pWindowsCreateString(a, b, c)
  341. Prototype pRoGetActivationFactory(a, b, c)
  342. Prototype pWindowsDeleteString(a)
  343. Prototype pWindowsGetStringRawBuffer(a, length)
  344. Prototype pCreateRandomAccessStreamOnFile(file, iRead, *GUID, *out)
  345. Prototype pCreateRandomAccessStreamOverStream(*stream, options, *riid, *ppv)
  346.  
  347. Structure DynamicRuntimeFuncs
  348. WindowsCreateString.pWindowsCreateString
  349. WindowsDeleteString.pWindowsDeleteString
  350. RoGetActivationFactory.pRoGetActivationFactory
  351. WindowsGetStringRawBuffer.pWindowsGetStringRawBuffer
  352. CreateRandomAccessStreamOnFile.pCreateRandomAccessStreamOnFile
  353. CreateRandomAccessStreamOverStream.pCreateRandomAccessStreamOverStream
  354. RoInitialize.pRoInitialize
  355. EndStructure
  356.  
  357. Global RT_Funcs.DynamicRuntimeFuncs
  358. Global RT_INIT_DONE
  359.  
  360. Structure MyRTIntArray
  361. i.i[0]
  362. EndStructure
  363.  
  364. Structure fRECT
  365. left.f
  366. top.f
  367. width.f
  368. height.f
  369. EndStructure
  370.  
  371. ;-
  372. ;- HelperFuncs
  373. ;-
  374.  
  375. Procedure RT_Init()
  376.  
  377. Protected hr.l
  378.  
  379. #RO_INIT_SINGLETHREADED = 0
  380. #RO_INIT_MULTITHREADED = 1
  381.  
  382. Protected hCombase = OpenLibrary(#PB_Any, "combase.dll")
  383. If Not hCombase
  384. ProcedureReturn 0
  385. EndIf
  386.  
  387. RT_Funcs\WindowsCreateString = GetFunction(hCombase, "WindowsCreateString")
  388. RT_Funcs\WindowsDeleteString = GetFunction(hCombase, "WindowsDeleteString")
  389. RT_Funcs\RoGetActivationFactory = GetFunction(hCombase, "RoGetActivationFactory")
  390. RT_Funcs\WindowsGetStringRawBuffer = GetFunction(hCombase, "WindowsGetStringRawBuffer")
  391. RT_Funcs\RoInitialize = GetFunction(hCombase, "RoInitialize")
  392.  
  393. hCombase = OpenLibrary(#PB_Any, "SHCore.dll")
  394. If Not hCombase
  395. ProcedureReturn 0
  396. EndIf
  397.  
  398. RT_Funcs\CreateRandomAccessStreamOnFile = GetFunction(hCombase, "CreateRandomAccessStreamOnFile")
  399. RT_Funcs\CreateRandomAccessStreamOverStream = GetFunction(hCombase, "CreateRandomAccessStreamOverStream")
  400.  
  401.  
  402. RT_INIT_DONE = 1
  403.  
  404. Protected *i.MyRTIntArray = @RT_Funcs, i
  405. For i = 0 To (SizeOf(RT_Funcs) / SizeOf(Integer)) - 1
  406. If Not *i\i[i]
  407. RT_INIT_DONE = 0
  408. Break
  409. EndIf
  410. Next
  411.  
  412. If RT_INIT_DONE
  413. hr = RT_Funcs\RoInitialize(#RO_INIT_MULTITHREADED)
  414. CompilerIf #MyRoDebug : Debug "RoInitialize = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  415. ;RPC_E_CHANGED_MODE = $80010106
  416. EndIf
  417.  
  418. ProcedureReturn RT_INIT_DONE
  419. EndProcedure
  420.  
  421.  
  422.  
  423. Procedure CreateHString(sString.s, *hString)
  424. ProcedureReturn RT_Funcs\WindowsCreateString(@sString, Len(sString), *hString)
  425. EndProcedure
  426.  
  427. Procedure DeleteHString(*hString)
  428. ProcedureReturn RT_Funcs\WindowsDeleteString(*hString)
  429. EndProcedure
  430.  
  431. Procedure CreateClass(sString.s, sGUID.s, *OutClass)
  432.  
  433. Protected hString, GUID.GUID, iReturn
  434. CreateHString(sString, @hString)
  435. CLSIDFromString_(@sGUID, @GUID)
  436.  
  437. iReturn = RT_Funcs\RoGetActivationFactory(hString, @GUID, *OutClass)
  438. RT_Funcs\WindowsDeleteString(hString)
  439. ProcedureReturn iReturn
  440.  
  441. EndProcedure
  442.  
  443.  
  444. Procedure WaitForAsync(*InOut.Integer)
  445.  
  446. Protected *Object.IAsyncOperationWithProgress = *InOut\i
  447. Protected IAsyncInfo.IAsyncInfo, status.l, ErrorCode.l, hr.l
  448.  
  449. hr = *Object\QueryInterface(?IID_IAsyncInfo, @IAsyncInfo)
  450. ;Debug "WaitForAsync QueryInterface = 0x" + Hex(hr)
  451.  
  452. If Not IAsyncInfo
  453. Debug "ER QueryInterface IID_IAsyncInfo failed!"
  454. ProcedureReturn 0
  455. EndIf
  456.  
  457. While 1
  458. hr = IAsyncInfo\get_Status(@status)
  459. ;Debug "IAsyncInfo::Status = 0x" + Hex(hr) + " - " + Hex(status)
  460.  
  461. If (status <> 0)
  462. If (status <> 1)
  463. IAsyncInfo\get_ErrorCode(@ErrorCode)
  464. ;Debug "ER IAsyncInfo status error 0x" + Hex(ErrorCode, #PB_Long)
  465. End
  466. EndIf
  467. IAsyncInfo\Release()
  468. Break
  469. EndIf
  470. Sleep_(10)
  471. Wend
  472.  
  473. Protected ObjectResult
  474.  
  475. *Object\GetResults(@ObjectResult)
  476. If ObjectResult
  477. *Object\Release()
  478.  
  479. *InOut\i = ObjectResult
  480. EndIf
  481. ProcedureReturn ObjectResult
  482.  
  483. EndProcedure
  484.  
  485. Procedure FileToRandomAccessStream(sFile.s)
  486.  
  487. If Not RT_INIT_DONE
  488. RT_Init()
  489. If Not RT_INIT_DONE
  490. ProcedureReturn 0
  491. EndIf
  492. EndIf
  493.  
  494. Protected IRandomAccessStream.IRandomAccessStream
  495. Protected hr.l = RT_Funcs\CreateRandomAccessStreamOnFile(@sFile, 0, ?IID_IRandomAccessStream, @IRandomAccessStream)
  496. CompilerIf #MyRoDebug : Debug "CreateRandomAccessStreamOnFile = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  497.  
  498. ProcedureReturn IRandomAccessStream
  499.  
  500. EndProcedure
  501.  
  502.  
  503. Structure PICTDESC_bmp
  504. hbitmap.i
  505. hpal.i
  506. EndStructure
  507.  
  508. Structure PICTDESC_wmf
  509. hmete.i
  510. xExt.l
  511. yExt.l
  512. EndStructure
  513.  
  514. Structure PICTDESC_icon
  515. hicon.i
  516. EndStructure
  517.  
  518. Structure PICTDESC_emf
  519. hemf.i
  520. EndStructure
  521.  
  522. Structure PICTDESC
  523. cbSizeofstruct.l
  524. picType.l
  525. StructureUnion
  526. bmp.PICTDESC_bmp
  527. wmf.PICTDESC_wmf
  528. icon.PICTDESC_icon
  529. emf.PICTDESC_emf
  530. EndStructureUnion
  531. EndStructure
  532.  
  533.  
  534. Procedure HBitmapToRandomAccessStream(hBitmap)
  535.  
  536. If Not RT_INIT_DONE
  537. RT_Init()
  538. If Not RT_INIT_DONE
  539. ProcedureReturn 0
  540. EndIf
  541. EndIf
  542.  
  543. Protected pIStream.IStream, pIPicture.IPicture, hr.l
  544. hr = CreateStreamOnHGlobal_(0, #True, @pIStream)
  545. If pIStream
  546.  
  547. #PICTYPE_BITMAP = 1
  548.  
  549. Protected PD.PICTDESC
  550. PD\cbSizeofstruct = SizeOf(PICTDESC)
  551. PD\picType = #PICTYPE_BITMAP
  552. PD\bmp\hbitmap = hBitmap
  553.  
  554. hr = OleCreatePictureIndirect_(@PD, ?IID_IPicture, #False, @pIPicture)
  555. If pIPicture
  556.  
  557. Protected cbSize
  558. hr = pIPicture\SaveAsFile(pIStream, #True, 0)
  559.  
  560. Protected pIRandomAccessStream.IRandomAccessStream
  561. #BSOS_DEFAULT = 0
  562. hr = RT_Funcs\CreateRandomAccessStreamOverStream(pIStream, #BSOS_DEFAULT, ?IID_IRandomAccessStream, @pIRandomAccessStream)
  563. pIPicture\Release()
  564. EndIf
  565.  
  566. pIStream\Release()
  567. EndIf
  568.  
  569. ProcedureReturn pIRandomAccessStream
  570.  
  571. EndProcedure
  572.  
  573.  
  574.  
  575. Procedure HBitmapFromHWND(hWnd, x = 0, y = 0, w = 0, h = 0)
  576.  
  577. Protected HDC, rc.rect, HBM, PDC
  578.  
  579. HDC = GetDC_(hWnd)
  580. If HDC
  581. GetWindowRect_(hWnd, @rc)
  582. If w = 0 : w = rc\right - rc\left : EndIf
  583. If h = 0 : h = rc\bottom - rc\top : EndIf
  584.  
  585. HBM = CreateCompatibleBitmap_(HDC, W, H)
  586. If HBM
  587. PDC = CreateCompatibleDC_(HDC)
  588. If PDC
  589. SelectObject_(PDC, HBM)
  590. BitBlt_(PDC, 0, 0, W, H, HDC, X, Y, #SRCCOPY)
  591. DeleteDC_(PDC)
  592. EndIf
  593. EndIf
  594. ReleaseDC_(hWnd, HDC)
  595. EndIf
  596. ProcedureReturn HBM
  597. EndProcedure
  598.  
  599.  
  600. ;-
  601. ;- WorkerFuncs
  602. ;-
  603.  
  604. Procedure.s GetOCRFromImage(IRandomAccessStream.IRandomAccessStream, sLanguage.s = "", bReturnLanguages = 0)
  605.  
  606. If Not RT_INIT_DONE
  607. RT_Init()
  608. If Not RT_INIT_DONE
  609. ProcedureReturn ""
  610. EndIf
  611. EndIf
  612.  
  613. Static ILanguageFactory.ILanguageFactory
  614. Static IBitmapDecoderStatics.IBitmapDecoderStatics
  615. Static IOcrEngineStatics.IOcrEngineStatics
  616. Static IGlobalizationPreferencesStatics.IGlobalizationPreferencesStatics
  617. Static MaxDimension
  618.  
  619. Protected hString, hr.l, bSupported, i, sOCRText.s
  620. Protected hText, length, *p, count, hLanguage, iFrames, width, height, iUsedLanguage
  621.  
  622. Protected IOcrEngine.IOcrEngine
  623. Protected ILanguageList.IReadOnlyList
  624. Protected ILanguageTest.ILanguage
  625.  
  626. Protected IBitmapDecoder.IBitmapDecoder
  627. Protected IBitmapFrame.IBitmapFrame
  628. Protected IClosable.IClosable
  629. Protected IOcrResult.IOcrResult
  630.  
  631. Protected ILinesList.IReadOnlyList
  632. Protected IBitmapFrameWithSoftwareBitmap.IBitmapFrameWithSoftwareBitmap
  633. Protected ISoftwareBitmap.ISoftwareBitmap
  634. Protected IOCRLine.ILanguage
  635.  
  636. If ILanguageFactory = 0 Or
  637. IBitmapDecoderStatics = 0 Or
  638. IOcrEngineStatics = 0 Or
  639. IGlobalizationPreferencesStatics = 0
  640. hr = CreateClass("Windows.Globalization.Language", "{9B0252AC-0C27-44F8-B792-9793FB66C63E}", @ILanguageFactory)
  641. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Globalization.Language = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  642.  
  643. hr = CreateClass("Windows.Graphics.Imaging.BitmapDecoder", "{438CCB26-BCEF-4E95-BAD6-23A822E58D01}", @IBitmapDecoderStatics)
  644. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Graphics.Imaging.BitmapDecoder = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  645.  
  646. hr = CreateClass("Windows.Media.Ocr.OcrEngine", "{5BFFA85A-3384-3540-9940-699120D428A8}", @IOcrEngineStatics)
  647. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Media.Ocr.OcrEngine = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  648.  
  649. hr = CreateClass("Windows.System.UserProfile.GlobalizationPreferences", "{01BF4326-ED37-4E96-B0E9-C1340D1EA158}", @IGlobalizationPreferencesStatics)
  650. CompilerIf #MyRoDebug : Debug "CreateClass Windows.System.UserProfile.GlobalizationPreferences = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  651. EndIf
  652.  
  653.  
  654. If ILanguageFactory = 0 Or
  655. IBitmapDecoderStatics = 0 Or
  656. IOcrEngineStatics = 0 Or
  657. IGlobalizationPreferencesStatics = 0
  658. Goto Release
  659. EndIf
  660.  
  661. If Not MaxDimension
  662. IOcrEngineStatics\MaxDimensions(@MaxDimension)
  663. EndIf
  664.  
  665.  
  666.  
  667. If sLanguage = ""
  668.  
  669. hr = IGlobalizationPreferencesStatics\get_Languages(@ILanguageList)
  670. CompilerIf #MyRoDebug : Debug "IGlobalizationPreferencesStatics::get_Languages = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  671.  
  672.  
  673.  
  674. If ILanguageList
  675. hr = ILanguageList\count(@count)
  676. CompilerIf #MyRoDebug : Debug "ILanguageList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  677.  
  678. For i = 0 To count -1
  679. hr = ILanguageList\get_Item(0, @hString)
  680. CompilerIf #MyRoDebug : Debug "ILanguageList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  681.  
  682. hr = ILanguageFactory\createLanguage(hString, @ILanguageTest)
  683. CompilerIf #MyRoDebug : Debug "ILanguageFactory::createLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  684.  
  685. If ILanguageTest
  686. hr = IOcrEngineStatics\IsLanguageSupported(ILanguageTest, @bSupported)
  687. CompilerIf #MyRoDebug : Debug "IOcrEngineStatics::IsLanguageSupported = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  688.  
  689. If bSupported
  690. hText = 0
  691. ILanguageTest\get_LanguageTag(@hText)
  692. CompilerIf #MyRoDebug : Debug "ILanguageTest::get_LanguageTag = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  693.  
  694. If hText
  695. *p = RT_Funcs\WindowsGetStringRawBuffer(hText, @length)
  696. If *p
  697. sLanguage = PeekS(*p)
  698. If bReturnLanguages
  699. sOCRText + sLanguage + #CRLF$
  700. EndIf
  701. ;Debug sLanguage
  702. EndIf
  703.  
  704. EndIf
  705.  
  706. EndIf
  707.  
  708. ILanguageTest\Release()
  709. EndIf
  710. Next
  711.  
  712. ILanguageList\Release()
  713. EndIf
  714.  
  715. EndIf
  716.  
  717.  
  718. If bReturnLanguages
  719. Goto Release
  720. EndIf
  721.  
  722. hString = 0
  723. CreateHString(sLanguage, @hString)
  724. If hString
  725. hr = ILanguageFactory\createLanguage(hString, @iUsedLanguage)
  726. CompilerIf #MyRoDebug : Debug "ILanguageFactory::createLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  727.  
  728. hr = RT_Funcs\WindowsDeleteString(hString)
  729. CompilerIf #MyRoDebug : Debug "WindowsDeleteString = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  730.  
  731. hr = IOcrEngineStatics\TryCreateFromLanguage(iUsedLanguage, @IOcrEngine)
  732. CompilerIf #MyRoDebug : Debug "IOcrEngineStatics::TryCreateFromLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  733.  
  734. If IOcrEngine
  735.  
  736. If IRandomAccessStream
  737. hr = IBitmapDecoderStatics\CreateAsync(IRandomAccessStream, @IBitmapDecoder)
  738. CompilerIf #MyRoDebug : Debug "IBitmapDecoderStatics::CreateAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  739.  
  740. If IBitmapDecoder
  741. WaitForAsync(@IBitmapDecoder)
  742.  
  743. hr = IBitmapDecoder\FrameCount(@iFrames)
  744. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::FrameCount = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  745.  
  746. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrame, @IBitmapFrame)
  747. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  748.  
  749. If IBitmapFrame
  750. hr = IBitmapFrame\PixelWidth(@width)
  751. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelWidth = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  752.  
  753. hr = IBitmapFrame\PixelHeight(@height)
  754. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelHeight = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  755.  
  756. If width > MaxDimension Or height > MaxDimension
  757. Debug "ER: Image is to big"
  758. Else
  759.  
  760. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrameWithSoftwareBitmap, @IBitmapFrameWithSoftwareBitmap)
  761. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  762.  
  763. If IBitmapFrameWithSoftwareBitmap
  764. hr = IBitmapFrameWithSoftwareBitmap\GetSoftwareBitmapAsync(@ISoftwareBitmap)
  765. CompilerIf #MyRoDebug : Debug "IBitmapFrameWithSoftwareBitmap::GetSoftwareBitmapAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  766.  
  767. If ISoftwareBitmap
  768. WaitForAsync(@ISoftwareBitmap)
  769.  
  770. IOcrEngine\RecognizeAsync(ISoftwareBitmap, @IOcrResult)
  771. CompilerIf #MyRoDebug : Debug "IOcrEngine::RecognizeAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  772.  
  773. If IOcrResult
  774. WaitForAsync(@IOcrResult)
  775.  
  776. hr = IOcrResult\Lines(@ILinesList)
  777. CompilerIf #MyRoDebug : Debug "IOcrResult::Lines = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  778.  
  779. If ILinesList
  780. count = 0
  781. hr = ILinesList\count(@count)
  782. CompilerIf #MyRoDebug : Debug "ILinesList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  783.  
  784. For i = 0 To count -1
  785. hText = 0
  786. hr = ILinesList\get_Item(i, @IOCRLine)
  787. CompilerIf #MyRoDebug : Debug "ILinesList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  788.  
  789. hr = IOCRLine\get_DisplayName(@hText)
  790. CompilerIf #MyRoDebug : Debug "IOCRLine::get_DisplayName = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  791.  
  792. If hText
  793. *p = RT_Funcs\WindowsGetStringRawBuffer(hText, 0)
  794. If *p
  795. sOCRText + PeekS(*p) + #CRLF$
  796. EndIf
  797. EndIf
  798. IOCRLine\Release()
  799.  
  800. Next
  801.  
  802. EndIf
  803. EndIf
  804. EndIf
  805. EndIf
  806.  
  807.  
  808. EndIf
  809. EndIf
  810.  
  811. EndIf
  812.  
  813. If ISoftwareBitmap
  814. hr = ISoftwareBitmap\QueryInterface(?IID_IClosable, @IClosable)
  815. CompilerIf #MyRoDebug : Debug "ISoftwareBitmap::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  816. If IClosable
  817. IClosable\Close()
  818. IClosable\Release()
  819. IClosable = 0
  820. EndIf
  821. EndIf
  822.  
  823. EndIf
  824.  
  825. IOcrEngine\Release()
  826.  
  827. EndIf
  828.  
  829. EndIf
  830.  
  831. Release:
  832.  
  833. If IBitmapDecoder : IBitmapDecoder\Release() : EndIf
  834. If IBitmapFrame : IBitmapFrame\Release() : EndIf
  835. If IBitmapFrameWithSoftwareBitmap : IBitmapFrameWithSoftwareBitmap\Release() : EndIf
  836. If ISoftwareBitmap : ISoftwareBitmap\Release() : EndIf
  837. If IOcrResult : IOcrResult\Release() : EndIf
  838. If ILinesList : ILinesList\Release() : EndIf
  839.  
  840. CompilerIf #MyRoDebug : Debug "### Func End ###" + #CRLF$ : CompilerEndIf
  841.  
  842. ProcedureReturn sOCRText
  843.  
  844. EndProcedure
  845.  
  846.  
  847. Procedure.l GetOCR_WordList_FromImage(IRandomAccessStream.IRandomAccessStream, List rcWords.OCR_WordRects(), sLanguage.s = "")
  848.  
  849. If Not RT_INIT_DONE
  850. RT_Init()
  851. If Not RT_INIT_DONE
  852. ProcedureReturn 0
  853. EndIf
  854. EndIf
  855.  
  856. Static ILanguageFactory.ILanguageFactory
  857. Static IBitmapDecoderStatics.IBitmapDecoderStatics
  858. Static IOcrEngineStatics.IOcrEngineStatics
  859. Static IGlobalizationPreferencesStatics.IGlobalizationPreferencesStatics
  860. Static MaxDimension
  861.  
  862. Protected hString, hr.l, bSupported, i
  863. Protected hText, length, *p, count, hLanguage, iFrames, width, height, iUsedLanguage
  864.  
  865. Protected IOcrEngine.IOcrEngine
  866. Protected ILanguageList.IReadOnlyList
  867. Protected ILanguageTest.ILanguage
  868.  
  869. Protected IBitmapDecoder.IBitmapDecoder
  870. Protected IBitmapFrame.IBitmapFrame
  871. Protected IClosable.IClosable
  872. Protected IOcrResult.IOcrResult
  873.  
  874. Protected ILinesList.IReadOnlyList
  875. Protected IBitmapFrameWithSoftwareBitmap.IBitmapFrameWithSoftwareBitmap
  876. Protected ISoftwareBitmap.ISoftwareBitmap
  877. Protected IOCRLine.IOCRLine
  878.  
  879. If ILanguageFactory = 0 Or
  880. IBitmapDecoderStatics = 0 Or
  881. IOcrEngineStatics = 0 Or
  882. IGlobalizationPreferencesStatics = 0
  883. hr = CreateClass("Windows.Globalization.Language", "{9B0252AC-0C27-44F8-B792-9793FB66C63E}", @ILanguageFactory)
  884. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Globalization.Language = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  885.  
  886. hr = CreateClass("Windows.Graphics.Imaging.BitmapDecoder", "{438CCB26-BCEF-4E95-BAD6-23A822E58D01}", @IBitmapDecoderStatics)
  887. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Graphics.Imaging.BitmapDecoder = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  888.  
  889. hr = CreateClass("Windows.Media.Ocr.OcrEngine", "{5BFFA85A-3384-3540-9940-699120D428A8}", @IOcrEngineStatics)
  890. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Media.Ocr.OcrEngine = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  891.  
  892. hr = CreateClass("Windows.System.UserProfile.GlobalizationPreferences", "{01BF4326-ED37-4E96-B0E9-C1340D1EA158}", @IGlobalizationPreferencesStatics)
  893. CompilerIf #MyRoDebug : Debug "CreateClass Windows.System.UserProfile.GlobalizationPreferences = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  894. EndIf
  895.  
  896.  
  897. If ILanguageFactory = 0 Or
  898. IBitmapDecoderStatics = 0 Or
  899. IOcrEngineStatics = 0 Or
  900. IGlobalizationPreferencesStatics = 0
  901. Goto Release
  902. EndIf
  903.  
  904. If Not MaxDimension
  905. IOcrEngineStatics\MaxDimensions(@MaxDimension)
  906. EndIf
  907.  
  908.  
  909.  
  910. If sLanguage = ""
  911.  
  912. hr = IGlobalizationPreferencesStatics\get_Languages(@ILanguageList)
  913. CompilerIf #MyRoDebug : Debug "IGlobalizationPreferencesStatics::get_Languages = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  914.  
  915.  
  916.  
  917. If ILanguageList
  918. hr = ILanguageList\count(@count)
  919. CompilerIf #MyRoDebug : Debug "ILanguageList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  920.  
  921. For i = 0 To count -1
  922. hr = ILanguageList\get_Item(0, @hString)
  923. CompilerIf #MyRoDebug : Debug "ILanguageList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  924.  
  925. hr = ILanguageFactory\createLanguage(hString, @ILanguageTest)
  926. CompilerIf #MyRoDebug : Debug "ILanguageFactory::createLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  927.  
  928. If ILanguageTest
  929. hr = IOcrEngineStatics\IsLanguageSupported(ILanguageTest, @bSupported)
  930. CompilerIf #MyRoDebug : Debug "IOcrEngineStatics::IsLanguageSupported = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  931.  
  932. If bSupported
  933. hText = 0
  934. ILanguageTest\get_LanguageTag(@hText)
  935. CompilerIf #MyRoDebug : Debug "ILanguageTest::get_LanguageTag = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  936.  
  937. If hText
  938. *p = RT_Funcs\WindowsGetStringRawBuffer(hText, @length)
  939. If *p
  940. sLanguage = PeekS(*p)
  941. ;Debug sLanguage
  942. EndIf
  943.  
  944. EndIf
  945.  
  946. EndIf
  947.  
  948. ILanguageTest\Release()
  949. EndIf
  950. Next
  951.  
  952. ILanguageList\Release()
  953. EndIf
  954.  
  955. EndIf
  956.  
  957.  
  958. hString = 0
  959. CreateHString(sLanguage, @hString)
  960. If hString
  961. hr = ILanguageFactory\createLanguage(hString, @iUsedLanguage)
  962. CompilerIf #MyRoDebug : Debug "ILanguageFactory::createLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  963.  
  964. hr = RT_Funcs\WindowsDeleteString(hString)
  965. CompilerIf #MyRoDebug : Debug "WindowsDeleteString = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  966.  
  967. hr = IOcrEngineStatics\TryCreateFromLanguage(iUsedLanguage, @IOcrEngine)
  968. CompilerIf #MyRoDebug : Debug "IOcrEngineStatics::TryCreateFromLanguage = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  969.  
  970. If IOcrEngine
  971.  
  972. If IRandomAccessStream
  973. hr = IBitmapDecoderStatics\CreateAsync(IRandomAccessStream, @IBitmapDecoder)
  974. CompilerIf #MyRoDebug : Debug "IBitmapDecoderStatics::CreateAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  975.  
  976. If IBitmapDecoder
  977. WaitForAsync(@IBitmapDecoder)
  978.  
  979. hr = IBitmapDecoder\FrameCount(@iFrames)
  980. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::FrameCount = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  981.  
  982. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrame, @IBitmapFrame)
  983. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  984.  
  985. If IBitmapFrame
  986. hr = IBitmapFrame\PixelWidth(@width)
  987. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelWidth = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  988.  
  989. hr = IBitmapFrame\PixelHeight(@height)
  990. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelHeight = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  991.  
  992. If width > MaxDimension Or height > MaxDimension
  993. Debug "ER: Image is to big"
  994. Else
  995.  
  996. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrameWithSoftwareBitmap, @IBitmapFrameWithSoftwareBitmap)
  997. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  998.  
  999. If IBitmapFrameWithSoftwareBitmap
  1000. hr = IBitmapFrameWithSoftwareBitmap\GetSoftwareBitmapAsync(@ISoftwareBitmap)
  1001. CompilerIf #MyRoDebug : Debug "IBitmapFrameWithSoftwareBitmap::GetSoftwareBitmapAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1002.  
  1003. If ISoftwareBitmap
  1004. WaitForAsync(@ISoftwareBitmap)
  1005.  
  1006. IOcrEngine\RecognizeAsync(ISoftwareBitmap, @IOcrResult)
  1007. CompilerIf #MyRoDebug : Debug "IOcrEngine::RecognizeAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1008.  
  1009. If IOcrResult
  1010. WaitForAsync(@IOcrResult)
  1011.  
  1012. hr = IOcrResult\Lines(@ILinesList)
  1013. CompilerIf #MyRoDebug : Debug "IOcrResult::Lines = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1014.  
  1015. If ILinesList
  1016.  
  1017.  
  1018. count = 0
  1019. hr = ILinesList\count(@count)
  1020. CompilerIf #MyRoDebug : Debug "ILinesList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1021.  
  1022. For i = 0 To count -1
  1023. hText = 0
  1024. hr = ILinesList\get_Item(i, @IOCRLine)
  1025. CompilerIf #MyRoDebug : Debug "ILinesList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1026.  
  1027. Protected IVectorView1.IVectorViewOcrLine, IOCRWord.IOCRWord
  1028. hr = IOCRLine\GetWords(@IVectorView1)
  1029. CompilerIf #MyRoDebug : Debug "IOCRLine::GetWords = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1030.  
  1031. If hr = 0
  1032. Protected ilenLine, j
  1033. hr = IVectorView1\get_Size(@ilenLine)
  1034. CompilerIf #MyRoDebug : Debug "IVectorView1::get_Size = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1035.  
  1036. ilenLine - 1
  1037. For j = 0 To iLenLine
  1038. hr = IVectorView1\GetAt(j, @IOCRWord)
  1039. CompilerIf #MyRoDebug : Debug "IVectorView1::GetAt = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1040.  
  1041. If SUCCEEDED_(hr)
  1042. Protected rc.frect
  1043. hr = IOCRWord\get_BoundingRect(@rc)
  1044. CompilerIf #MyRoDebug : Debug "IOCRWord::get_BoundingRect = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1045.  
  1046. hText = 0
  1047. hr = IOCRWord\get_Text(@hText)
  1048. CompilerIf #MyRoDebug : Debug "IOCRWord::get_Text = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1049. If hText
  1050. *p = RT_Funcs\WindowsGetStringRawBuffer(hText, 0)
  1051. If *p
  1052. AddElement(rcWords())
  1053. rcWords()\sWord = PeekS(*p)
  1054. rcWords()\x = rc\left
  1055. rcWords()\y = rc\top
  1056. rcWords()\w = rc\width
  1057. rcWords()\h = rc\height
  1058.  
  1059. EndIf
  1060. EndIf
  1061.  
  1062. IOCRWord\Release()
  1063. EndIf
  1064.  
  1065. ; Debug Hex(hr)
  1066. Next
  1067.  
  1068.  
  1069. IVectorView1\Release()
  1070. EndIf
  1071. ;
  1072.  
  1073.  
  1074.  
  1075. IOCRLine\Release()
  1076.  
  1077. Next
  1078.  
  1079. EndIf
  1080. EndIf
  1081. EndIf
  1082. EndIf
  1083.  
  1084.  
  1085. EndIf
  1086. EndIf
  1087.  
  1088. EndIf
  1089.  
  1090. If ISoftwareBitmap
  1091. hr = ISoftwareBitmap\QueryInterface(?IID_IClosable, @IClosable)
  1092. CompilerIf #MyRoDebug : Debug "ISoftwareBitmap::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1093. If IClosable
  1094. IClosable\Close()
  1095. IClosable\Release()
  1096. IClosable = 0
  1097. EndIf
  1098. EndIf
  1099.  
  1100. EndIf
  1101.  
  1102. IOcrEngine\Release()
  1103.  
  1104. EndIf
  1105.  
  1106. EndIf
  1107.  
  1108. Release:
  1109.  
  1110. If IBitmapDecoder : IBitmapDecoder\Release() : EndIf
  1111. If IBitmapFrame : IBitmapFrame\Release() : EndIf
  1112. If IBitmapFrameWithSoftwareBitmap : IBitmapFrameWithSoftwareBitmap\Release() : EndIf
  1113. If ISoftwareBitmap : ISoftwareBitmap\Release() : EndIf
  1114. If IOcrResult : IOcrResult\Release() : EndIf
  1115. If ILinesList : ILinesList\Release() : EndIf
  1116.  
  1117. CompilerIf #MyRoDebug : Debug "### Func End ###" + #CRLF$ : CompilerEndIf
  1118.  
  1119. ProcedureReturn ListSize(rcWords())
  1120.  
  1121. EndProcedure
  1122.  
  1123. Procedure FaceDetect(IRandomAccessStream, List rcList.rect(), maxheight = 2000)
  1124.  
  1125. Static IBitmapDecoderStatics.IBitmapDecoderStatics
  1126. Static IBitmapEncoderStatics.IBitmapEncoderStatics
  1127. Static ISoftwareBitmapStatics.ISoftwareBitmapStatics
  1128. Static IFaceDetectorStatics.IFaceDetectorStatics
  1129.  
  1130. Protected IFaceDetector.IFaceDetector
  1131. Protected IReadOnlyList.IReadOnlyList
  1132. Protected IBitmapDecoder.IBitmapDecoder
  1133. Protected IBitmapEncoder.IBitmapEncoder
  1134. Protected IBitmapFrame.IBitmapFrame
  1135. Protected IBitmapFrameWithSoftwareBitmap.IBitmapFrameWithSoftwareBitmap
  1136. Protected IBitmapTransform.IBitmapTransform
  1137. Protected ISoftwareBitmap.ISoftwareBitmap, ISoftwareBitmapTemp.ISoftwareBitmap
  1138. Protected IClosable.IClosable
  1139. Protected IDetectedFaceList.IReadOnlyList
  1140. Protected IDetectedFace.IDetectedFace
  1141.  
  1142. Protected hr.l, iCount, i, hString, *p
  1143. Protected BitmapPixelFormat
  1144. Protected sSupportedBitmapPixelFormats.s
  1145. Protected width, height, rc.rect
  1146.  
  1147. If Not RT_INIT_DONE
  1148. RT_Init()
  1149. If Not RT_INIT_DONE
  1150. ProcedureReturn 0
  1151. EndIf
  1152. EndIf
  1153.  
  1154. If IBitmapDecoderStatics = 0 Or
  1155. IBitmapEncoderStatics = 0 Or
  1156. ISoftwareBitmapStatics = 0 Or
  1157. IFaceDetectorStatics = 0
  1158.  
  1159. hr = CreateClass("Windows.Graphics.Imaging.BitmapDecoder", "{438CCB26-BCEF-4E95-BAD6-23A822E58D01}", @IBitmapDecoderStatics)
  1160. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Graphics.Imaging.BitmapDecoder = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1161.  
  1162. hr = CreateClass("Windows.Graphics.Imaging.BitmapEncoder", "{A74356A7-A4E4-4EB9-8E40-564DE7E1CCB2}", @IBitmapEncoderStatics)
  1163. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Graphics.Imaging.BitmapEncoder = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1164.  
  1165. hr = CreateClass("Windows.Graphics.Imaging.SoftwareBitmap", "{DF0385DB-672F-4A9D-806E-C2442F343E86}", @ISoftwareBitmapStatics)
  1166. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Graphics.Imaging.SoftwareBitmap = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1167.  
  1168. hr = CreateClass("Windows.Media.FaceAnalysis.FaceDetector", "{BC042D67-9047-33F6-881B-6746C1B218B8}", @IFaceDetectorStatics)
  1169. CompilerIf #MyRoDebug : Debug "CreateClass Windows.Media.FaceAnalysis.FaceDetector = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1170.  
  1171. EndIf
  1172.  
  1173.  
  1174. If IBitmapDecoderStatics = 0 Or
  1175. IBitmapEncoderStatics = 0 Or
  1176. ISoftwareBitmapStatics = 0 Or
  1177. IFaceDetectorStatics = 0
  1178. Goto Release
  1179. EndIf
  1180.  
  1181.  
  1182. hr = IFaceDetectorStatics\CreateAsync(@IFaceDetector)
  1183. CompilerIf #MyRoDebug : Debug "IFaceDetectorStatics::CreateAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1184.  
  1185. If IFaceDetector
  1186.  
  1187. WaitForAsync(@IFaceDetector)
  1188. hr = IFaceDetectorStatics\GetSupportedBitmapPixelFormats(@IReadOnlyList)
  1189. CompilerIf #MyRoDebug : Debug "IFaceDetectorStatics::GetSupportedBitmapPixelFormats = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1190.  
  1191. If IReadOnlyList
  1192. hr = IReadOnlyList\count(@iCount)
  1193. CompilerIf #MyRoDebug : Debug "IReadOnlyList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1194.  
  1195. For i = 0 To iCount -1
  1196. hr = IReadOnlyList\get_Item(i, @BitmapPixelFormat)
  1197. CompilerIf #MyRoDebug : Debug "ILanguageList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1198.  
  1199. sSupportedBitmapPixelFormats + "|" + Str(BitmapPixelFormat) + "|"
  1200.  
  1201. Next
  1202.  
  1203. IReadOnlyList\Release()
  1204.  
  1205. EndIf
  1206.  
  1207. IFaceDetectorStatics\Release()
  1208.  
  1209. EndIf
  1210.  
  1211.  
  1212.  
  1213. hr = IBitmapDecoderStatics\CreateAsync(IRandomAccessStream, @IBitmapDecoder)
  1214. CompilerIf #MyRoDebug : Debug "IBitmapDecoderStatics::CreateAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1215.  
  1216. If IBitmapDecoder
  1217. WaitForAsync(@IBitmapDecoder)
  1218. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrame, @IBitmapFrame)
  1219. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1220.  
  1221. If IBitmapFrame
  1222. hr = IBitmapFrame\PixelWidth(@width)
  1223. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelWidth = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1224.  
  1225. hr = IBitmapFrame\PixelHeight(@height)
  1226. CompilerIf #MyRoDebug : Debug "IBitmapFrame::PixelHeight = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1227.  
  1228. hr = IBitmapFrame\BitmapPixelFormat(@BitmapPixelFormat)
  1229. CompilerIf #MyRoDebug : Debug "IBitmapFrame::BitmapPixelFormat = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1230.  
  1231. hr = IBitmapDecoder\QueryInterface(?IID_IBitmapFrameWithSoftwareBitmap, @IBitmapFrameWithSoftwareBitmap)
  1232. CompilerIf #MyRoDebug : Debug "IBitmapDecoder::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1233.  
  1234. If IBitmapFrameWithSoftwareBitmap
  1235.  
  1236. If (height > maxheight)
  1237. hr = IBitmapEncoderStatics\CreateForTranscodingAsync(IRandomAccessStream, IBitmapDecoder, @IBitmapEncoder)
  1238. CompilerIf #MyRoDebug : Debug "IBitmapEncoderStatics::CreateForTranscodingAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1239.  
  1240. If IBitmapEncoder
  1241. WaitForAsync(@IBitmapEncoder)
  1242. hr = IBitmapEncoder\BitmapTransform(@IBitmapTransform)
  1243. CompilerIf #MyRoDebug : Debug "IBitmapEncoder::BitmapTransform = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1244.  
  1245. If IBitmapTransform
  1246. hr = IBitmapTransform\ScaledWidth((maxheight/height*width))
  1247. CompilerIf #MyRoDebug : Debug "IBitmapTransform::ScaledWidth = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1248.  
  1249. hr = IBitmapTransform\ScaledHeight(maxheight)
  1250. CompilerIf #MyRoDebug : Debug "IBitmapTransform::ScaledHeight = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1251.  
  1252. hr = IBitmapFrameWithSoftwareBitmap\GetSoftwareBitmapTransformedAsync(BitmapPixelFormat, 0, IBitmapTransform, 0, 0, @ISoftwareBitmap)
  1253. CompilerIf #MyRoDebug : Debug "IBitmapFrameWithSoftwareBitmap::GetSoftwareBitmapTransformedAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1254.  
  1255. EndIf
  1256.  
  1257. EndIf
  1258.  
  1259. Else
  1260. hr = IBitmapFrameWithSoftwareBitmap\GetSoftwareBitmapAsync(@ISoftwareBitmap)
  1261. CompilerIf #MyRoDebug : Debug "IBitmapFrameWithSoftwareBitmap::GetSoftwareBitmapTransformedAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1262. EndIf
  1263.  
  1264. If ISoftwareBitmap
  1265. WaitForAsync(@ISoftwareBitmap)
  1266.  
  1267. If Not FindString(sSupportedBitmapPixelFormats, "|" + BitmapPixelFormat + "|")
  1268. hr = ISoftwareBitmapStatics\Convert(ISoftwareBitmap, 62, @ISoftwareBitmapTemp)
  1269. CompilerIf #MyRoDebug : Debug "ISoftwareBitmapStatics::Convert = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1270. hr = ISoftwareBitmap\QueryInterface(?IID_IClosable, @IClosable)
  1271.  
  1272. If IClosable
  1273. IClosable\Close()
  1274. IClosable\Release()
  1275. EndIf
  1276.  
  1277. ISoftwareBitmap\Release()
  1278.  
  1279. ISoftwareBitmap = ISoftwareBitmapTemp
  1280.  
  1281. EndIf
  1282.  
  1283. hr = IFaceDetector\DetectFacesAsync(ISoftwareBitmap, @IDetectedFaceList)
  1284. CompilerIf #MyRoDebug : Debug "IFaceDetector\DetectFacesAsync = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1285.  
  1286. If IDetectedFaceList
  1287. WaitForAsync(@IDetectedFaceList)
  1288.  
  1289. hr = IDetectedFaceList\count(@iCount)
  1290. CompilerIf #MyRoDebug : Debug "IDetectedFaceList::count = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1291.  
  1292. For i = 0 To iCount -1
  1293.  
  1294. hr = IDetectedFaceList\get_Item(i, @IDetectedFace)
  1295. CompilerIf #MyRoDebug : Debug "IDetectedFaceList::get_Item = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1296.  
  1297. If IDetectedFace
  1298. AddElement(rcList())
  1299. hr = IDetectedFace\FaceBox(@rcList())
  1300. CompilerIf #MyRoDebug : Debug "IDetectedFace::FaceBox = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1301.  
  1302. IDetectedFace\Release()
  1303. IDetectedFace = 0
  1304. EndIf
  1305.  
  1306. Next
  1307.  
  1308. EndIf
  1309.  
  1310.  
  1311. hr = ISoftwareBitmap\QueryInterface(?IID_IClosable, @IClosable)
  1312. CompilerIf #MyRoDebug : Debug "ISoftwareBitmap::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1313. If IClosable
  1314. IClosable\Close()
  1315. IClosable\Release()
  1316. IClosable = 0
  1317. EndIf
  1318.  
  1319.  
  1320. EndIf
  1321.  
  1322. EndIf
  1323.  
  1324. EndIf
  1325.  
  1326. EndIf
  1327.  
  1328. Release:
  1329.  
  1330.  
  1331. If IBitmapDecoder : IBitmapDecoder\Release() : EndIf
  1332. If IBitmapFrame : IBitmapFrame\Release() : EndIf
  1333.  
  1334. If (height > maxheight)
  1335. If IBitmapEncoder : IBitmapEncoder\Release() : EndIf
  1336. If IBitmapTransform : IBitmapTransform\Release() : EndIf
  1337. EndIf
  1338.  
  1339. If IBitmapFrameWithSoftwareBitmap : IBitmapFrameWithSoftwareBitmap\Release() : EndIf
  1340. If IDetectedFaceList : IDetectedFaceList\Release() : EndIf
  1341.  
  1342. If IFaceDetector : IFaceDetector\Release() : EndIf
  1343.  
  1344. EndProcedure
  1345.  
  1346. ;-
  1347. ;- Module Exports
  1348. ;-
  1349.  
  1350. Procedure.s get_Languages()
  1351. ProcedureReturn GetOCRFromImage(0, "", 1)
  1352. EndProcedure
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359. Procedure.s get_TextFromFile(sFile.s, sLanguage.s = "")
  1360.  
  1361. Protected sOutPut.s
  1362. Protected IRandomAccessStream.IRandomAccessStream = FileToRandomAccessStream(sFile)
  1363. If IRandomAccessStream
  1364. sOutPut = GetOCRFromImage(IRandomAccessStream, sLanguage, 0)
  1365.  
  1366. Protected IClosable.IClosable
  1367. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1368. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1369.  
  1370. If IClosable
  1371. IClosable\Close()
  1372. IClosable\Release()
  1373. IClosable = 0
  1374. EndIf
  1375. IRandomAccessStream\Release()
  1376.  
  1377. EndIf
  1378. ProcedureReturn sOutPut
  1379.  
  1380. EndProcedure
  1381.  
  1382.  
  1383.  
  1384. Procedure.s get_TextFromImageID(ImageID, sLanguage.s = "")
  1385.  
  1386. Protected sOutPut.s
  1387. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1388. If IRandomAccessStream
  1389. sOutPut = GetOCRFromImage(IRandomAccessStream, sLanguage, 0)
  1390.  
  1391. Protected IClosable.IClosable
  1392. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1393. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1394.  
  1395. If IClosable
  1396. IClosable\Close()
  1397. IClosable\Release()
  1398. IClosable = 0
  1399. EndIf
  1400. IRandomAccessStream\Release()
  1401.  
  1402. EndIf
  1403. ProcedureReturn sOutPut
  1404. EndProcedure
  1405.  
  1406.  
  1407. Procedure.s get_TextFromHWND(hWND, sLanguage.s = "")
  1408.  
  1409. Protected sOutPut.s
  1410. Protected ImageID = HBitmapFromHWND(hWND)
  1411. If ImageID
  1412. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1413. If IRandomAccessStream
  1414. sOutPut = GetOCRFromImage(IRandomAccessStream, sLanguage, 0)
  1415.  
  1416. Protected IClosable.IClosable
  1417. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1418. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1419.  
  1420. If IClosable
  1421. IClosable\Close()
  1422. IClosable\Release()
  1423. IClosable = 0
  1424. EndIf
  1425. IRandomAccessStream\Release()
  1426.  
  1427. EndIf
  1428. DeleteObject_(ImageID)
  1429. EndIf
  1430. ProcedureReturn sOutPut
  1431. EndProcedure
  1432.  
  1433. ;-
  1434.  
  1435.  
  1436. Procedure.l get_WordListFromFile(sFile.s, List rcWords.OCR_WordRects(), sLanguage.s = "")
  1437.  
  1438. Protected iWords
  1439. Protected IRandomAccessStream.IRandomAccessStream = FileToRandomAccessStream(sFile)
  1440. If IRandomAccessStream
  1441. iWords = GetOCR_WordList_FromImage(IRandomAccessStream, rcWords.OCR_WordRects(), sLanguage)
  1442.  
  1443. Protected IClosable.IClosable
  1444. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1445. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1446.  
  1447. If IClosable
  1448. IClosable\Close()
  1449. IClosable\Release()
  1450. IClosable = 0
  1451. EndIf
  1452. IRandomAccessStream\Release()
  1453.  
  1454. EndIf
  1455. ProcedureReturn iWords
  1456.  
  1457. EndProcedure
  1458.  
  1459.  
  1460.  
  1461. Procedure.l get_WordListFromImageID(ImageID, List rcWords.OCR_WordRects(), sLanguage.s = "")
  1462.  
  1463. Protected iWords
  1464. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1465. If IRandomAccessStream
  1466. iWords = GetOCR_WordList_FromImage(IRandomAccessStream, rcWords.OCR_WordRects(), sLanguage)
  1467.  
  1468. Protected IClosable.IClosable
  1469. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1470. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1471.  
  1472. If IClosable
  1473. IClosable\Close()
  1474. IClosable\Release()
  1475. IClosable = 0
  1476. EndIf
  1477. IRandomAccessStream\Release()
  1478.  
  1479. EndIf
  1480. ProcedureReturn iWords
  1481. EndProcedure
  1482.  
  1483.  
  1484. Procedure.l get_WordListFromHWND(hWND, List rcWords.OCR_WordRects(), sLanguage.s = "")
  1485.  
  1486. Protected iWords
  1487. Protected ImageID = HBitmapFromHWND(hWND)
  1488. If ImageID
  1489. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1490. If IRandomAccessStream
  1491. iWords = GetOCR_WordList_FromImage(IRandomAccessStream, rcWords.OCR_WordRects(), sLanguage)
  1492.  
  1493. Protected IClosable.IClosable
  1494. Protected hr.l = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1495. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1496.  
  1497. If IClosable
  1498. IClosable\Close()
  1499. IClosable\Release()
  1500. IClosable = 0
  1501. EndIf
  1502. IRandomAccessStream\Release()
  1503.  
  1504. EndIf
  1505. DeleteObject_(ImageID)
  1506. EndIf
  1507. ProcedureReturn iWords
  1508. EndProcedure
  1509.  
  1510. ;-
  1511.  
  1512. Procedure get_FacesFromFile(sFile.s, List rcList.rect())
  1513.  
  1514. Protected iCount
  1515. Protected IRandomAccessStream.IRandomAccessStream = FileToRandomAccessStream(sFile)
  1516. If IRandomAccessStream
  1517. iCount = facedetect(IRandomAccessStream, rcList())
  1518.  
  1519. Protected IClosable.IClosable
  1520. Protected hr = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1521. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1522.  
  1523. If IClosable
  1524. IClosable\Close()
  1525. IClosable\Release()
  1526. IClosable = 0
  1527. EndIf
  1528. IRandomAccessStream\Release()
  1529.  
  1530. EndIf
  1531. ProcedureReturn iCount
  1532.  
  1533. EndProcedure
  1534.  
  1535. ;-
  1536.  
  1537.  
  1538. Procedure get_FacesFromImageID(ImageID, List rcList.rect())
  1539.  
  1540. Protected iCount
  1541. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1542. If IRandomAccessStream
  1543. iCount = facedetect(IRandomAccessStream, rcList())
  1544.  
  1545. Protected IClosable.IClosable
  1546. Protected hr = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1547. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1548.  
  1549. If IClosable
  1550. IClosable\Close()
  1551. IClosable\Release()
  1552. IClosable = 0
  1553. EndIf
  1554. IRandomAccessStream\Release()
  1555.  
  1556. EndIf
  1557. ProcedureReturn iCount
  1558.  
  1559. EndProcedure
  1560.  
  1561.  
  1562. Procedure get_FacesFromHWND(hWND, List rcList.rect())
  1563.  
  1564. Protected iCount
  1565. Protected ImageID = HBitmapFromHWND(hWND)
  1566. If ImageID
  1567. Protected IRandomAccessStream.IRandomAccessStream = HBitmapToRandomAccessStream(ImageID)
  1568. If IRandomAccessStream
  1569. iCount = facedetect(IRandomAccessStream, rcList())
  1570.  
  1571. Protected IClosable.IClosable
  1572. Protected hr = IRandomAccessStream\QueryInterface(?IID_IClosable, @IClosable)
  1573. CompilerIf #MyRoDebug : Debug "IRandomAccessStream::QueryInterface = 0x" + Hex(hr, #PB_Long) : CompilerEndIf
  1574.  
  1575. If IClosable
  1576. IClosable\Close()
  1577. IClosable\Release()
  1578. IClosable = 0
  1579. EndIf
  1580. IRandomAccessStream\Release()
  1581.  
  1582. EndIf
  1583.  
  1584. DeleteObject_(ImageID)
  1585. EndIf
  1586.  
  1587. ProcedureReturn iCount
  1588.  
  1589. EndProcedure
  1590.  
  1591. EndModule
  1592.  
  1593.  
  1594.  
  1595. CompilerIf #PB_Compiler_IsMainFile
  1596.  
  1597.  
  1598. Structure tOCR_DEMO
  1599. sFile.s
  1600. ImageID.i
  1601. ;
  1602. sLanguages.s
  1603. sFromFile.s
  1604. sFromImage.s
  1605.  
  1606. List rcFaces.Rect()
  1607. List rcWords.WinOCR::OCR_WordRects()
  1608. EndStructure
  1609.  
  1610. ;-
  1611.  
  1612. Procedure OCR_DEMO(*OCR_DEMO.tOCR_DEMO)
  1613. *OCR_DEMO\sLanguages = WinOCR::get_Languages()
  1614. If *OCR_DEMO\sFile <> ""
  1615. *OCR_DEMO\sFromFile = WinOCR::get_TextFromFile(*OCR_DEMO\sFile)
  1616. EndIf
  1617. If *OCR_DEMO\ImageID
  1618. *OCR_DEMO\sFromImage = WinOCR::get_TextFromImageID(*OCR_DEMO\ImageID)
  1619. EndIf
  1620.  
  1621. WinOCR::get_FacesFromFile(*OCR_DEMO\sFile, *OCR_DEMO\rcFaces())
  1622.  
  1623. WinOCR::get_WordListFromFile(*OCR_DEMO\sFile, *OCR_DEMO\rcWords())
  1624.  
  1625. EndProcedure
  1626.  
  1627.  
  1628.  
  1629. Procedure HBitmapFromScreen(X, Y, W, H)
  1630. Protected HDC = GetDC_(0)
  1631. Protected HBM = CreateCompatibleBitmap_(HDC, W, H)
  1632. Protected PDC = CreateCompatibleDC_(HDC)
  1633. SelectObject_(PDC, HBM)
  1634. BitBlt_(PDC, 0, 0, W, H, HDC, X, Y, #SRCCOPY)
  1635. DeleteDC_(PDC)
  1636. ReleaseDC_(0, HDC)
  1637. ProcedureReturn HBM
  1638. EndProcedure
  1639.  
  1640.  
  1641.  
  1642.  
  1643. Define tOCR_DEMO.tOCR_DEMO
  1644.  
  1645. tOCR_DEMO\sFile = OpenFileRequester("Choose Image File", "Image.png", "ImageFiles (*.png;jpg;gif;bmp;tif;tiff)|*.png;*.jpg;*.gif;*.bmp;*.tif;*.tiff", 0)
  1646. If tOCR_DEMO\sFile
  1647.  
  1648. UsePNGImageDecoder()
  1649. UseJPEGImageDecoder()
  1650. UseTIFFImageDecoder()
  1651. LoadImage(0, tOCR_DEMO\sFile)
  1652. tOCR_DEMO\ImageID = ImageID(0)
  1653.  
  1654. tOCR_DEMO\ImageID = HBitmapFromScreen(0, 0, 800, 600)
  1655.  
  1656. Define thread = CreateThread(@OCR_DEMO(), @tOCR_DEMO)
  1657. If thread
  1658. WaitThread(thread)
  1659.  
  1660. Define sFaces.s = "Detected Faces = " + Str(ListSize(tOCR_DEMO\rcFaces())) + #CRLF$
  1661. ForEach tOCR_DEMO\rcFaces()
  1662. sFaces + #CRLF$ + Str(tOCR_DEMO\rcFaces()\left) + ", " + Str(tOCR_DEMO\rcFaces()\top) + ", " + Str(tOCR_DEMO\rcFaces()\right) + ", " + Str(tOCR_DEMO\rcFaces()\bottom)
  1663. Next
  1664.  
  1665. MessageRequester("Available Languages", tOCR_DEMO\sLanguages)
  1666. MessageRequester("TextFromFile", tOCR_DEMO\sFromFile)
  1667. MessageRequester("TextFromImageID", tOCR_DEMO\sFromImage)
  1668.  
  1669. MessageRequester("Faces", sFaces)
  1670.  
  1671. SetClipboardText(tOCR_DEMO\sFromImage)
  1672.  
  1673. Define sWord.s
  1674. ForEach tOCR_DEMO\rcWords()
  1675. sWord + #CRLF$ + Str(tOCR_DEMO\rcWords()\x) + ", " + Str(tOCR_DEMO\rcWords()\y) + ", " + Str(tOCR_DEMO\rcWords()\w) + ", " + Str(tOCR_DEMO\rcWords()\h) + #TAB$ + tOCR_DEMO\rcWords()\sWord
  1676. Next
  1677.  
  1678. MessageRequester("Words from " + tOCR_DEMO\sFile, sWord)
  1679.  
  1680. EndIf
  1681. EndIf
  1682.  
  1683. CompilerEndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement