/**************************************************************************************/ /* * File name: MCode.c * * Synopsis: This program decodes a 1D Code 39 linear Barcode and a 2D DataMatrix code. */ #include #include /* Target image character specifications. */ #define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Code.mim") /* Regions around 1D code. */ #define BARCODE_REGION_TOP_LEFT_X 256L #define BARCODE_REGION_TOP_LEFT_Y 80L #define BARCODE_REGION_SIZE_X 290L #define BARCODE_REGION_SIZE_Y 60L /* Regions around 2D code. */ #define DATAMATRIX_REGION_TOP_LEFT_X 8L #define DATAMATRIX_REGION_TOP_LEFT_Y 312L #define DATAMATRIX_REGION_SIZE_X 118L #define DATAMATRIX_REGION_SIZE_Y 105L /* Maximum length of the string to read. */ #define STRING_LENGTH_MAX 64L void main(void) { MIL_ID MilApplication, /* Application identifier. */ MilSystem, /* System identifier. */ MilDisplay, /* Display identifier. */ MilImage, /* Image buffer identifier. */ MilOverlayImage, /* Image buffer identifier. */ DataMatrixRegion, /* Child containing DataMatrix. */ DataMatrixCode, /* DataMatrix 2D code identifier. */ BarCodeRegion, /* Child containing Code39 */ Barcode; /* Code39 barcode identifier. */ long BarcodeStatus; /* Decoding status. */ long DataMatrixStatus; /* Decoding status. */ long AnnotationColor = M_COLOR_GREEN, AnnotationBackColor = M_COLOR_GRAY, n; MIL_TEXT_CHAR OutputString [STRING_LENGTH_MAX]; /* Array of characters to draw. */ MIL_TEXT_CHAR BarcodeString [STRING_LENGTH_MAX]; /* Array of characters read. */ MIL_TEXT_CHAR DataMatrixString[STRING_LENGTH_MAX]; /* Array of characters read. */ /* Allocate defaults. */ MappAllocDefault(M_SETUP, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); /* Restore source image into an automatically allocated image buffer. */ MbufRestore(IMAGE_FILE, MilSystem, &MilImage); /* Display the image buffer. */ MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotations. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Pause to show the original image. */ printf("\n1D and 2D CODE READING:\n"); printf("-----------------------\n\n"); printf("This program will decode a linear Code 39 and a DataMatrix code.\n"); printf("Press to continue.\n\n"); getch(); /* 1D BARCODE READING: */ /* Create a read region around the code to speedup reading. */ MbufChild2d(MilImage, BARCODE_REGION_TOP_LEFT_X, BARCODE_REGION_TOP_LEFT_Y, BARCODE_REGION_SIZE_X, BARCODE_REGION_SIZE_Y, &BarCodeRegion); /* Allocate CODE objects. */ McodeAlloc(MilSystem, M_CODE39, M_DEFAULT, &Barcode); /* Read codes from image. */ McodeRead(Barcode, BarCodeRegion, M_DEFAULT); /* Get decoding status. */ McodeGetResult(Barcode, M_STATUS+M_TYPE_LONG, &BarcodeStatus); /* Check if decoding was successful. */ if (BarcodeStatus == M_STATUS_OK) { /* Get decoded string. */ McodeGetResult(Barcode, M_STRING, BarcodeString); /* Draw the decoded strings and read region in the overlay image. */ MgraColor(M_DEFAULT, AnnotationColor); MgraBackColor(M_DEFAULT, AnnotationBackColor); MOs_sprintf(OutputString, MIL_TEXT("\"%s\""), BarcodeString); MgraText(M_DEFAULT, MilOverlayImage, BARCODE_REGION_TOP_LEFT_X+10, BARCODE_REGION_TOP_LEFT_Y+80, MIL_TEXT(" 1D linear 39 bar code: ")); MgraText(M_DEFAULT, MilOverlayImage, BARCODE_REGION_TOP_LEFT_X+200, BARCODE_REGION_TOP_LEFT_Y+80, OutputString); MgraRect(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); } /* Free objects. */ McodeFree(Barcode); MbufFree(BarCodeRegion); /* 2D CODE READING: */ /* Create a read region around the code to speedup reading. */ MbufChild2d(MilImage, DATAMATRIX_REGION_TOP_LEFT_X, DATAMATRIX_REGION_TOP_LEFT_Y, DATAMATRIX_REGION_SIZE_X, DATAMATRIX_REGION_SIZE_Y, &DataMatrixRegion); /* Allocate CODE objects. */ McodeAlloc(MilSystem, M_DATAMATRIX, M_DEFAULT, &DataMatrixCode); /* Set the foreground value for the DataMatrix since it is different from the default value. */ McodeControl(DataMatrixCode, M_FOREGROUND_VALUE, M_FOREGROUND_WHITE); /* Read codes from image. */ McodeRead(DataMatrixCode, DataMatrixRegion, M_DEFAULT); /* Get decoding status. */ McodeGetResult(DataMatrixCode, M_STATUS + M_TYPE_LONG, &DataMatrixStatus); /* Check if decoding was successful. */ if (DataMatrixStatus == M_STATUS_OK) { /* Get decoded string. */ McodeGetResult(DataMatrixCode, M_STRING, DataMatrixString); /* Draw the decoded strings and read region in the overlay image. */ MgraColor(M_DEFAULT, AnnotationColor); MgraBackColor(M_DEFAULT, AnnotationBackColor); for (n=0; DataMatrixString[n] != '\0'; n++) /* Replace non printable characters with space.*/ if ((DataMatrixString[n] < '0') || (DataMatrixString[n] > 'Z')) DataMatrixString[n] = ' '; MOs_sprintf(OutputString, MIL_TEXT("\"%s\" "), DataMatrixString); MgraText(M_DEFAULT, MilOverlayImage, DATAMATRIX_REGION_TOP_LEFT_X, DATAMATRIX_REGION_TOP_LEFT_Y+120, MIL_TEXT(" 2D Data Matrix code: ")); MgraText(M_DEFAULT, MilOverlayImage, DATAMATRIX_REGION_TOP_LEFT_X+200, DATAMATRIX_REGION_TOP_LEFT_Y+120, OutputString); MgraRect(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); } /* Free objects. */ McodeFree(DataMatrixCode); MbufFree(DataMatrixRegion); /* Pause to show the results. */ if ( (DataMatrixStatus == M_STATUS_OK) && (BarcodeStatus == M_STATUS_OK) ) printf("Decoding was successful and the strings were written under each code.\n"); else printf("Decoding error found.\n"); printf("Press to end.\n\n"); getch(); /* Free other allocations. */ MbufFree(MilImage); MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); }