Files
Yuzu-GCA/ocr_script.ps1
2026-07-19 22:57:19 +08:00

44 lines
1.7 KiB
PowerShell

# PowerShell script using Windows built-in OCR
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Runtime.WindowsRuntime
Add-Type -AssemblyName Windows.Foundation
Add-Type -AssemblyName Windows.Graphics.Imaging
Add-Type -AssemblyName Windows.Media.Ocr
$winRtTypes = [Windows.Media.Ocr.OcrEngine]::GetType()
# Load the image
$imagePath = "D:\Yuzu-GCA\screen2.png"
$bitmap = [System.Drawing.Bitmap]::FromFile($imagePath)
Write-Host "Image size: $($bitmap.Width) x $($bitmap.Height)"
# Convert to stream
$ms = New-Object System.IO.MemoryStream
$bitmap.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
$bitmap.Dispose()
# Create BitmapDecoder
$ms.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
$randomAccessStream = New-Object Windows.Storage.Streams.InMemoryRandomAccessStream
$outputStream = $randomAccessStream.GetOutputStreamAt(0)
$dataWriter = New-Object Windows.Storage.Streams.DataWriter($outputStream)
$bytes = $ms.ToArray()
$dataWriter.WriteBytes($bytes)
$dataWriter.StoreAsync().GetAwaiter().GetResult() | Out-Null
$dataWriter.FlushAsync().GetAwaiter().GetResult() | Out-Null
$ms.Dispose()
$decoder = [Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($randomAccessStream).GetAwaiter().GetResult()
$softwareBitmap = [Windows.Graphics.Imaging.SoftwareBitmap]::Convert($decoder.GetSoftwareBitmapAsync().GetAwaiter().GetResult(), [Windows.Graphics.Imaging.BitmapPixelFormat]::Bgra8, [Windows.Graphics.Imaging.BitmapAlphaMode]::Premultiplied)
# Perform OCR
$engine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromUserProfileLanguages()
$result = $engine.RecognizeAsync($softwareBitmap).GetAwaiter().GetResult()
# Output results
Write-Host "Recognized text:"
Write-Host "===================="
foreach ($line in $result.Lines) {
Write-Host $line.Text
}