用海康函数在C#设置相机实时图ROI

设置ROI的目的

生产线中对相机读图速度的要求特别快 大范围的图对采集有很重的负担
所以要把图片的范围缩小
下面上代码
要注意的是要先关闭相机 再开相机 再设置roi 再实时采集

关闭相机

 public void Close()
    {
      // ch:关闭设备 | en:Close Device
      int nRet;

      nRet = m_pMyCamera.MV_CC_CloseDevice_NET();
      if (MyCamera.MV_OK != nRet)
      {
        return;
      }

      nRet = m_pMyCamera.MV_CC_DestroyDevice_NET();
      if (MyCamera.MV_OK != nRet)
      {
        return;
      }

      // ch:控件操作 | en:Control Operation
      //SetCtrlWhenClose();

      // ch:取流标志位清零 | en:Reset flow flag bit
      // m_bGrabbing = false;
    }//关闭相机```


    打开相机
    public void dakaixiangji()
    {

      if (m_pDeviceList.nDeviceNum == 0 || comboBox1.SelectedIndex == -1)
      {
        cc.ShowErrorMsg("No device, please select", 0);
        return;
      }
      int nRet = -1;

      // ch:获取选择的设备信息 | en:Get selected device information
      MyCamera.MV_CC_DEVICE_INFO device =
          (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[comboBox1.SelectedIndex],
                                                        typeof(MyCamera.MV_CC_DEVICE_INFO));

      // ch:打开设备 | en:Open device
      if (null == m_pMyCamera)
      {
        m_pMyCamera = new MyCamera();
        if (null == m_pMyCamera)
        {
          return;
        }
      }

      nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
      if (MyCamera.MV_OK != nRet)
      {
        return;
      }

      nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
      if (MyCamera.MV_OK != nRet)
      {
        m_pMyCamera.MV_CC_DestroyDevice_NET();
        cc.ShowErrorMsg("Device open fail!", nRet);
        return;
      }

      // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
      if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
      {
        int nPacketSize = m_pMyCamera.MV_CC_GetOptimalPacketSize_NET();
        if (nPacketSize > 0)
        {
          nRet = m_pMyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
          if (nRet != MyCamera.MV_OK)
          {
            Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
          }
        }
        else
        {
          Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
        }
      }

    }
    
设置ROI

public void SetRoi()
    {
      int nRet = MyCamera.MV_OK;
      ImageSize imgMaxSize = /*new ImageSize();*/  GetMaxSize();
      //imgMaxSize.width = 1024;
      //imgMaxSize.height = 960;
      uint nWidthMax = imgMaxSize.width;
      uint nHeightMax = imgMaxSize.height;
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", 0);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set OffsetX Fail!", nRet);
      }
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", 0);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set OffsetY Fail!", nRet);
      }
      imgWidth = (uint)(_roi.width * nWidthMax);
      uint nVal = (imgWidth / 16) * 16;
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Width", nVal);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set Width Fail!", nRet);
      }
      imgHeight = (uint)(_roi.height * nHeightMax);
      nVal = (imgHeight / 16) * 16;
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Height", nVal);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set Height Fail!", nRet);
      }
      offX = (uint)(_roi.offX * nWidthMax);
      nVal = (offX / 16) * 16;
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", nVal);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set OffsetX Fail!", nRet);
      }
      offY = (uint)(_roi.offY * nHeightMax);
      nVal = (offY / 16) * 16;
      nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", nVal);
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Set OffsetY Fail!", nRet);
      }
      //return true;/*GetPayloadSize();*/
    }


开始采集


public void caiji()
    {
      int nRet;
      // ch:开始采集 | en:Start Grabbing
      nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Trigger Fail!", nRet);
        return;
      }

      // ch:控件操作 | en:Control Operation
      //SetCtrlWhenStartGrab();

      // ch:标志位置位true | en:Set position bit true
      //m_bGrabbing = true;


      // ch:显示 | en:Display


      //Form1 fm = new Form1();
      //if (fm.b)
      //{
      //  SetRoi();
      //}

      //PointSetRoi point = new Point((int)offX,(int)offY);
      //pictureBox1.Location = point;
      //pictureBox1.Width = (int)imgWidth;
      //pictureBox1.Height = (int)imgHeight;
      nRet = m_pMyCamera.MV_CC_Display_NET(pictureBox1.Handle);

      // nRet = m_pMyCamera.MV_CC_Display_NET(pictureBox1.Handle);

      if (MyCamera.MV_OK != nRet)
      {
        cc.ShowErrorMsg("Display Fail!", nRet);
      }
    }
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值