C#与halcon混合编程(8)

标定与其他

**    1、  标定相关内容为标定后变换参数的修改 ;

**    2、  其他内容如下 ;

**    3、  高速检测状态的工程程序中不能有异常捕获的内容存在,因其处理时间相对较长,会打乱整个设备流程 ;

//  imageTool.cs
//  添加一个新的通用函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Windows.Forms;

namespace vision
{
    [Serializable]
    public class ImageTool
    {
        [NonSerialized]
        public HWindow hwin;
        [NonSerialized]
        public HObject image;
        [NonSerialized]
        public bool issetting=false;
        [NonSerialized]
        public FlowLayoutPanel pannel;
        [NonSerialized]
        public UserControl con1;
        [NonSerialized]
        public List<ImageTool> temp;

        public bool isdebug;

        public List<bool> ischeck = new List<bool>();
        public List<string> absname = new List<string>();
        public List<string> defname = new List<string>();

        public List<int> posdataset = new List<int>();

        public List<double> xx = new List<double>();
        public List<double> sx = new List<double>();
        public List<double> result = new List<double>();

        public List<bool> panding = new List<bool>();

        public List<int> goodcount = new List<int>();
        public List<int> badcount = new List<int>();
        public virtual void draw()
        { }
        public virtual void init()
        { }
        public virtual void method()
        { }
        public virtual void dispresult()
        { }
        public virtual void afterdraw()
        { }
        public virtual void recon()
        { }
        public void senttods()
        {
            lock (pathtool.lockobj)  //加锁,防止读和写同时进行;
            {
                //按照传入的位置,把结果和合格率写入 ds
                for (int i = 0; i < posdataset.Count; i++)
                {
                    mainform.ds.Tables[1].Rows[posdataset[i]]["测量"] = result[i];
                    mainform.ds.Tables[1].Rows[posdataset[i]]["合格率"] = (100 * goodcount[i] / Convert.ToDouble((goodcount[i] + badcount[i]))).ToString("0.0");
                }
            }
        }
    }
}
//  areatool.cs
// 检测模式和设置模式所采用的方式有所不同 ;一个根据下标,一个根据设定值 ;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Windows.Forms;

namespace vision
{
    [Serializable]
    class areatool:ImageTool
    {
        [NonSerialized]
        public HObject imreduce, resultroi;
        [NonSerialized]
        HTuple r1, c1, r2, c2, r, c, area;
        [NonSerialized]
        HObject roi;
        [NonSerialized]
        public areacon con;
        double lowthresh, hightresh, larea, harea,row1,col1,row2,col2;
        [NonSerialized]
        public bool logicresult;
        public areatool()
        {
                    con = new areacon();
                    con.SetChangeEvent += new areacon.SetChangeHandler(afterdraw);
                    con.redrawEvent += new areacon.redrawHandler(this.draw);
                    con.RemoveEvent += new areacon.RemoveHandler(remove);
                    con1 = con;
                    ischeck.Clear();
                    absname.Clear();
                    defname.Clear();
                    posdataset.Clear();

                    ischeck.Add(true);
                    absname.Add("面积测量");
                    defname.Add("面积测量");
                    xx.Add(0);
                    sx.Add(0);
                    goodcount.Add(0);
                    badcount.Add(0);
                    result.Add(0);
                    panding.Add(false);
        }
        public override void draw()
        {
            HOperatorSet.SetColor(hwin, "red");
            HOperatorSet.GenEmptyObj(out roi);
            HOperatorSet.DrawRectangle1(hwin, out r1, out c1, out r2, out c2);
            HOperatorSet.GenRectangle1(out roi, r1, c1, r2, c2);
            row1=r1.D;
            col1=c1.D;
            row2 = r2.D;
            col2 = c2.D;
            afterdraw();
        }
        private void remove()
        {
            //  HOperatorSet.ClearMetrologyModel(MetrologyHandle);
            pannel.Controls.Remove(con);
            temp.Remove(this);
        }
        public override void init()
        {
            HOperatorSet.GenEmptyObj(out roi);
            HOperatorSet.GenRectangle1(out roi, Convert.ToInt32(row1), Convert.ToInt32(col1), Convert.ToInt32(row2), Convert.ToInt32(col2));
            // HOperatorSet.WriteRegion(roi, "1.reg");
            HOperatorSet.GenEmptyObj(out imreduce);
            HOperatorSet.GenEmptyObj(out resultroi);
        }
        public override void method()
        {
            HOperatorSet.ReduceDomain(image, roi, out imreduce);
            //  HOperatorSet.WriteRegion(roi, "1.reg");
            HOperatorSet.Threshold(imreduce, out resultroi, lowthresh, hightresh);
            HOperatorSet.AreaCenter(resultroi, out area, out r, out c);
            result[0] = area.D;
            if (area > xx[0] && area < sx[0])
            {
                panding[0] = true;
            }
            else
            {
                panding[0] = false;
            }
            if (issetting)  // 检测模式和设置模式所采用的方式有所不同 ;一个根据下标,一个根据设定值 ;
            {
                //tool set窗口中,使用上限下限来判断
                for (int i = 0; i < result.Count; i++)
                {
                    if (result[i] >= xx[i] && result[i] <= sx[i])
                    { panding.Add(true); goodcount[i]++; }
                    else
                    { panding.Add(false); badcount[i]++; }
                }
            }
            else
            {
                //检测过程中,直接使用ds中的参数判断
                for (int i = 0; i < result.Count; i++)
                {
                    if (result[i] >= Convert.ToDouble(mainform.ds.Tables[1].Rows[posdataset[i]]["下限"]) && result[i] <= Convert.ToDouble(mainform.ds.Tables[1].Rows[posdataset[i]]["上限"]))
                    { panding.Add(true); goodcount[i]++; }
                    else
                    { panding.Add(false); badcount[i]++; }
                }
            }
        }
        public override void dispresult()
        {
            if (logicresult)
            {
                HOperatorSet.SetColor(hwin, "green");
                HOperatorSet.DispObj(resultroi, hwin);
            }
            else
            {
                HOperatorSet.SetColor(hwin, "red");
                HOperatorSet.DispObj(resultroi, hwin);
            }
            if (issetting)
            {
                con.areaval =Convert.ToInt32( result[0]);
            }
        }
        public override void afterdraw()
        {
            HOperatorSet.DispObj(image, hwin);
            lowthresh = con.Lowthresh;
            hightresh = con.Highthresh;
            xx[0] = con.Larea;
            sx[0] = con.Harea;
            method();
            con.areaval = area;
            dispresult();
        }
        public override void recon()
        {
            con = new areacon();
            con.setval(lowthresh, hightresh, xx[0], sx[0]);
            con.SetChangeEvent += new areacon.SetChangeHandler(afterdraw);
            con.redrawEvent += new areacon.redrawHandler(this.draw);
            con.RemoveEvent += new areacon.RemoveHandler(remove);
            con1 = con;
        }
        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)
        {
            // Local iconic variables 
            // Local control variables 
            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 = null, hv_HeightWin = null, hv_MaxAscent = null;
            HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
            HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
            HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null;
            HTuple 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();

            // Initialize local and output iconic variables 
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow (same as if no second value is given)
            //       otherwise -> use given string as color string for the shadow color
            //
            //Prepare window
            HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
                out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
                out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_WindowHandle, 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_WindowHandle, 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_WindowHandle, 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_WindowHandle, 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_WindowHandle, 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_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, 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_WindowHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                    hv_Index));
            }
            //Reset changed window settings
            HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                hv_Column2Part);
            return;
        }
    }
}
//  existcon.cs
//  添加一个新的检测项,可以和之前的检测项一同显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;

namespace vision
{
    public partial class existcon : UserControl  //添加一个新的检测项
    {
        public bool state = true;
        public HWindowControl wincon;
        public HObject image;
        public HTuple lowthresh, highthresh;
        public delegate void SetChangeHandler();
        public event SetChangeHandler SetChangeEvent;
        public delegate void RemoveHandler();
        public event RemoveHandler RemoveEvent;
        public delegate void upRemoveHandler();
        public event upRemoveHandler upRemoveEvent;
        public delegate void downRemoveHandler();
        public event downRemoveHandler downRemoveEvent;
        public delegate void redrawHandler();
        public event redrawHandler redrawEvent;
        public int Lowthresh
        {
            set { lowthresh = value; trackBar1.Value = value; }
            get { return trackBar1.Value; }
        }
        public int Highthresh
        {
            set { highthresh = value; trackBar3.Value = value; }
            get { return trackBar3.Value; }
        }
        public bool check1
        {
            get { return checkEdit1.Checked; }
        }

        public bool check2
        {
            get { return checkEdit2.Checked; }
        }
        public List<double> xx
        {
            get {
                List<double> temp = new List<double>();
                if (checkEdit1.Checked)
                {
                    temp.Add(Convert.ToDouble(textEdit2.Text));
                
                }
                if (checkEdit2.Checked)
                {
                    temp.Add(Convert.ToDouble(textEdit5.Text));
                }
                return temp; 
            }
        }
        public List<double> sx
        {
            get
            {
                List<double> temp = new List<double>();
                if (checkEdit1.Checked)
                {
                    temp.Add(Convert.ToDouble(textEdit3.Text));
                }
                if (checkEdit2.Checked)
                {
                    temp.Add(Convert.ToDouble(textEdit4.Text));
                }
                return temp;
            }
        }
        public List<bool> ischeck
        {
            get
            {
                List<bool> temp = new List<bool>();
                temp.Add(checkEdit1.Checked);
                temp.Add(checkEdit2.Checked);
                return temp;
            }
        }
        public existcon()
        {
            InitializeComponent();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            redrawEvent();
            button3.Enabled = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (state)
            {
                this.Height = 40;
                state = !state;
            }
            else
            {
                this.Height = 189;
                state = !state;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            RemoveEvent();
        }
        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            SetChangeEvent();
        }
        private void checkEdit2_CheckedChanged(object sender, EventArgs e)
        {
            SetChangeEvent();
        }
        private void textEdit2_EditValueChanged(object sender, EventArgs e)
        {
            if (textEdit2.Text != "")
            {
                SetChangeEvent();
            }
        }
        private void textEdit3_EditValueChanged(object sender, EventArgs e)
        {
            if (textEdit3.Text != "")
            {
                SetChangeEvent();
            }
        }
        private void textEdit5_EditValueChanged(object sender, EventArgs e)
        {
            if (textEdit5.Text != "")
            {
                SetChangeEvent();
            }
        }
        private void textEdit4_EditValueChanged(object sender, EventArgs e)
        {
            if (textEdit4.Text != "")
            {
                SetChangeEvent();
            }
        }
        public void setinitval(double minth, double maxth, List<bool> ischeck, List<double> xx, List<double> sx)
        {
            trackBar1.Scroll -= new EventHandler(trackBar1_Scroll);
            trackBar3.Scroll -= new EventHandler(trackBar3_Scroll);
            checkEdit1.CheckedChanged -= new EventHandler(checkEdit1_CheckedChanged);
            checkEdit2.CheckedChanged -= new EventHandler(checkEdit2_CheckedChanged);
            textEdit2.EditValueChanged -= new EventHandler(textEdit2_EditValueChanged);
            textEdit3.EditValueChanged -= new EventHandler(textEdit3_EditValueChanged);
            textEdit5.EditValueChanged -= new EventHandler(textEdit5_EditValueChanged);
            textEdit4.EditValueChanged -= new EventHandler(textEdit4_EditValueChanged);
            checkEdit1.Checked = ischeck[0];
            checkEdit2.Checked = ischeck[1];
            trackBar1.Value = Convert.ToInt32(minth);
            trackBar3.Value = Convert.ToInt32(maxth);
            if (ischeck[0])
            {
                textEdit2.Text = xx[0].ToString();
                textEdit3.Text = sx[0].ToString();
            }
            if (ischeck[1])
            {
                textEdit5.Text = xx[1].ToString();
                textEdit4.Text = sx[1].ToString();
            }
            trackBar1.Scroll += new EventHandler(trackBar1_Scroll);
            trackBar3.Scroll += new EventHandler(trackBar3_Scroll);
            checkEdit1.CheckedChanged += new EventHandler(checkEdit1_CheckedChanged);
            checkEdit2.CheckedChanged += new EventHandler(checkEdit2_CheckedChanged);
            textEdit2.EditValueChanged += new EventHandler(textEdit2_EditValueChanged);
            textEdit3.EditValueChanged += new EventHandler(textEdit3_EditValueChanged);
            textEdit5.EditValueChanged += new EventHandler(textEdit5_EditValueChanged);
            textEdit4.EditValueChanged += new EventHandler(textEdit4_EditValueChanged);
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            SetChangeEvent();
        }
        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            SetChangeEvent();
        }
    }
}
//  Camera.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml;

namespace vision
{
    public class Camera
    {
        //10.28ch add
        public mainform mrf;
        public object lockobj = new object();
        public List<ImageTool> toollist;
        public int mode;//模式,0内触发,1外触发预采集,2.检测模式;
        public int fps;
        public bool islastcam;//是否为最后一个相机
        public List<bool> result = new List<bool>();//结果队列
        //public List<long> rechunk = new List<long>();
        public List<HWindowControl> halconwin = new List<HWindowControl>();
        public string name;
        public string logicName;
        public HObject Image;
        public delegate void sentlogo(bool isok);
        //private delegate void DeviceRemovedEventHandler();
        public event sentlogo sentlogoEvent;
        public HTuple resultHTuple;
        public HObject RegionToDisp;
        protected Object m_lockObject;
        public HObject tempImage;
        public bool isopen;
        private int exposureTime;
        public int rectifytype;
        public int ExposureTime
        {
            get { return exposureTime; }
            set { exposureTime = value; SetFloatSetting("ExposureTimeAbs", value); }
        }
        private int gain;
        public int Gain
        {
            get { return gain; }
            set { gain = value; SetIntegerSetting("Gain", value); }
        }
        private double pixelDist;
        public double PixelDist
        {
            get { return pixelDist; }
            set { pixelDist = value; }
        }
        public Camera()
        {
            try
            {
                isopen = false;
                toollist = new List<ImageTool>();
                resultHTuple = new HTuple();
                HOperatorSet.GenEmptyObj(out RegionToDisp);
                HOperatorSet.GenEmptyObj(out Image);
                HOperatorSet.GenEmptyObj(out tempImage);
                m_lockObject = new Object();
                exposureTime = 30;
                gain = 300;
                pixelDist = 1;
                mode = 0;
            }
            catch (Exception e)
            {
                MessageBox.Show("相机初始化失败");
            }
        }
        public int sentcounter = 0;
        public int imagecount = 0;
        bool isready = true;
        long lastchunk = 0;
        HObject rectifymap;
        public void ImageProcess()
        {
            try
            {
                if (isready)
                {
                    isready = false;
                    Image.Dispose();
                 //   HOperatorSet.CopyImage(m_imageProvider.ho_Image, out Image);
                    lock (lockobj)
                    {
                        // Card.stop();

                        //三种模式 模式,0内触发,1外触发预采集,2.检测模式;
                        if (mode == 0)
                        {
                            foreach (HWindowControl hwin in halconwin)
                            {
                                // HOperatorSet.ClearWindow(hwin.HalconWindow);
                                hwin.HalconWindow.DispObj(Image);
                            }
                        }
                        else if (mode == 1)
                        {
                            foreach (HWindowControl hwin in halconwin)
                            {
                                hwin.HalconWindow.DispObj(Image);
                            }
                            HOperatorSet.WriteImage(Image, "bmp", 0, pathtool.currentProductPath + "\\" + logicName + "\\" + imagecount + ".bmp");
                        }
                        else if (mode == 2)
                        {
                            //if (this.logicName == "CCD2")
                            //{
                            //    Card.stop();
                            //}
                            //畸变校正
                            if (rectifytype == 0)
                            {
                                HOperatorSet.MapImage(Image, rectifymap, out Image);
                            }
                            bool tempresult = true;
                            //调用toollist处理和显示
                            foreach (ImageTool tool in toollist)
                            {
                                tool.image = Image;
                                tool.method();
                                Image = tool.image;
                                tempresult = tempresult && (!tool.panding.Contains(false));
                            }
                            halconwin[0].HalconWindow.DispObj(Image);
                            foreach (ImageTool tool in toollist)
                            {
                                tool.hwin = halconwin[0].HalconWindow;
                                tool.dispresult();
                                tool.senttods();
                            }
                            ///这里使用检测
                            //并按照是否为currentccd,显示左上角ok ng
                            if (logicName == mainform.CurrentCCD)
                            {
                                //   Card.stop();
                                //此处调用主线程的invoke导致,在imageprovider里的 thread join 时候发生死锁
                                sentlogoEvent(tempresult);
                                //HObject tempim;
                                //HOperatorSet.GenEmptyObj(out tempim);
                                //HOperatorSet.DumpWindowImage(out tempim, halconwin[0].HalconWindow);
                                if (halconwin.Count == 2)
                                {
                                    halconwin[1].HalconWindow.DispObj(Image);
                                    foreach (ImageTool tool in toollist)
                                    {
                                        tool.hwin = halconwin[1].HalconWindow;
                                        tool.dispresult();
                                    }
                                }
                            }
                        }
                    }
                    imagecount++;
                    isready = true;
                }
                else
                {
                }
            }
            catch (Exception e)
            {
                int aa = 0;
            }
        }
        public void Open()
        {
        }
        public void Stop()
        {
        }
        public void InterGrab()
        {
        }
        public void StartGrab()
        {
        }
        public void Close()
        {
        }
        public bool SetIntegerSetting(string Feature, int value)
        {
            return false;
        }
        public bool SetFloatSetting(string Feature, int value)
        {
            return false;
        }
        public bool SetModeFeature(string Feature, string value)
        {
            return false;
        }
        private void CameraRemovedEvent()
        {
        }
        private void OnGrabErrorEvent(Exception grabException, string additionalErrorMessage)
        {
        }
        public void OutPut(bool ONOFF)
        {
        }
        public void singlegrab()
        {
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值