using System;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace Mdmr
{
class Program
{
const string IMAGE_FILE_TO_READ = MIL.M_IMAGE_PATH + "ExpiryDateAndLot.mim";
const string FONT_FILE_TO_IMPORT = MIL.M_CONTEXT_PATH + "ExpiryDateAndLotFont5x7.mdmrf";
const double STRING_DOT_DIAMETER = 6.0;
const double STRING_BOX_WIDTH = 400;
const double STRING_BOX_HEIGHT = 100;
static readonly MIL_INT EXPIRY_DATE_LENGTH = 7;
static readonly MIL_INT LOT_NUMBER_LENGTH = 7;
const int TEXT_MAX_SIZE = 128;
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 MilOverlay = MIL.M_NULL;
MIL_ID MilDmrContext = MIL.M_NULL;
MIL_ID MilDmrResult = MIL.M_NULL;
MIL_INT NumberOfStrings = 0;
MIL_INT StringSize = 0;
MIL_INT StringModelIndex = 0;
MIL_INT Index = 0;
StringBuilder StringResult = new StringBuilder(TEXT_MAX_SIZE + 1);
string PrintText;
Console.Write("\nDOT MATRIX READER (SureDotOCR) 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_TO_READ, MilSystem, ref MilImage);
MIL.MdispSelect(MilDisplay, MilImage);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE);
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, ref MilOverlay);
MIL.MdmrAlloc(MilSystem, MIL.M_DOT_MATRIX, MIL.M_DEFAULT, ref MilDmrContext);
MIL.MdmrAllocResult(MilSystem, MIL.M_DOT_MATRIX, MIL.M_DEFAULT, ref MilDmrResult);
MIL.MdmrImportFont(FONT_FILE_TO_IMPORT, MIL.M_DMR_FONT_FILE, MilDmrContext, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_DEFAULT);
MIL.MdmrControl(MilDmrContext, MIL.M_STRING_ADD, MIL.M_DEFAULT);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(0), MIL.M_DEFAULT, MIL.M_STRING_RANK, 1, MIL.M_DEFAULT, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(0), MIL.M_DEFAULT, MIL.M_STRING_SIZE_MIN_MAX, LOT_NUMBER_LENGTH, LOT_NUMBER_LENGTH, MIL.M_NULL);
MIL.MdmrControl(MilDmrContext, MIL.M_STRING_ADD, MIL.M_DEFAULT);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_DEFAULT, MIL.M_STRING_RANK, 0, MIL.M_DEFAULT, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_DEFAULT, MIL.M_STRING_SIZE_MIN_MAX, EXPIRY_DATE_LENGTH, EXPIRY_DATE_LENGTH, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(0), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_DIGITS, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(1), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_DIGITS, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(2), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_LETTERS_UPPERCASE, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(3), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_LETTERS_UPPERCASE, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(4), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_LETTERS_UPPERCASE, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(5), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_DIGITS, MIL.M_NULL);
MIL.MdmrControlStringModel(MilDmrContext, MIL.M_STRING_INDEX(1), MIL.M_POSITION_IN_STRING(6), MIL.M_ADD_PERMITTED_CHARS_ENTRY, MIL.M_FONT_LABEL(MIL.M_ANY), MIL.M_DIGITS, MIL.M_NULL);
Console.Write("A Dot Matrix Reader (SureDotOCR) context was set up to read\n" +
"an expiry date and a lot number from a target image.\n\n");
MIL.MdmrControl(MilDmrContext, MIL.M_DOT_DIAMETER, STRING_DOT_DIAMETER);
MIL.MdmrControl(MilDmrContext, MIL.M_STRING_BOX_WIDTH, STRING_BOX_WIDTH);
MIL.MdmrControl(MilDmrContext, MIL.M_STRING_BOX_HEIGHT, STRING_BOX_HEIGHT);
MIL.MdmrPreprocess(MilDmrContext, MIL.M_DEFAULT);
MIL.MdmrRead(MilDmrContext, MilImage, MilDmrResult, MIL.M_DEFAULT);
MIL.MdmrGetResult(MilDmrResult, MIL.M_GENERAL, MIL.M_DEFAULT, MIL.M_STRING_NUMBER + MIL.M_TYPE_MIL_INT, ref NumberOfStrings);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN);
MIL.MdmrDraw(MIL.M_DEFAULT, MilDmrResult, MilOverlay, MIL.M_DRAW_STRING_CHAR_BOX, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_CYAN);
MIL.MdmrDraw(MIL.M_DEFAULT, MilDmrResult, MilOverlay, MIL.M_DRAW_STRING_CHAR_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT);
if (NumberOfStrings > 0)
{
Console.Write("Result: {0} strings have been read:\n\n", NumberOfStrings);
for (Index = 0; Index < NumberOfStrings; Index++)
{
MIL.MdmrGetResult(MilDmrResult, Index, MIL.M_GENERAL, MIL.M_STRING_MODEL_INDEX + MIL.M_TYPE_MIL_INT, ref StringModelIndex);
MIL.MdmrGetResult(MilDmrResult, Index, MIL.M_GENERAL, MIL.M_STRING + MIL.M_STRING_SIZE + MIL.M_TYPE_MIL_INT, ref StringSize);
MIL.MdmrGetResult(MilDmrResult, Index, MIL.M_GENERAL, MIL.M_STRING, StringResult);
switch ((int)StringModelIndex)
{
case 0:
PrintText = string.Format(" LOT# : {0} ", StringResult);
MIL.MgraText(MIL.M_DEFAULT, MilOverlay, 20, 20 + Index * 20, PrintText);
Console.Write(" LOT# : {0}\n", StringResult);
break;
case 1:
PrintText = string.Format(" EXP. : {0} ", StringResult);
MIL.MgraText(MIL.M_DEFAULT, MilOverlay, 20, 20 + Index * 20, PrintText.ToString());
Console.Write(" EXPIRY DATE: {0}\n", StringResult);
break;
default:
Console.Write("Unexpected model index\n");
break;
}
}
}
else
{
Console.Write("Error: no string found.\n");
}
Console.Write("\nPress <Enter> to end.\n\n");
Console.ReadKey();
MIL.MdmrFree(MilDmrContext);
MIL.MdmrFree(MilDmrResult);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}