c# + halcon 二维码识别

使用c#结合halcon实现对二维码的识别。
ho_Image:包含二维码的halcon图像

private string action(HObject ho_Image)
{
    HObject ho_SymbolXLDs;
    HTuple hv_DataCodeHandle = new HTuple(), hv_ResultHandles = new HTuple();
    HTuple hv_DecodedDataStrings = new HTuple();
    // Initialize local and output iconic variables 
    //HOperatorSet.GenEmptyObj(out ho_Image);
    HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
    //ho_Image.Dispose();
    //HOperatorSet.ReadImage(out ho_Image, img_path);
    hv_DataCodeHandle.Dispose();
    HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
    HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "light_on_dark");
    ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose();
    HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, "train",
        "all", out hv_ResultHandles, out hv_DecodedDataStrings);
    ho_Image.Dispose();
    ho_SymbolXLDs.Dispose();
    hv_DataCodeHandle.Dispose();
    hv_ResultHandles.Dispose();

    DateTimeOffset currentTime = DateTimeOffset.Now;
    string time_now_ymd = currentTime.Year.ToString() + "_" + currentTime.Month.ToString() + "_" + currentTime.Day.ToString();
    string time_now_hms = currentTime.Hour.ToString() + ":" + currentTime.Minute.ToString() + ":" + currentTime.Second.ToString();
    string time_now = time_now_ymd + " " + time_now_hms;

    try
    {
        return hv_DecodedDataStrings;

    }
    catch (Exception ex)
    {
        return time_now;
    }

    hv_DecodedDataStrings.Dispose();

}

使用范例:结合海康相机取图,Bitmap类型的图片。

Bitmap image = getBitmapFromByteStream(data, stConvertPixelParam.nHeight, stConvertPixelParam.nWidth, 3);

DateTimeOffset currentTime = DateTimeOffset.Now;
string time_now_ymd = currentTime.Year.ToString() + "_" + currentTime.Month.ToString() + "_" + currentTime.Day.ToString();
string time_now_hms = currentTime.Hour.ToString() + "_" + currentTime.Minute.ToString() + "_" + currentTime.Second.ToString();
string time_now = time_now_ymd + "_" + time_now_hms;

//此处保存二维码图片  图中包含上下部分两个二维码
image.Save(System.Windows.Forms.Application.StartupPath + "img\\" + time_now + ".bmp");

byte[] bt = getByteStreamFromBitmap(image.Width, image.Height, 3, image);
Bitmap bt1 = image.Clone(new System.Drawing.Rectangle(0, 0, image.Width, image.Height / 2), image.PixelFormat);
Bitmap bt2 = image.Clone(new System.Drawing.Rectangle(0, image.Height / 2, image.Width, image.Height / 2), image.PixelFormat);
HObject ho;
HOperatorSet.GenEmptyObj(out ho);
Bitmap2HObjectBpp24(bt1, out ho);

HObject ho2;
HOperatorSet.GenEmptyObj(out ho2);
Bitmap2HObjectBpp24(bt2, out ho2);

//扫码成功获得结果,失败获得时间戳
string str_L = action(ho);
string str_R = action(ho2);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛奶奥利奥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值