1.编写halcon条码识别程序
* 创建一个条码读取模型
create_bar_code_model ([],[], BarCodeHandle)
*执行条形码符号的静区的验证,当启用时,在检测到的条形码序列的左侧或右侧的空白区域内检测到的条纹时,扫描线将被拒绝。
set_bar_code_param (BarCodeHandle,'element_size_variable','true')
* 关闭程序计数器,图新变量更新,图形窗口图像更新
dev_update_off ()
*关闭已经打开的窗口
dev_close_window ()
*打开一个新的窗口
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
CodeTypes := ['EAN-13','Code 39','GS1 DataBar Omnidir','Code 128']
*清除窗口里的内容
dev_clear_window ()
*读取一张图像
read_image (Image, 'E:/2.bmp')
*获取图像大小
get_image_size (Image, Width, Height)
*改变激活窗口的位置和大小
*dev_set_window_extents (-1, -1, Width, Height)
*显示图像
dev_display (Image)
find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
*显示条码解码的时间,条码类型,条码数据
disp_message (WindowHandle, DecodedDataStrings, 'image', -1, -1, 'black', 'true')
*删除条码模版并释放分配的内存
clear_bar_code_model (BarCodeHandle)
2.将halcon条码识别的代码转成C++程序
在VS中配置halcon
并在.h和.cpp文件中添加以下代码
#include "HalconCpp.h"
using namespace HalconCpp;
3.创建如下所示的界面
4.双击条码识别按钮,添加点击事件
在Dlg.h文件中添加halcon转换为C++的初始化代码
HObject ho_Image, ho_SymbolRegions;
// Local control variables
HTuple hv_BarCodeHandle, hv_WindowHandle, hv_CodeTypes;
HTuple hv_Width, hv_Height, hv_DecodedDataStrings;
导入条码识别的程序,将action中的程序复制粘贴到按钮的点击事件中
//创建一个条码读取模型
CreateBarCodeModel(HTuple(), HTuple(), &hv_BarCodeHandle);
//执行条形码符号的静区的验证,当启用时,在检测到的条形码序列的左侧或右侧的空白区域内检测到的条纹时,扫描线将被拒绝。
SetBarCodeParam(hv_BarCodeHandle, "element_size_variable", "true");
//关闭程序计数器,图新变量更新,图形窗口图像更新
dev_update_off();
//关闭已经打开的窗口
if (HDevWindowStack::IsOpen())
{
HalconCpp::CloseWindow(HDevWindowStack::Pop());
}
//打开一个新的窗口
SetWindowAttr("background_color", "black");
//OpenWindow(0, 0, 512, 512, 0, "visible", "", &hv_WindowHandle);
hv_CodeTypes.Clear();
hv_CodeTypes[0] = "EAN-13";
hv_CodeTypes[1] = "Code 39";
hv_CodeTypes[2] = "GS1 DataBar Omnidir";
hv_CodeTypes[3] = "Code 128";
//清除窗口里的内容
if (HDevWindowStack::IsOpen())
ClearWindow(HDevWindowStack::GetActive());
//读取一张图像
ReadImage(&ho_Image, "E:/23.bmp");
//ReadImage(&ho_Image, "E:/1.jpg");
//获取图像大小
GetImageSize(ho_Image, &hv_Width, &hv_Height);
将图像显示到picture control控件上
CRect rect; //设置一块区域
HTuple hv_Exception;
GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect); //获取显示控件的指针
HWND hrect = GetDlgItem(IDC_PIC_STATIC)->m_hWnd; //获取控件的句柄
HTuple Row = (HTuple)rect.top; //获取区域顶端的行坐标
HTuple Column = (HTuple)rect.left; //获取区域左边的列坐标
HTuple Width = (HTuple)rect.Width(); //获取区域的宽度信息
HTuple Height = (HTuple)rect.Height(); //获取区域的高度信息
OpenWindow(Row, Column, Width, Height, (Hlong)hrect, "visible", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
//显示图像
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
进行条码识别
FindBarCode(ho_Image, &ho_SymbolRegions, hv_BarCodeHandle, hv_CodeTypes, &hv_DecodedDataStrings);
判断条码识别是否成功,将成功与否的结果显示到IDC_EDIT2控件中,将识别出的条码显示到IDC_EDIT1中。
HTuple temp = hv_DecodedDataStrings.Length();
if (temp == 0)
{
SetDlgItemText(IDC_EDIT2, TEXT("检测NG"));
isOK = false;
return;
}
else
{
CString m_str = hv_DecodedDataStrings.ToString(); //当识别不出时不会报错
m_str = m_str.Left(m_str.GetLength() - 1); //去掉左边的单引号
m_str = m_str.Right(m_str.GetLength() - 1); //去掉右边的单引号
//CString strBuffer = hv_DecodedDataStrings[0].S(); //当识别不出时会报错
SetDlgItemText(IDC_EDIT1, m_str);
SetDlgItemText(IDC_EDIT2, TEXT("检测OK"));
}
//删除条码模版并释放分配的内存
ClearBarCodeModel(hv_BarCodeHandle);
更改IDC_EDIT2控件中的字符颜色和大小
//CFont *m_Font;
//m_Font = new CFont; //1.创建字体对象
//m_Font->CreateFont(50, 20, 0, 0, 100,
// FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
// CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_SWISS, LPCTSTR("Arial"));//2.配置字体参数 50为字体大小
//CEdit *m_Edit = (CEdit *)GetDlgItem(IDC_EDIT2);
//m_Edit->SetFont(m_Font, FALSE);
//GetDlgItem(IDC_EDIT2)->SetFont(m_Font);//使用字体到编辑框
//CFont font;
//CEdit*pEdt = (CEdit*)GetDlgItem(IDC_EDIT2);
//font.CreatePointFont(100, LPCTSTR( "黑体")); //Arial 设置字体
//pEdt->SetFont(&font);
在OnInitDialog()初始化代码中添加字体设置的代码
m_Brush.CreateSolidBrush(RGB(255,255,255));
CFont *m_Font;
m_Font = new CFont; //1.创建字体对象
m_Font->CreateFont(100, 20, 0, 0, 150,
FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_SWISS, LPCTSTR("Arial"));//2.配置字体参数 100为字体大小
CEdit *m_Edit = (CEdit *)GetDlgItem(IDC_EDIT2);
m_Edit->SetFont(m_Font, FALSE);
GetDlgItem(IDC_EDIT2)->SetFont(m_Font);
此为字体设置的使用版本,添加WM_OnCtlColor消息响应函数,并添加以下代码,当结果不同时,字体的颜色也不同。
HBRUSH CbarcodetestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
if (nCtlColor == CTLCOLOR_EDIT)
{
if (pWnd->GetDlgCtrlID() == IDC_EDIT2 && isOK == true)
{
pDC->SetTextColor(RGB(0, 255, 0)); //OK为绿色
//return (HBRUSH)m_Brush.GetSafeHandle();
}
if (pWnd->GetDlgCtrlID() == IDC_EDIT2 && isOK == false)
{
pDC->SetTextColor(RGB(255, 0, 0)); //NG为红色
//return (HBRUSH)m_Brush.GetSafeHandle();
}
if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
{
pDC->SetTextColor(RGB(0, 0, 0)); // 字符显示为黑色
//return (HBRUSH)m_Brush.GetSafeHandle();
}
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
程序运行如下