使用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);