我正在使用將通過透明圖片框識別文本的 Windows.Media.Ocr。它適用于以下代碼:
Imports Windows.Media.Ocr
Imports System.IO
Imports System.Runtime.InteropServices.WindowsRuntime
Public Class Form1
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim softwareBmp As Windows.Graphics.Imaging.SoftwareBitmap
Using bmp As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Using g As Graphics = Graphics.FromImage(bmp)
Dim pt As Point = Me.PointToScreen(New Point(PictureBox1.Left, PictureBox1.Top))
g.CopyFromScreen(pt.X, pt.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy)
Using memStream = New Windows.Storage.Streams.InMemoryRandomAccessStream()
bmp.Save(memStream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp)
Dim decoder As Windows.Graphics.Imaging.BitmapDecoder = Await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memStream)
softwareBmp = Await decoder.GetSoftwareBitmapAsync()
End Using
End Using
End Using
Dim ocrEng = OcrEngine.TryCreateFromLanguage(New Windows.Globalization.Language("en-US"))
Dim languages As IReadOnlyList(Of Windows.Globalization.Language) = ocrEng.AvailableRecognizerLanguages
For Each language In languages
Console.WriteLine(language.LanguageTag)
Next
Dim r = ocrEng.RecognizerLanguage
Dim n = ocrEng.MaxImageDimension
Dim ocrResult = Await ocrEng.RecognizeAsync(softwareBmp)
RichTextBox1.Text = ocrResult.Text
End Sub
End Class
但如果寬度和或高度有點小,它似乎不會掃描任何東西,比如寬度:89和高度:27,即使文本在上面確實可見。. 當height 至少為 50且width 至少為 40時,它似乎開始掃描。這是ocr引擎的限制嗎?謝謝
uj5u.com熱心網友回復:
我通過調整最終 SoftwareBitmap 的大小進行了測驗,它似乎適用于一個小的 PictureBox 高度(用 32 測驗,它不適用于原始 SoftwareBitmap )
添加:
Dim scaledSoftwareBitmap As Windows.Graphics.Imaging.SoftwareBitmap
并在行后softwareBmp = Await decoder.GetSoftwareBitmapAsync()添加
Dim transform = New Windows.Graphics.Imaging.BitmapTransform() With {
.ScaledWidth = CUInt(PictureBox1.Width * 2),
.ScaledHeight = CUInt(PictureBox1.Height * 2),
.InterpolationMode = Windows.Graphics.Imaging.BitmapInterpolationMode.NearestNeighbor
}
Dim pixelData As Windows.Graphics.Imaging.PixelDataProvider = Await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, transform, Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.ColorManageToSRgb)
scaledSoftwareBitmap = New Windows.Graphics.Imaging.SoftwareBitmap(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, CInt(transform.ScaledWidth), CInt(transform.ScaledHeight))
Dim pixels As Byte() = pixelData.DetachPixelData()
Dim buffer As Windows.Storage.Streams.IBuffer = pixels.AsBuffer()
scaledSoftwareBitmap.CopyFromBuffer(buffer)
并更換
Dim ocrResult = Await ocrEng.RecognizeAsync(softwareBmp)
和
Dim ocrResult = Await ocrEng.RecognizeAsync(scaledSoftwareBitmap)
現在它應該可以作業了
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410503.html
標籤:
上一篇:如何洗掉richtextbox多行中整數旁邊的字符并將它們求和
下一篇:將按鈕與textarea角對齊
