'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MOcr
Friend Class Program
Private Const CHAR_IMAGE_FILE As String = MIL.M_IMAGE_PATH & "OcrSemi1292.mim"
Private Const CHAR_SIZE_X_MIN As Double = 22.0
Private Const CHAR_SIZE_X_MAX As Double = 23.0
Private Const CHAR_SIZE_X_STEP As Double = 0.5
Private Const CHAR_SIZE_Y_MIN As Double = 43.0
Private Const CHAR_SIZE_Y_MAX As Double = 44.0
Private Const CHAR_SIZE_Y_STEP As Double = 0.5
Private Const READ_REGION_POS_X As Integer = 30
Private Const READ_REGION_POS_Y As Integer = 40
Private Const READ_REGION_WIDTH As Integer = 420
Private Const READ_REGION_HEIGHT As Integer = 70
Private Const READ_SCORE_MIN As Double = 50.0
Private Const FONT_FILE_IN As String = MIL.M_CONTEXT_PATH + "SEMI_M12-92.mfo"
Private Const FONT_FILE_OUT As String = MIL.M_TEMP_DIR + "Semi1292Calibrated.mfo"
Private Const STRING_LENGTH As Integer = 13
Private Const STRING_CALIBRATION As String = "M940902-MXD5"
Shared Sub Main(ByVal args() As String)
Dim MilApplication As MIL_ID = MIL.M_NULL
Dim MilSystem As MIL_ID = MIL.M_NULL
Dim MilDisplay As MIL_ID = MIL.M_NULL
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilSubImage As MIL_ID = MIL.M_NULL
Dim MilFontSubImage As MIL_ID = MIL.M_NULL
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim OcrFont As MIL_ID = MIL.M_NULL
Dim OcrResult As MIL_ID = MIL.M_NULL
Dim Score As Double = 0
Dim SizeX As MIL_INT = 0
Dim SizeY As MIL_INT = 0
Dim Type As MIL_INT = 0
Dim ReadString As New StringBuilder(STRING_LENGTH)
Console.Write(Constants.vbLf + "OCR MODULE (SEMI font reading):" + Constants.vbLf)
Console.Write("-------------------------------" + Constants.vbLf + Constants.vbLf)
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufAlloc2d(MilSystem, MIL.MbufDiskInquire(CHAR_IMAGE_FILE, MIL.M_SIZE_X, SizeX), MIL.MbufDiskInquire(CHAR_IMAGE_FILE, MIL.M_SIZE_Y, SizeY) * 3 / 2, MIL.MbufDiskInquire(CHAR_IMAGE_FILE, MIL.M_TYPE, Type), MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MilImage)
MIL.MbufClear(MilImage, 0)
MIL.MbufLoad(CHAR_IMAGE_FILE, MilImage)
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MbufChild2d(MilImage, READ_REGION_POS_X, READ_REGION_POS_Y, READ_REGION_WIDTH, READ_REGION_HEIGHT, MilSubImage)
MIL.MbufChild2d(MilImage, 50, SizeY + 10, SizeX - 100, (SizeY / 3) - 10, MilFontSubImage)
MIL.MocrRestoreFont(FONT_FILE_IN, MIL.M_RESTORE, MilSystem, OcrFont)
MIL.MocrCopyFont(MilFontSubImage, OcrFont, MIL.M_COPY_FROM_FONT + MIL.M_ALL_CHAR, "")
Console.Write("The SEMI string at the top will be read using the font displayed at the bottom." + Constants.vbLf + Constants.vbLf)
Console.Write("Calibrating SEMI font..." + Constants.vbLf + Constants.vbLf)
MIL.MocrCalibrateFont(MilSubImage, OcrFont, STRING_CALIBRATION, CHAR_SIZE_X_MIN, CHAR_SIZE_X_MAX, CHAR_SIZE_X_STEP, CHAR_SIZE_Y_MIN, CHAR_SIZE_Y_MAX, CHAR_SIZE_Y_STEP, MIL.M_DEFAULT)
MIL.MocrSetConstraint(OcrFont, 0, MIL.M_LETTER)
MIL.MocrSetConstraint(OcrFont, 1, MIL.M_DIGIT, "9")
MIL.MocrSetConstraint(OcrFont, 2, MIL.M_DIGIT)
MIL.MocrSetConstraint(OcrFont, 3, MIL.M_DIGIT)
MIL.MocrSetConstraint(OcrFont, 4, MIL.M_DIGIT)
MIL.MocrSetConstraint(OcrFont, 5, MIL.M_DIGIT)
MIL.MocrSetConstraint(OcrFont, 6, MIL.M_DIGIT)
MIL.MocrSetConstraint(OcrFont, 7, MIL.M_DEFAULT, "-")
MIL.MocrSetConstraint(OcrFont, 8, MIL.M_LETTER, "M")
MIL.MocrSetConstraint(OcrFont, 9, MIL.M_LETTER, "X")
MIL.MocrSetConstraint(OcrFont, 10, MIL.M_LETTER, "ABCDEFGH")
MIL.MocrSetConstraint(OcrFont, 11, MIL.M_DIGIT, "01234567")
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MocrAllocResult(MilSystem, MIL.M_DEFAULT, OcrResult)
MIL.MocrReadString(MilSubImage, OcrFont, OcrResult)
MIL.MocrGetResult(OcrResult, MIL.M_STRING, ReadString)
MIL.MocrGetResult(OcrResult, MIL.M_STRING_SCORE, Score)
Console.Write(Constants.vbLf + "The string read is: ""{0}"" (score: {1:P1})." + Constants.vbLf + Constants.vbLf, ReadString.ToString(), Score / 100)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MgraFont(MIL.M_DEFAULT, MIL.M_FONT_DEFAULT_LARGE)
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_YELLOW)
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, READ_REGION_POS_X + (READ_REGION_WIDTH / 4), READ_REGION_POS_Y + READ_REGION_HEIGHT + 50, ReadString.ToString())
If Score > READ_SCORE_MIN Then
MIL.MocrSaveFont(FONT_FILE_OUT, MIL.M_SAVE, OcrFont)
Console.Write("Read successful, calibrated OCR font was saved to disk." + Constants.vbLf)
Else
Console.Write("Error: Read score too low, calibrated OCR font not saved." + Constants.vbLf)
End If
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.MocrFree(OcrFont)
MIL.MocrFree(OcrResult)
MIL.MbufFree(MilSubImage)
MIL.MbufFree(MilFontSubImage)
MIL.MbufFree(MilImage)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
End Class
End Namespace