我正在嘗試決議數字和螢屏上的一些字母。網上有很多主題,但 99% 都是過時的,而且他們都在問如何從影像中獲取文本(我不需要)。就我而言,我只想要一個透明的圖片框,可以掃描下面的任何文本。我找到了一個很 老的youtube教程
寫在生物上的代碼是
Imports Emgu.CV
Imports Emgu.Util
Imports Emgu.CV.OCR
Imports Emgu.CV.Structure
Public Class Form1
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
Dim pic As Bitmap = New Bitmap(270, 100)
Dim gfx As Graphics = Graphics.FromImage(pic)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'If Windows XP
gfx.CopyFromScreen(New Point(Me.Location.X PictureBox1.Location.X 4, Me.Location.Y PictureBox1.Location.Y 30), New Point(0, 0), pic.Size)
PictureBox1.Image = pic
'If Windows 7
'gfx.CopyFromScreen(MousePositi-on, New Point(0, 0), pic.Size)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OCRz.Recognize(New Image(Of Bgr, Byte)(pic))
RichTextBox1.Text = OCRz.GetText
End Sub
End Class
這是一個 8 y 的舊代碼,所以我認為一定有更好的東西。我從 nuget 下載了 emgu.cv,這是第一個也是下載次數最多的軟體包,但在運行時出現錯誤“'無法使用路徑 'tessdata' 和語言 'eng' 創建 ocr 模型。'”在編譯時我收到錯誤“' Tesseract' 未定義。”。這真的很令人沮喪,因為我到處尋找,也在 c# 論壇中尋找,但沒有一個可以幫助我。你有什么解決辦法嗎?我會很感激你的幫助。謝謝
uj5u.com熱心網友回復:
那個代碼很舊,我覺得它不能正常作業。你考慮過Windows.Media.OCR嗎? 需要Windows v.10 SDK。
使用的控制元件:
- 1 個透明圖片框(使用與表單透明鍵相同的背景色,我使用的是灰色)
- 1 個富文本框
- 1 個按鈕
添加作為參考:
“C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd”
“C:\ProgramFiles(x86)\ReferenceAssemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll”
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.TryCreateFromUserProfileLanguages()
'If you want to scan only letters, set the language
'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
'Follow lines are just to test how the OCR engine has cutted the lines from the whole text
Dim lines As IReadOnlyList(Of OcrLine) = ocrResult.Lines
For Each line In lines
Console.WriteLine(line.Text)
Next
End Sub
End Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402841.html
標籤:
上一篇:如果選擇索引,則串列視圖條件
