这个是HALCON10.0的代码,你可以参考下:
- * Example program for the usage of the bar code
- * reader autodiscrimination feature of HALCON.
- *
- * Create bar code reader model
- create_bar_code_model ([], [], BarCodeHandle)
- *
- * Initialization
- dev_update_var ('off')
- dev_update_pc ('off')
- dev_update_window ('off')
- dev_close_window ()
- dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
- set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
- dev_set_draw ('margin')
- dev_set_line_width (3)
- dev_set_color ('forest green')
- *
- * Define the example images to use
- ExampleImagesAny := ['barcode/ean13/ean1301', 'barcode/code39/code3901', 'barcode/rss14/rss14_01']
- ExampleImagesAmbiguous := ['barcode/mixed/barcode_mixed_01']
- ExampleImagesMixed := ['barcode/mixed/barcode_mixed_04', 'barcode/mixed/barcode_mixed_03', 'barcode/mixed/barcode_mixed_01']
- *
- * Use autodiscrimination to decode any of the bar code types
- * supported by HALCON (except PharmaCode) or determine the bar
- * code type of unknown bar codes
- for IdxExample := 0 to 1 by 1
- dev_clear_window ()
- if (IdxExample = 0)
- CodeTypes := 'auto'
- Message := 'Usage of autodiscrimination to decode'
- Message[1] := 'unknown bar code types.'
- Message[2] := 'In the first run we use CodeType=\''+CodeTypes+'\' to look'
- Message[3] := 'for all bar code types known to HALCON'
- disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- else
- CodeTypes := ['EAN-13','Code 39','RSS-14']
- Message := 'In the second run we look for the expected types only:'
- Message[1] := sum(' ' + CodeTypes)
- Message[2] := 'This reduces the decoding time and'
- Message[3] := 'increases the decoding reliability.'
- disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- endif
- for IdxIma := 0 to |ExampleImagesAny|-1 by 1
- FileName := ExampleImagesAny[IdxIma]
- read_image (Image, FileName)
- *
- * Set display defaults
- get_image_size (Image, Width, Height)
- dev_set_window_extents (-1, -1, Width, Height)
- dev_display (Image)
- disp_message (WindowHandle, ['Looking for bar code(s) of type:', sum(' ' + CodeTypes)], 'window', 12, 12, 'black', 'true')
- *
- * Find and decode bar codes. Measure the time needed.
- count_seconds (Start)
- find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
- count_seconds (Stop)
- Duration := (Stop - Start) * 1000
- *
- * Display results and the time needed
- dev_display (SymbolRegions)
- get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
- disp_message (WindowHandle, ['Found bar code(s) in ' + Duration
- 3.0f' + 'ms:',' Type: ' + DecodedDataTypes + '\n Data: ' + DecodedDataStrings], 'window', 5*12, 12, 'forest green', 'true')
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- endfor
- endfor
- *
- *
- * Autodiscrimination can be used to decode multiple bar code
- * types within one image at once.
- *
- * First we demonstrate that the bar code types list
- * should be confined whenever possible.
- *
- * Display information for the user:
- Message := 'If you have a list of bar code types that'
- Message[1] := 'you want to decode, you should supply only those'
- Message[2] := 'types to the bar code reader. In the following,'
- Message[3] := 'it is demonstrated that otherwise a wrong bar code'
- Message[4] := 'could be decoded (here: UPC-E Add-On 2)'
- dev_clear_window ()
- disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- *
- * For demonstration purposes we scan here, among others,
- * for UPC codes, too.
- CodeTypes := ['auto']
- *
- * Demonstrate autodiscrimination for mixed bar code types
- for IdxIma := 0 to |ExampleImagesAmbiguous|-1 by 1
- FileName := ExampleImagesAmbiguous[IdxIma]
- read_image (Image, FileName)
- *
- * Display image and description
- get_image_size (Image, Width, Height)
- dev_set_window_extents (-1, -1, Width/2, Height/2)
- dev_display (Image)
- disp_message (WindowHandle, ['Looking for bar code(s) of type:', sum(' ' + CodeTypes)], 'window', 12, 12, 'black', 'true')
- *
- * Decode mixed bar codes
- find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
- *
- * Display decoded data and symbol region
- get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
- area_center (SymbolRegions, Area, Rows, Columns)
- for IdxResult := 0 to |DecodedDataStrings|-1 by 1
- if (regexp_test (DecodedDataTypes[IdxResult], 'UPC.*') # 0)
- Color := 'red'
- else
- Color := 'forest green'
- endif
- dev_set_color (Color)
- select_obj (SymbolRegions, SelectedRegion, IdxResult+1)
- dev_display (SelectedRegion)
- disp_message (WindowHandle, DecodedDataTypes[IdxResult]+': '+DecodedDataStrings[IdxResult], 'image', Rows[IdxResult], Columns[IdxResult]-160, Color, 'true')
- endfor
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- endfor
- *
- *
- * Now we decode multiple bar code types correctly
- *
- MixedScanAllExceptRSS_UPC := false
- if (MixedScanAllExceptRSS_UPC)
- *
- * We could scan for all bar codes except certain bar code
- * families...
- get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
- NoRSS := '~'+regexp_select (AllCodeTypes, 'RSS.*')
- NoUPC := '~'+regexp_select (AllCodeTypes, 'UPC.*')
- CodeTypes := ['auto', NoRSS, NoUPC]
- CodeTypesDescription := 'auto, ~RSS*, ~UPC*'
- else
- *
- * ...or (as we do here) scan only for the EAN family and
- * Code 39
- get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
- AllEAN := regexp_select (AllCodeTypes, 'EAN.*')
- CodeTypes := [AllEAN, 'Code 39']
- CodeTypesDescription := 'EAN-13*, EAN-8*, Code 39'
- endif
- *
- * Display information for the user
- Message := 'Now we demonstrate the usage of'
- Message[1] := 'autodiscrimination to decode multiple'
- Message[2] := 'mixed bar code types within one image.'
- dev_clear_window ()
- disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- *
- * Demonstrate autodiscrimination for mixed bar code types
- for IdxIma := 0 to |ExampleImagesMixed|-1 by 1
- FileName := ExampleImagesMixed[IdxIma]
- read_image (Image, FileName)
- *
- * Display image and description
- get_image_size (Image, Width, Height)
- dev_set_window_extents (-1, -1, Width/2, Height/2)
- dev_display (Image)
- disp_message (WindowHandle, ['Looking for bar code(s) of type:', ' ' + CodeTypesDescription], 'window', 12, 12, 'black', 'true')
- *
- * Decode mixed bar codes
- find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
- *
- * Display decoded data and symbol region
- dev_set_color ('forest green')
- dev_display (SymbolRegions)
- get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
- area_center (SymbolRegions, Area, Rows, Columns)
- for IdxResult := 0 to |DecodedDataStrings|-1 by 1
- disp_message (WindowHandle, DecodedDataTypes[IdxResult]+': '+DecodedDataStrings[IdxResult], 'image', Rows[IdxResult], Columns[IdxResult]-160, 'forest green', 'true')
- endfor
- if (IdxIma # |ExampleImagesMixed|-1)
- disp_continue_message (WindowHandle, 'black', 'true')
- stop ()
- endif
- endfor
- *
- * Close the bar code reader
- clear_bar_code_model (BarCodeHandle)
复制代码
|