Halcon联合C#对二维码解码的信息读取错误

using System;
using System.Threading;
using System.Windows.Forms;
using HalconDotNet;

namespace CSharp_Theard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        // Local iconic variables 

        HObject ho_Image, ho_SymbolXLDs = null;

        // Local control variables 

        HTuple hv_DataCodeHandle = null, hv_AcqHandle = null;
        HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple();
        HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();

        private void Button1_Click(object sender, EventArgs e)
        {
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
            State();
        }



        private void State()
        {
            Thread t = new Thread(new ThreadStart(this.DoSomething));
            t.Start();
        }
        private void DoSomething()
        {
                HOperatorSet.SetSystem("filename_encoding", "utf8");
                //dev_close_window(...);
                HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
                //Image Acquisition 01: Code generated by Image Acquisition 01
                HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8,
                    "rgb", -1, "false", "default", "[1] XW500", 0, -1, out hv_AcqHandle);
                ho_Image.Dispose();
                HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //dev_open_window(...);
                HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                while ((int)(1) != 0)
                {
                    ho_Image.Dispose();
                    HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                    //Image Acquisition 01: Do something
                    ho_SymbolXLDs.Dispose();
                    HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
                        new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
                    MessageBox.Show(hv_DecodedDataStrings);
                }

需要识别的二维码

C#的识别结果

Halcon程序

set_system ('filename_encoding', 'utf8')
dev_close_window ()
create_data_code_2d_model ('QR Code', [], [], DataCodeHandle)
* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[1] XW500', 0, -1, AcqHandle)
grab_image (Image, AcqHandle)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
dev_display (Image)
while (true)
    grab_image (Image, AcqHandle)
    * Image Acquisition 01: Do something
    find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
    disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')
endwhile
close_framegrabber (AcqHandle)

Halcon的识别结果

'PN:PESDU0511P1 Package:DFN1006-2\tQty:10000pcs  Date code:2051 Lot No:PNS20200903001B02'

 

想问各位大佬们,为什么会出现这样的问题呀,该如何解决呢?

万分感谢!!!

  // Main procedure 
  private void action()
  {


    // Local iconic variables 

    HObject ho_Image, ho_SymbolXLDs=null;

    // Local control variables 

    HTuple hv_DataCodeHandle = null, hv_AcqHandle = null;
    HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple();
    HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
    try
    {
      HOperatorSet.SetSystem("filename_encoding", "utf8");
      //dev_close_window(...);
      HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
      //Image Acquisition 01: Code generated by Image Acquisition 01
      HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, 
          "rgb", -1, "false", "default", "[1] XW500", 0, -1, out hv_AcqHandle);
      ho_Image.Dispose();
      HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
      HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
      //dev_open_window(...);
      HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
      while ((int)(1) != 0)
      {
        ho_Image.Dispose();
        HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
        //Image Acquisition 01: Do something
        ho_SymbolXLDs.Dispose();
        HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, 
            new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
        disp_message(hv_ExpDefaultWinHandle, hv_DecodedDataStrings, "window", 12, 
            12, "black", "true");
      }
      HOperatorSet.CloseFramegrabber(hv_AcqHandle);
    }
    catch (HalconException HDevExpDefaultException)
    {
      ho_Image.Dispose();
      ho_SymbolXLDs.Dispose();

      throw HDevExpDefaultException;
    }
    ho_Image.Dispose();
    ho_SymbolXLDs.Dispose();

  }

这是Halcon导出后的action函数,因为在vs中运行时catch中的 throw HDevExpDefaulException; 会报如下图的错误。

所以将try语句去掉了,之后便可以正常运行,只是信息读取会出下错误。

第一个问题:https://blog.csdn.net/weixin_34370347/article/details/94078024,简单来说就是编码的转换。

第二个问题,你把详细信息看下,至少你要定位到是哪句代码报错

set_data_code_2d_param (DataCodeHandle, 'string_encoding', 'utf8')

Halcon中加一句上面的代码试试。这个码里面有中文的冒号,因此会有乱码。