using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MStr
{
class Program
{
private const string IMAGE_FILE_DEFINITION = MIL.M_IMAGE_PATH + "QcPlates.mim";
private const string IMAGE_FILE_TO_READ = MIL.M_IMAGE_PATH + "LicPlate.mim";
private const string TEXT_DEFINITION = "AVS300CVK781JNK278 EBX380QKN918HCC839 YRH900ZQR756977AMQ GPK742389EYE569ESQ";
private const int NORMALIZATION_SIZE_Y = 20;
private const int STRING_MAX_SIZE = 32;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilImage = MIL.M_NULL;
MIL_ID MilOverlayImage = MIL.M_NULL;
MIL_ID MilStrContext = MIL.M_NULL;
MIL_ID MilStrResult = MIL.M_NULL;
MIL_INT NumberOfStringRead = 0;
double Score = 0.0;
StringBuilder StringResult = new StringBuilder(STRING_MAX_SIZE + 1);
double Time = 0.0;
Console.Write("\nSTRING READER MODULE:\n");
Console.Write("---------------------\n\n");
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(IMAGE_FILE_DEFINITION, MilSystem, ref MilImage);
MIL.MdispSelect(MilDisplay, MilImage);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE);
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, ref MilOverlayImage);
MIL.MstrAlloc(MilSystem, MIL.M_FONT_BASED, MIL.M_DEFAULT, ref MilStrContext);
MIL.MstrAllocResult(MilSystem, MIL.M_DEFAULT, ref MilStrResult);
MIL.MstrControl(MilStrContext, MIL.M_CONTEXT, MIL.M_ENCODING, MIL.M_UNICODE);
MIL.MstrControl(MilStrContext, MIL.M_CONTEXT, MIL.M_FONT_ADD, MIL.M_USER_DEFINED);
MIL.MstrEditFont(MilStrContext, MIL.M_FONT_INDEX(0), MIL.M_CHAR_ADD, MIL.M_USER_DEFINED + MIL.M_FOREGROUND_BLACK, MilImage, TEXT_DEFINITION);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN);
MIL.MstrDraw(MIL.M_DEFAULT, MilStrContext, MilOverlayImage, MIL.M_DRAW_CHAR, MIL.M_FONT_INDEX(0), MIL.M_ORIGINAL);
MIL.MstrEditFont(MilStrContext, MIL.M_FONT_INDEX(0), MIL.M_CHAR_NORMALIZE, MIL.M_SIZE_Y, NORMALIZATION_SIZE_Y);
MIL.MstrControl(MilStrContext, MIL.M_CONTEXT, MIL.M_STRING_ADD, MIL.M_USER_DEFINED);
MIL.MstrControl(MilStrContext, MIL.M_CONTEXT, MIL.M_STRING_ADD, MIL.M_USER_DEFINED);
MIL.MstrControl(MilStrContext, MIL.M_CONTEXT, MIL.M_STRING_NUMBER, 1);
MIL.MstrControl(MilStrContext, MIL.M_STRING_INDEX(MIL.M_ALL), MIL.M_STRING_SIZE_MIN, 6);
MIL.MstrControl(MilStrContext, MIL.M_STRING_INDEX(MIL.M_ALL), MIL.M_STRING_SIZE_MAX, 6);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(0), MIL.M_DEFAULT, MIL.M_LETTER + MIL.M_UPPERCASE, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(1), MIL.M_DEFAULT, MIL.M_LETTER + MIL.M_UPPERCASE, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(0), 3, MIL.M_DIGIT, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(0), 4, MIL.M_DIGIT, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(0), 5, MIL.M_DIGIT, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(1), 0, MIL.M_DIGIT, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(1), 1, MIL.M_DIGIT, IntPtr.Zero);
MIL.MstrSetConstraint(MilStrContext, MIL.M_STRING_INDEX(1), 2, MIL.M_DIGIT, IntPtr.Zero);
Console.Write("This program has defined a font with this Quebec plates mosaic image.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT);
MIL.MbufLoad(IMAGE_FILE_TO_READ, MilImage);
MIL.MstrPreprocess(MilStrContext, MIL.M_DEFAULT);
MIL.MstrRead(MilStrContext, MilImage, MilStrResult);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
MIL.MstrRead(MilStrContext, MilImage, MilStrResult);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref Time);
MIL.MstrGetResult(MilStrResult, MIL.M_GENERAL, MIL.M_STRING_NUMBER + MIL.M_TYPE_MIL_INT, ref NumberOfStringRead);
if (NumberOfStringRead >= 1)
{
Console.Write("The license plate was read successfully ({0:#.##} ms)\n\n", Time * 1000);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_BLUE);
MIL.MstrDraw(MIL.M_DEFAULT, MilStrResult, MilOverlayImage, MIL.M_DRAW_STRING, MIL.M_ALL, MIL.M_DEFAULT);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN);
MIL.MstrDraw(MIL.M_DEFAULT, MilStrResult, MilOverlayImage, MIL.M_DRAW_STRING_BOX, MIL.M_ALL, MIL.M_DEFAULT);
Console.Write(" String Score\n");
Console.Write(" -----------------------------------\n");
MIL.MstrGetResult(MilStrResult, 0, MIL.M_STRING + MIL.M_TYPE_TEXT_CHAR, StringResult);
MIL.MstrGetResult(MilStrResult, 0, MIL.M_STRING_SCORE, ref Score);
Console.Write(" {0} {1:0.0}\n", StringResult.ToString(), Score);
}
else
{
Console.Write("Error: Plate was not read.\n");
}
Console.Write("\nPress <Enter> to end.\n\n");
Console.ReadKey();
MIL.MstrFree(MilStrContext);
MIL.MstrFree(MilStrResult);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}