using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MCode
{
class Program
{
static void Main(string[] args)
{
const string IMAGE_FILE = MIL.M_IMAGE_PATH + "Code.mim";
const int STRING_LENGTH_MAX = 64;
const int BARCODE_REGION_TOP_LEFT_X = 256;
const int BARCODE_REGION_TOP_LEFT_Y = 80;
const int BARCODE_REGION_SIZE_X = 290;
const int BARCODE_REGION_SIZE_Y = 60;
const int DATAMATRIX_REGION_TOP_LEFT_X = 8;
const int DATAMATRIX_REGION_TOP_LEFT_Y = 312;
const int DATAMATRIX_REGION_SIZE_X = 118;
const int DATAMATRIX_REGION_SIZE_Y = 105;
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 DataMatrixRegion = MIL.M_NULL;
MIL_ID DataMatrixCode = MIL.M_NULL;
MIL_ID BarCodeRegion = MIL.M_NULL;
MIL_ID Barcode = MIL.M_NULL;
MIL_ID CodeResults = MIL.M_NULL;
MIL_INT BarcodeStatus = MIL.M_NULL;
MIL_INT DataMatrixStatus = MIL.M_NULL;
double AnnotationColor = MIL.M_COLOR_GREEN;
double AnnotationBackColor = MIL.M_COLOR_GRAY;
int n = 0;
StringBuilder BarcodeString = new StringBuilder(STRING_LENGTH_MAX);
StringBuilder DataMatrixString = new StringBuilder(STRING_LENGTH_MAX);
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(IMAGE_FILE, 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.McodeAllocResult(MilSystem, MIL.M_DEFAULT, ref CodeResults);
Console.Write("\n1D and 2D CODE READING:\n");
Console.Write("-----------------------\n\n");
Console.Write("This program will decode a linear Code 39 and a DataMatrix code.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufChild2d(MilImage, BARCODE_REGION_TOP_LEFT_X, BARCODE_REGION_TOP_LEFT_Y, BARCODE_REGION_SIZE_X, BARCODE_REGION_SIZE_Y, ref BarCodeRegion);
MIL.McodeAlloc(MilSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, ref Barcode);
MIL.McodeModel(Barcode, MIL.M_ADD, MIL.M_CODE39, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_NULL);
MIL.McodeRead(Barcode, BarCodeRegion, CodeResults);
MIL.McodeGetResult(CodeResults, MIL.M_STATUS + MIL.M_TYPE_MIL_INT, ref BarcodeStatus);
if (BarcodeStatus == MIL.M_STATUS_READ_OK)
{
MIL.McodeGetResult(CodeResults, MIL.M_STRING, BarcodeString);
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor);
MIL.MgraBackColor(MIL.M_DEFAULT, AnnotationBackColor);
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, BARCODE_REGION_TOP_LEFT_X + 10, BARCODE_REGION_TOP_LEFT_Y + 80, " 1D linear 39 bar code: ");
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, BARCODE_REGION_TOP_LEFT_X + 200, BARCODE_REGION_TOP_LEFT_Y + 80, BarcodeString.ToString());
MIL.MgraRect(MIL.M_DEFAULT, MilOverlayImage, BARCODE_REGION_TOP_LEFT_X, BARCODE_REGION_TOP_LEFT_Y, BARCODE_REGION_TOP_LEFT_X + BARCODE_REGION_SIZE_X, BARCODE_REGION_TOP_LEFT_Y + BARCODE_REGION_SIZE_Y);
}
MIL.McodeFree(Barcode);
MIL.MbufFree(BarCodeRegion);
MIL.MbufChild2d(MilImage, DATAMATRIX_REGION_TOP_LEFT_X, DATAMATRIX_REGION_TOP_LEFT_Y, DATAMATRIX_REGION_SIZE_X, DATAMATRIX_REGION_SIZE_Y, ref DataMatrixRegion);
MIL.McodeAlloc(MilSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, ref DataMatrixCode);
MIL.McodeModel(DataMatrixCode, MIL.M_ADD, MIL.M_DATAMATRIX, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_NULL);
MIL.McodeControl(DataMatrixCode, MIL.M_FOREGROUND_VALUE, MIL.M_FOREGROUND_WHITE);
MIL.McodeRead(DataMatrixCode, DataMatrixRegion, CodeResults);
MIL.McodeGetResult(CodeResults, MIL.M_STATUS + MIL.M_TYPE_MIL_INT, ref DataMatrixStatus);
if (DataMatrixStatus == MIL.M_STATUS_READ_OK)
{
MIL.McodeGetResult(CodeResults, MIL.M_STRING, DataMatrixString);
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor);
MIL.MgraBackColor(MIL.M_DEFAULT, AnnotationBackColor);
for (n = 0; n < DataMatrixString.Length; n++)
{
if ((DataMatrixString[n] < '0') || (DataMatrixString[n] > 'Z'))
{
DataMatrixString[n] = ' ';
}
}
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, DATAMATRIX_REGION_TOP_LEFT_X, DATAMATRIX_REGION_TOP_LEFT_Y + 120, " 2D Data Matrix code: ");
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, DATAMATRIX_REGION_TOP_LEFT_X + 200, DATAMATRIX_REGION_TOP_LEFT_Y + 120, DataMatrixString.ToString());
MIL.MgraRect(MIL.M_DEFAULT, MilOverlayImage, DATAMATRIX_REGION_TOP_LEFT_X, DATAMATRIX_REGION_TOP_LEFT_Y, DATAMATRIX_REGION_TOP_LEFT_X + DATAMATRIX_REGION_SIZE_X, DATAMATRIX_REGION_TOP_LEFT_Y + DATAMATRIX_REGION_SIZE_Y);
}
MIL.McodeFree(DataMatrixCode);
MIL.MbufFree(DataMatrixRegion);
MIL.McodeFree(CodeResults);
if ((DataMatrixStatus == MIL.M_STATUS_READ_OK) && (BarcodeStatus == MIL.M_STATUS_READ_OK))
{
Console.Write("Decoding was successful and the strings were written under each code.\n");
}
else
{
Console.Write("Decoding error found.\n");
}
Console.Write("Press <Enter> to end.\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}