第六神奇----快捷编程(工具的可视化组织和运行)(二)

这里我们理清一个过程的配合,把实时图像从相机丢给桥梁,桥梁(自己创建的程序)丢给图像处理工具(toolbox中用线连起来的tool),结果返回。

第一,相机实时取像dll中,copydata按键截取实时图像传递出来。(使用了微软的消息机制WM_COPYDATA)

        [DllImport("User32.dll", EntryPoint = "SendMessage"),]
        private static extern int SendMsg(int hwnd, int msg, int wparms, ref CopyDataStruct lparm);
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName); 

       public const int WM_COPYDATA = 0x04AA;//注意,并不是0x004a

      public Bitmap glob_curBitmap;
        public struct CopyDataStruct
        {
            public IntPtr dwData;
            public int cbData;
            //  [MarshalAs(UnmanagedType.LPStr)]
            public byte[] lpData;
            public Bitmap tempbmp;//在原来结构基础上增加了位图项
        }

    private void wMCOPYDATAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CopyDataStruct cds = new CopyDataStruct();
            cds.dwData = (IntPtr)null;

            cds.cbData = glob_curBitmap.Width * glob_curBitmap.Height;    //传出图像的宽高   
            cds.lpData = new byte[cds.cbData];//传出的图像buffer
            cds.tempbmp = glob_curBitmap;//传出的有位图

           cds.lpData = glob_buffer8;
            int windowHandler = FindWindow(null, "TestMegaugingLib");  //这是我创建的应用程序窗口,桥梁    
            if (windowHandler == 0)
            {
                return;
            }
            SendMsg(windowHandler, WM_COPYDATA, 0, ref cds);//图像丢给了桥梁
        }

第二,自己创建的应用程序,接受并转发另一个图像处理dll。

   protected override void DefWndProc(ref System.Windows.Forms.Message m)   
        {
            switch (m.Msg)
            {
                case WM_COPYDATA://此处仅有500万图像//已经更改为80万,200万,500万。20161212
                    {
                        var mybytes = new CopyDataStruct();
                        Type mytype = mybytes.GetType();
                        mybytes = (CopyDataStruct)m.GetLParam(mytype);
                        SendMsg(meGaugingLib1.Handle, WM_COPYDATA, 0, ref mybytes);//接受图像转发meGaugingLib1
                    }
                    break;

   default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

第三,meGaugingLib1的运作:

1,双击toolbox中的imagesoure和calibration,创建工具,并连线二者

a,声明  toolitemgrab = new mgToolitem();
            toolitemCA = new mgToolitem();并初始化

mgToolitem的来历:from 线工具设计的变化。

  public class mgToolitem : ROIsimple
    {
        public void DrawRectFrame(bool lbtn, PointF point)
        {
            if (lbtn)
            {
                switch (m_eHandle)
                {
                    case E_HANDLES.E_HANDLE_INSIDE:
                        m_RoiBase1.Drag(point);
                        break;

                    case E_HANDLES.E_HANDLE_SOUTH_EAST:
                        {
                            // m_RoiBase1.DragTolerance(point);
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //m_eHandle = (E_HANDLES)m_RoiBase1.IsPointInRect(point);
                m_eHandle = (E_HANDLES)IsPointInRect(point);
            }
            Insteadof((int)m_eHandle);

            return;
        }
        public int IsPointInRect(PointF point)
        {
            if (point.X <= m_RoiBase1.m_Center.X + 5 && point.X >= m_RoiBase1.m_Center.X - 5
               && point.Y >= m_RoiBase1.m_Center.Y - 5 && point.Y <= m_RoiBase1.m_Center.Y + 5)
            {
                return (int)E_HANDLES.E_HANDLE_INSIDE;
            }
            //else if (point.X <= rcWin.Left + rcWin.Width + 5 && point.X >= rcWin.Left + rcWin.Width - 5
            //      && point.Y >= rcWin.Top + rcWin.Height - 5 && point.Y <= rcWin.Top + rcWin.Height + 5)
            //{
            //    return (int)E_HANDLES.E_HANDLE_SOUTH_EAST;// E_HANDLE_SOUTH_EAST,9   

            //}
            else
            {
                return 0;
            }
        }
    }

b,双击后添加这两个工具,工具响应代码:

  private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int toolnum = 0;//使用list之后此处要好好设计一下201612050827
            if (ispointInRect(new Point(e.X, e.Y), rcs[0]))
            {
                toolnum = 1;
                m_bAddgrab = true;
                toolNo.Add(1);

                if (null == toolitemgrab) toolitemgrab = new mgToolitem();
                    toolitemgrab.m_RoiBase1.ResetRc(new RectangleF(0, 0, 100, 15));/11111             
                mgvppview.Invalidate();
                return;
            }
            if (ispointInRect(new Point(e.X, e.Y), rcs[1]))
            {
                toolnum = 2;
                m_bAdd_ca = true;
                toolNo.Add(2);

                toolitemCA.m_RoiBase1.ResetRc(new RectangleF(0, 40, 100, 15));
                mgvppview.Invalidate();
                return;
            }

}

c,在mgvppview中把二者连线,并paint(画出来):

 PointF[] globarraypointfca = null;//grab到calibration

  private void mgvppview_MouseUp(object sender, MouseEventArgs e)
        {    List<PointF> listPtca = new List<PointF>();

   //toolitemCA指calibration//以下是grab到calibration           
                  if (ispointInRect(startPt, toolitemgrab.m_RoiBase1.rcWin) &&
                      ispointInRect(endPt, toolitemCA.m_RoiBase1.rcWin))
                {//以上可以替换使用ispointinrect201612050822
                    grab传递cali = true;//暂时无取消201709221212
                    float tempy = (float)(toolitemgrab.m_RoiBase1.rcWin.Top / 2.0 + toolitemgrab.m_RoiBase1.rcWin.Bottom / 2.0);
                    float tempy1 = (float)(toolitemCA.m_RoiBase1.rcWin.Top / 2.0 + toolitemCA.m_RoiBase1.rcWin.Bottom / 2.0);
                    listPtca.Add(new PointF(toolitemgrab.m_RoiBase1.rcWin.Right, tempy));
                    Random ran = new Random();
                    int Rn = ran.Next(100, 200);
                    listPtca.Add(new PointF(toolitemgrab.m_RoiBase1.rcWin.Right + Rn, tempy));
                    listPtca.Add(new PointF(toolitemgrab.m_RoiBase1.rcWin.Right + Rn, tempy1));
                    listPtca.Add(new PointF(toolitemCA.m_RoiBase1.rcWin.Right, tempy1));
                    globarraypointfca = listPtca.ToArray();//很像连连看中的连线
                }

}

  private void mgvppview_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Black);

        if (globarraypointfca != null)
            {
                Random ran = new Random();
                int Rn = ran.Next(100, 200);
                g.DrawLines(new Pen(Color.FromArgb(0, Rn, 0)), globarraypointfca); //画了出来连线             
            }

}

2,创建的工具item(show秀)和工具form(图像处理实质)挂钩联系起来:

a,解析到达的图像

   protected override void DefWndProc(ref System.Windows.Forms.Message m)
        // protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WmCopydata://此处仅有500万图像//已经更改为80万,200万,500万。20161212
                    {
                        var mybytes = new COPYDATASTRUCT();
                        Type mytype = mybytes.GetType();
                        mybytes = (COPYDATASTRUCT)m.GetLParam(mytype);//mybytes.cbdata//  string str = mybytes.LpData;

                        int tempW= mybytes.tempbmp.Width;
                        int tempH = mybytes.tempbmp.Height;
                        glob_buffer8 = new byte[tempW * tempH];
                        for (int j = 0; j < tempH; j++)
                        {
                            for (int i = 0; i < tempW; i = i + 1)
                            {
                                int nn = j * tempW + i;

                                glob_buffer8[nn] = mybytes.LpData[nn];//mybytes.DwData
                            }
                        }
                        realtimeImage = mybytes.tempbmp;

                    break;

     default:
                    base.DefWndProc(ref m);
                    break;
            }

}

b,图像是否传递给工具form,由布尔值决定

                  if (m_bAddgrab)
                        {
                           
                            testimagesource.init(tempW, tempH);
                            testimagesource.IS_orgImg = glob_buffer8; //原来这样可以哦201612091526
                            //测试是ok的,201612121004

                            Bitmap tempBmp = realtimeImage;
                            testimagesource.ISbmp = tempBmp;
                            testimagesource.mgUpdate();
                        }
                        if (m_bAdd_ca)
                        {
                            if (grab传递cali)//下一步cali传递gaugeline
                            {
                                tempcalibrate.init(tempW, tempH);
                                tempcalibrate.tongyongimg33gaos = glob_buffer8;

                                Bitmap tempBmp = realtimeImage;
                                tempcalibrate.glob_curBitmap = tempBmp;
                                tempcalibrate.mgUpdate();
                            }
                        }

3,返回结果

a,结果参数和图像继续向后传递:比如,标定最后一步按钮完成savexml()之后,下一步添加找斑(blob)工具,把参数结果和图像向后传递:代码如下:

   if (cali传递blob)
                            {
                                testblobmatch.init(tempW, tempH);
                                testblobmatch.Ma_orgImg = tempcalibrate.tongyongimg33gaos;
                                Bitmap tempBmp = tempcalibrate.glob_curBitmap;
                                testblobmatch.Mabmp = tempBmp;
                                testblobmatch.mgUpdate();
                                //补增xml加载参数201709241121
                                testblobmatch.loadxml();
                               testblobmatch.undistortion(tempW, tempH, testblobmatch.Ma_orgImg, testblobmatch.径向_p);
                            }

b,结果参数返回:仍然使用微软消息传递机制,给桥梁(自己的应用程序),再转给实时图像显示出来,或给运动轴或者激光工作。

待续(慢慢来!...........)每天一点小改变☺

我的邮箱liuganggang_1978@163.com;734523623@qq.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值