Skip to content

Workflow for reading bar codesđŸ”—

  1. Create the bar code model.

    You create a bar code model with the operator create_bar_code_model. The returned handle provides all necessary information about the structure of the bar code. In most cases, you do not have to adjust any parameters.

  2. Adjust bar code model.

    In most cases no adaptation of parameters is necessary for reading bar codes with HALCON. If the bar code reading fails, however, you can adjust the following parameters with the operator set_bar_code_param before applying find_bar_code.

    Adjust bar code and element size

    In particular, you can adjust the bar code size and the range of the bar code element’s width with the parameters

    • 'barcode_width_min',
    • 'barcode_width_max',
    • 'barcode_height_min',
    • 'element_size_min', and
    • 'element_size_max'.

    The example program %HALCONEXAMPLES%/solution_guide/basics/barcode_typical_cases.hdev shows how an adaption of the element’s size can improve the decoding.

    Determine check character

    For code types having a check character, you can specify whether the check character is used to check the result or if it is returned as part of the decoded string ('check_char').

    Adapting the thresholds for the extraction of element edges

    Two threshold parameters control the detection of edges within a bounding box. 'meas_thresh' calculates a relative threshold by detecting the highest and lowest gray values within a rough bar code region and using this dynamic range as a basis for edge detection. 'meas_thresh' works well unless images contain lots of noise. It also fails if the code is a stacked code, which, due to its structure, contains a white patch within the bounding box. In both cases the calculated gray-value range of the images is small. This results in an unreasonably small value for 'meas_thresh' and can therefore cause the detection of false edges.

    This, however, is prevented by 'meas_thresh_abs', which sets an absolute threshold of 5.0 for all images. This means that the difference between the brightest and the darkest value of the element border has to be more than 5.0 to be classified as an edge. Therefore, no false edges are detected in images with a small dynamic range.

    Sometimes, a low threshold is needed, e.g., if an image contains parts with a high dynamic range as well as parts with a low one, due to inhomogeneous illumination. Then, 'meas_thresh_abs' must be turned off, by setting it to 0.0 before decreasing the value of 'meas_thresh'.

  3. Read the data code.

    Bar codes are read using the operator find_bar_code. Within this operator, you specify the model and the bar code type that you search for. The operator returns the regions and the decoded strings of all bar codes of the specified type that are found in the image or in the specified region of interest.

Minimal example
read_image (image, 'barcode/ean13/ean1301')
create_bar_code_model ([], [], BarCodeHandle)
find_bar_code (image, SymbolRegions, BarCodeHandle, 'EAN-13', DecodedDataStrings)