| Customize Help

Automatically detecting the code type

You can automatically detect the code type, encoding scheme, and position of most 1D code occurrences in an image, using McodeDetect(). You can use this information to automatically or manually set up your code models with an appropriate code type and encoding scheme for the code occurrences in your images. Detecting the code type is especially useful when it is unknown because you need this information to add a code model. In addition, being specific about the encoding scheme, rather than M_ANY, can accelerate McodeRead() and McodeGrade() operations. You can use the positional results for graphical and debugging purposes.

McodeDetect() supports all 1D code types, except postal, GS1 Databar, and Pharmacode code types. It cannot detect 2D or composite code types.

Note that some code types are a subset of another. If a code occurrence has only the common features of these code types, McodeDetect() cannot distinguish if the code occurrence is of one type or the other at decode-time. In these cases, McodeDetect() reports the following:

When the following code types cannot be differentiated

Code type reported

M_UPC_A and M_EAN13


M_EAN14, M_GS1_128, and M_CODE128


M_GS1_128 and M_CODE128


You can remove this ambiguity by restricting the possible code types when calling McodeDetect(). Another reason to restrict the possible code types is if you are not interested in detecting certain code types; this will accelerate McodeDetect(). Note that McodeDetect() does not take a code context. All information must be passed directly to the function.

When you call McodeDetect(), you must specify the total number of code occurrences to find in the specified image, regardless of their code type. The function detects the specified number of code occurrences with the best quality. If it detects less than the specified number, McodeGetResult() with M_STATUS returns M_STATUS_DETECT_FAILED.

To automatically add code models to a code context using the results of McodeDetect(), use McodeModel() with M_RESET_FROM_DETECTED_RESULTS. This first deletes all existing code models in the code context, and then adds a code model for each detected code type. If two code occurrences with the same code type but different encoding schemes are detected, only one code model of this type is added and its encoding scheme is set to M_ANY (if supported by the code type).

The following example shows you how to automatically add code models to a code context using the results of McodeDetect(). It also shows you how to annotate a displayed image with the location and name of the detected code occurrences.

If you don't want to delete the existing models in the context, you can manually add a code model to a code context using the results of McodeDetect(). To do so, perform the following:

  1. Retrieve the code type and encoding scheme of a detected code occurrence using McodeGetResult(), first with M_CODE_TYPE and then with M_ENCODING, respectively.

  2. Call McodeModel() with M_ADD, the code context, and the code type; this adds a code model of the specified code type to the context. Note that McodeDetect() can find multiple occurrences of the same code type and encoding scheme. Instead of adding multiple code models of the same code type and encoding scheme, increment the number of occurrences to find of that code model, using McodeControl() with the code model index and M_NUMBER.

  3. Set the encoding scheme of the newly added code model using McodeControl() with M_ENCODING.

Repeat the process above for all occurrences detected. Note that you can also use McodeGetResult() with M_ALL to retrieve the above mentioned results; this retrieves the specified result for all code occurrences at once.