# win_ocr.ps1 - Windows 自带 OCR (Windows.Media.Ocr) 识别本地图片 # 用法: powershell -NoProfile -ExecutionPolicy Bypass -File win_ocr.ps1 -Image [-Lang zh-Hans] # 输出: UTF-8 JSON => {"ok":true,"items":[{"text","x","y","w","h"},...]} param( [Parameter(Mandatory=$true)][string]$Image, [string]$Lang = "zh-Hans" ) $ErrorActionPreference = "Stop" $path = (Resolve-Path $Image -ErrorAction SilentlyContinue) if (-not $path) { Write-Output '{"ok":false,"error":"image not found"}'; exit 1 } $path = $path.Path # ---- 正确加载 WinRT 类型 ---- try { Add-Type -AssemblyName System.Runtime.WindowsRuntime [void][System.WindowsRuntimeSystemExtensions, System.Runtime.WindowsRuntime, ContentType=WindowsRuntime] } catch { Write-Output '{"ok":false,"error":"winrt load fail"}'; exit 1 } $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] Function Await($WinRtTask, $ResultType) { $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) $netTask = $asTask.Invoke($null, @($WinRtTask)) $netTask.Wait(-1) | Out-Null $netTask.Result } function Write-Json($obj) { [Console]::OutputEncoding = [Text.Encoding]::UTF8 Write-Output ($obj | ConvertTo-Json -Compress -Depth 5) } try { $file = [Windows.Storage.StorageFile, Windows.Storage, ContentType=WindowsRuntime]::GetFileFromPathAsync($path) $storageFile = Await $file ([Windows.Storage.StorageFile]) $stream = Await ($storageFile.OpenAsync([Windows.Storage.FileAccessMode]::Read)) ([Windows.Storage.Streams.IRandomAccessStream]) $decoder = Await ([Windows.Graphics.Imaging.BitmapDecoder, Windows.Graphics.Imaging, ContentType=WindowsRuntime]::CreateAsync($stream)) ([Windows.Graphics.Imaging.BitmapDecoder]) $bitmap = Await ($decoder.GetSoftwareBitmapAsync()) ([Windows.Graphics.Imaging.SoftwareBitmap]) $engine = $null try { $langObj = New-Object Windows.Globalization.Language $Lang $engine = [Windows.Media.Ocr.OcrEngine, Windows.Media.Ocr, ContentType=WindowsRuntime]::TryCreateFromLanguage($langObj) } catch { $engine = $null } if ($engine -eq $null) { $engine = [Windows.Media.Ocr.OcrEngine, Windows.Media.Ocr, ContentType=WindowsRuntime]::TryCreateFromUserProfileLanguages() } if ($engine -eq $null) { Write-Json @{ ok = $false; error = 'no OCR engine (need language pack?)' } exit 1 } $result = Await ($engine.RecognizeAsync($bitmap)) ([Windows.Media.Ocr.OcrResult]) $items = @() foreach ($line in $result.Lines) { if ($line.Words.Count -eq 0) { continue } $t = ($line.Words | ForEach-Object { $_.Text }) -join " " $minX = [math]::Floor($line.Words[0].BoundingRect.X) $minY = [math]::Floor($line.Words[0].BoundingRect.Y) $maxX = [math]::Ceiling($line.Words[0].BoundingRect.X + $line.Words[0].BoundingRect.Width) $maxY = [math]::Ceiling($line.Words[0].BoundingRect.Y + $line.Words[0].BoundingRect.Height) foreach ($word in $line.Words) { $r = $word.BoundingRect if ($r.X -lt $minX) { $minX = [math]::Floor($r.X) } if ($r.Y -lt $minY) { $minY = [math]::Floor($r.Y) } if (($r.X + $r.Width) -gt $maxX) { $maxX = [math]::Ceiling($r.X + $r.Width) } if (($r.Y + $r.Height) -gt $maxY) { $maxY = [math]::Ceiling($r.Y + $r.Height) } } $items += [PSCustomObject]@{ text = $t x = $minX; y = $minY; w = ($maxX - $minX); h = ($maxY - $minY) } } Write-Json @{ ok = $true; items = $items } } catch { Write-Json @{ ok = $false; error = $_.Exception.Message } exit 1 }