此示例演示如何使用运算符decode_bar_code_rectangle2.
通过简单的分割提取条形码区域并将返回的矩形2参数传递给操作者decode_bar_code_rectangle2.
总代码:
* Initialize variables
*初始化变量
Directory := 'barcode/ean13/'
TeaBoxRegExp := '.*tea_box.*'
*
* Initialize settings
*初始化设置
dev_update_window ('off')
dev_set_draw ('margin')
*
* Get list of tea_box*-image files
*获取图像文件列表
list_image_files (Directory, 'default', [], ImageFiles)
ImageFilesTea := regexp_select(ImageFiles,[TeaBoxRegExp,'ignore_case'])
*读取图片
read_image (Image, ImageFilesTea[0])
*
* Prepare graphics window and bar code model
*准备图形窗口和条形码模型
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (2)
create_bar_code_model ([], [], BarCodeHandle)
*
* Decode tea_box*-images
*解码茶盒图像
for ImageIndex := 0 to |ImageFilesTea| - 1 by 1
read_image (Image, ImageFilesTea[ImageIndex])
dev_display (Image)
*
* segment and determine the bar code region
**分段并确定条形码区域
threshold (Image, RegionThresh, 56, 255)
connection (RegionThresh, RegThreshConn)
select_shape_std (RegThreshConn, RegionBC, 'max_area', 100)
*
* create input parameters for decode_bar_code_rectangle2
*为创建decode_bar_code_rectangle2输入参数
smallest_rectangle2 (RegionBC, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle2BC, Row, Column, Phi, Length1, Length2)
dev_set_color ('magenta')
dev_display (Rectangle2BC)
*
* decode_bar_code_rectangle2 directly uses the passed rectangle2 parameters as ROI
* decode_bar_code_rectangle2 直接使用传递的矩形2参数作为ROI
decode_bar_code_rectangle2 (Image, BarCodeHandle, 'EAN-13', Row, Column, Phi, Length1, Length2, Decoded)
get_bar_code_object (SymbolRegions, BarCodeHandle, 'all', 'symbol_regions')
dev_set_color ('lime green')
dev_display (SymbolRegions)
disp_message (WindowHandle, 'Found bar code: ' + Decoded, 'window', 5, 5, 'black', 'true')
if (ImageIndex < |ImageFilesTea| - 1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
clear_bar_code_model (BarCodeHandle)
*
逐段分析:
*初始化变量
Directory := 'barcode/ean13/'
TeaBoxRegExp := '.*tea_box.*'
*初始化设置
dev_update_window ('off')
dev_set_draw ('margin')
*获取图像文件列表
list_image_files (Directory, 'default', [], ImageFiles)
ImageFilesTea := regexp_select(ImageFiles,[TeaBoxRegExp,'ignore_case'])
*读取图片
read_image (Image, ImageFilesTea[0])
*准备图形窗口和条形码模型
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (2)
*创建条形码模型句柄
create_bar_code_model ([], [], BarCodeHandle)
*解码图像
for ImageIndex := 0 to |ImageFilesTea| - 1 by 1
*读取图片
read_image (Image, ImageFilesTea[ImageIndex])
dev_display (Image)
*分段并确定条形码区域
*阈值分割
threshold (Image, RegionThresh, 56, 255)
*联通区域
connection (RegionThresh, RegThreshConn)
*根绝最大面积特征选择
select_shape_std (RegThreshConn, RegionBC, 'max_area', 100)
*求最小外接矩形
smallest_rectangle2 (RegionBC, Row, Column, Phi, Length1, Length2)
*生成外接矩形
gen_rectangle2 (Rectangle2BC, Row, Column, Phi, Length1, Length2)
*设置颜色
dev_set_color ('magenta')
*显示矩形
dev_display (Rectangle2BC)
* decode_bar_code_rectangle2 直接使用传递的矩形2参数作为ROI
*解码矩形内的条形码符号。
decode_bar_code_rectangle2 (Image, BarCodeHandle, 'EAN-13', Row, Column, Phi, Length1, Length2, Decoded)
*获得条形码的区域
get_bar_code_object (SymbolRegions, BarCodeHandle, 'all', 'symbol_regions')
dev_set_color ('lime green')
dev_display (SymbolRegions)
*显示信息
disp_message (WindowHandle, 'Found bar code: ' + Decoded, 'window', 5, 5, 'black', 'true')
*判断条形码是否循环完
if (ImageIndex < |ImageFilesTea| - 1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
*释放条形码阅读器分配的内存
clear_bar_code_model (BarCodeHandle)
主要算子解释:
decode_bar_code_rectangle2(Image : : BarCodeHandle, CodeType, Row, Column, Phi, Length1, Length2 : DecodedDataStrings)
功能:解码矩形内的条形码符号。直接扫描提供的区域,以查找条形码。
BarCodeHandle:条形码模型的句柄
CodeType:条形码模型的句柄
Row:中心的行索引
Column:中心的列索引
Phi:矩形的弧度方向
Length1:沿条形码读取方向的矩形长度的一半
Length2 :垂直于条形码读取方向的矩形长度的一半
DecodedDataStrings:所有成功解码条形码的数据字符串