HDevelopExport.cs 13.9 KB
//
//  File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0
//
//  This file is intended to be used with the HDevelopTemplate or
//  HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using HalconDotNet;
using OnlineStore.Common;

public partial class HDevelopExport
{
    public HTuple hv_ExpDefaultWinHandle;

    // Procedures 
    // Chapter: Graphics / Text
    // Short Description: This procedure writes a text message. 
    public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
        HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
    { 
        HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
        HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
        HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
        HTuple hv_WidthWin = new HTuple(), hv_HeightWin = null;
        HTuple hv_MaxAscent = null, hv_MaxDescent = null, hv_MaxWidth = null;
        HTuple hv_MaxHeight = null, hv_R1 = new HTuple(), hv_C1 = new HTuple();
        HTuple hv_FactorRow = new HTuple(), hv_FactorColumn = new HTuple();
        HTuple hv_UseShadow = null, hv_ShadowColor = null, hv_Exception = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
        HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
        HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
        HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
        HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
        HTuple hv_CurrentColor = new HTuple();
        HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
        HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
        HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
        HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
        HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
 
        //Prepare window
        HOperatorSet.GetRgb(hv_ExpDefaultWinHandle, out hv_Red, out hv_Green, out hv_Blue);
        HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out hv_Row1Part, out hv_Column1Part,
            out hv_Row2Part, out hv_Column2Part);
        HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_RowWin, out hv_ColumnWin,
            out hv_WidthWin, out hv_HeightWin);
        HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
        //
        //default settings
        if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
        {
            hv_Row_COPY_INP_TMP = 12;
        }
        if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
        {
            hv_Column_COPY_INP_TMP = 12;
        }
        if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
        {
            hv_Color_COPY_INP_TMP = "";
        }
        //
        hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
        //
        //Estimate extentions of text depending on font size.
        HOperatorSet.GetFontExtents(hv_ExpDefaultWinHandle, out hv_MaxAscent, out hv_MaxDescent,
            out hv_MaxWidth, out hv_MaxHeight);
        if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
        {
            hv_R1 = hv_Row_COPY_INP_TMP.Clone();
            hv_C1 = hv_Column_COPY_INP_TMP.Clone();
        }
        else
        {
            //Transform image to window coordinates
            hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
            hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
            hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
            hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
        }
        //
        //Display text box depending on text size
        hv_UseShadow = 1;
        hv_ShadowColor = "gray";
        if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
        {
            if (hv_Box_COPY_INP_TMP == null)
                hv_Box_COPY_INP_TMP = new HTuple();
            hv_Box_COPY_INP_TMP[0] = "#fce9d4";
            hv_ShadowColor = "#f28d26";
        }
        if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
            1))) != 0)
        {
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
            {
                //Use default ShadowColor set above
            }
            else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                "false"))) != 0)
            {
                hv_UseShadow = 0;
            }
            else
            {
                hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                        1));
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
                    throw new HalconException(hv_Exception);
                }
            }
        }
        if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
        {
            //Valid color?
            try
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                    0));
            }
            // catch (Exception) 
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
                throw new HalconException(hv_Exception);
            }
            //Calculate box extents
            hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
            hv_Width = new HTuple();
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                )) - 1); hv_Index = (int)hv_Index + 1)
            {
                HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                    hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                hv_Width = hv_Width.TupleConcat(hv_W);
            }
            hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                ));
            hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
            hv_R2 = hv_R1 + hv_FrameHeight;
            hv_C2 = hv_C1 + hv_FrameWidth;
            //Display rectangles
            HOperatorSet.GetDraw(hv_ExpDefaultWinHandle, out hv_DrawMode);
            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");
            //Set shadow color
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_ShadowColor);
            if ((int)(hv_UseShadow) != 0)
            {
                HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
                    hv_C2 + 1);
            }
            //Set box color
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                0));
            HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1, hv_C1, hv_R2, hv_C2);
            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, hv_DrawMode);
        }
        //Write text.
        for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
            )) - 1); hv_Index = (int)hv_Index + 1)
        {
            hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                )));
            if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                "auto")))) != 0)
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_CurrentColor);
            }
            else
            {
                HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
            }
            hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
            HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_Row_COPY_INP_TMP, hv_C1);
            HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                hv_Index));
        }
        //Reset changed window settings
        HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
        HOperatorSet.SetPart(hv_ExpDefaultWinHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
            hv_Column2Part);

        return;
    }

    // Main procedure 
    private void action(HObject ho_Image)
    { 
        HObject ho_SymbolXLDs, ho_SymbolXLD = null; 
        HTuple hv_WindowHandle = new HTuple(), hv_DataCodeHandle = null;
        HTuple hv_ResultHandles = null, hv_DecodedDataStrings = null;
        HTuple hv_i = null, hv_Row = new HTuple(), hv_Col = new HTuple();
        HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
        HTuple hv_TxtWidth = new HTuple(), hv_TxtHeight = new HTuple();
 
        HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
        HOperatorSet.GenEmptyObj(out ho_SymbolXLD);
        try
        { 
            HOperatorSet.SetSystem("filename_encoding", "utf8");
             
            HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", "default_parameters", "enhanced_recognition", out hv_DataCodeHandle); 
            HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "dark_on_light"); 

            ho_SymbolXLDs.Dispose();
            HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
                new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
             
            //检测和读取二维代码符号,也支持读取二维数据模型的序列,参数详见这个算子
            for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_ResultHandles.TupleLength())) - 1); hv_i = (int)hv_i + 1)
            {
                ho_SymbolXLD.Dispose();
                HOperatorSet.SelectObj(ho_SymbolXLDs, out ho_SymbolXLD, hv_i + 1);
                HOperatorSet.GetContourXld(ho_SymbolXLD, out hv_Row, out hv_Col);
                HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect(
                    hv_i), out hv_Ascent, out hv_Descent, out hv_TxtWidth, out hv_TxtHeight);
                disp_message(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect(hv_i),
                    "image", ((hv_Row - 50)).TupleMax(), (((((((hv_Col + 30)).TupleMin()) - (hv_TxtWidth / 2))).TupleConcat(
                    1))).TupleMax(), "black", "true");
            } 
            //Clear the 2d data code model
            HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
            string[] resultList = hv_DecodedDataStrings.SArr;
            result = new List<string>(resultList);
            foreach (string res in resultList)
            {
                result.Add(res);
            }
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_SymbolXLDs.Dispose();
            ho_SymbolXLD.Dispose();
             
        }
        ho_Image.Dispose();
        ho_SymbolXLDs.Dispose();
        ho_SymbolXLD.Dispose();

    }
    public List<string> result = new List<string>( ); 

    public void RunHalcon(HTuple Window, HObject ho_Image,string codeType, int count)
    {
        hv_ExpDefaultWinHandle = Window; 
        string[] array = GetQrCode(ho_Image,codeType,count);
        result = new List<string>(array);
       
    }
    public void RunHalcon(HTuple Window, string filePath)
    {
        hv_ExpDefaultWinHandle = Window;
        HObject ho_Image;
        HOperatorSet.GenEmptyObj(out ho_Image);
        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, filePath); 
        HOperatorSet.DispObj(ho_Image, Window);
        action(ho_Image);
    }

    // Procedures 
    //private static HTuple hv_DataCodeHandle = null;
    private static string[] GetQrCode(HObject ho_Image, string symbolType,int count)
    {
        try
        {
            DateTime date = DateTime.Now; 
            HObject ho_SymbolXLDs; 
            HTuple hv_ResultHandles = null; 
            HTuple hv_DecodedDataStrings = null; 
            HTuple hv_DataCodeHandle = null; 
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs); 
            HOperatorSet.CreateDataCode2dModel(symbolType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);

            string hv_model_path = CodeTest.HDCodeHelper.GetCodeParamFilePath(symbolType);
            if (!hv_model_path.Equals(""))
            {
                HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
            }
            
            ho_SymbolXLDs.Dispose();
          
            if (count <= 0)
            {
                HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
               new HTuple (), new HTuple (), out hv_ResultHandles, out hv_DecodedDataStrings);
            }
            else
            {
                HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
               "stop_after_result_num", count, out hv_ResultHandles, out hv_DecodedDataStrings);
            }
            HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle); 
            string[] resultList = hv_DecodedDataStrings.SArr; 
            return resultList;
        }
        catch (Exception ex)
        {
            return new string[] { };
        }
    } 
}