最近项目上面用到了二维码识别,里面的halcon代码分享部分出来
dev_update_off ()
dev_close_window ()
read_image (Image, 'C:/Users/xq0138/Desktop/f9daf85b2c8df656e8f15f25a05e575.jpg')
* read_image(Image, 'C:/Users/xq0138/Desktop/dmimg01.jpg')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
create_data_code_2d_model ('Data Matrix ECC 200', [], [], DataCodeHandle)
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
dev_disp_text (DecodedDataStrings, 'window', 40, 12, 'black', [], [])
if (|DecodedDataStrings| == 0)
Length := 0
PositionStrings := []
for min_gray := 0 to 30 by 3
for max_gray := 255 to 180 by -7
*灰度线性映射
Mult:=255.0/(max_gray-min_gray)
Add:=-Mult*min_gray
rgb1_to_gray (Image, GrayImage)
scale_image (GrayImage, Image2, Mult, Add)
find_data_code_2d (Image2, SymbolXLDs, DataCodeHandle, 'stop_after_result_num', 4, ResultHandles, PositionStrings)
tuple_strlen (PositionStrings, Length)
*字符串大于5就是识别出来了
if (Length > 5)
break
endif
endfor
if (Length > 5)
break
endif
endfor
* dev_disp_text ('No data code found.\nPlease adjust the parameters.', 'window', 40, 12, 'red', [], [])
dev_display (Image2)
dev_display (SymbolXLDs)
dev_disp_text (PositionStrings, 'window', 40, 12, 'black', [], [])
endif
觉得有可以改进的地方可以私信我互相学习一下。