private void PictureBox_PREVIEWWND_MouseWheel(object sender, MouseEventArgs e) { if (m_nScale <= 0.3 && e.Delta <= 0) { return; //缩小下线 } if (m_nScale >= 4.9 && e.Delta >= 0) { return; //放大上线 } //获取 当前点到画布坐标原点的距离 SizeF szSub = (Size)m_ptCanvas - (Size)e.Location; //当前的距离差除以缩放比还原到未缩放长度 float tempX = szSub.Width / m_nScale; //这里 float tempY = szSub.Height / m_nScale; //将画布比例 //还原上一次的偏移 //按照当前缩放比还原到 m_ptCanvas.X -= (int)(szSub.Width - tempX); //没有缩放 m_ptCanvas.Y -= (int)(szSub.Height - tempY); //的状态 //重置距离差为 未缩放状态 szSub.Width = tempX; szSub.Height = tempY; m_nScale += e.Delta > 0 ? 0.2F : -0.2F; //重新计算 缩放并 重置画布原点坐标 m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width); m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height); PictureBox_PREVIEWWND.Invalidate(); }
private void PictureBox_PREVIEWWND_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //移动过程中 左键点下 重置画布坐标系 m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown)); PictureBox_PREVIEWWND.Invalidate(); } }
private void PictureBox_PREVIEWWND_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //如果左键点下 初始化计算要用的临时数据 m_ptMouseDown = e.Location; m_ptCanvasBuf = m_ptCanvas; } PictureBox_PREVIEWWND.Focus(); }
private void GvspThread() { int nRet = 0; uint uiGvspport = 0xcbbb; uint uiGvspsize = 0; KSJGigeVisionApi.KSJGIGEVISION_WriteRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0xd00, uiGvspport); KSJGigeVisionApi.KSJGIGEVISION_CameraSetGvspPort(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], (int)uiGvspport); KSJGigeVisionApi.KSJGIGEVISION_ReadRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0xd04, ref uiGvspsize); KSJGigeVisionApi.KSJGIGEVISION_CameraSetGvspSize(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], (int)uiGvspsize); uint nColSize = 0; uint nRowSize = 0; uint nType = 0; KSJGigeVisionApi.KSJGIGEVISION_ReadRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0x100, ref nColSize); KSJGigeVisionApi.KSJGIGEVISION_ReadRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0x104, ref nRowSize); KSJGigeVisionApi.KSJGIGEVISION_ReadRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0xb000, ref nType); byte[] pImageData = new byte[nColSize * nRowSize * 8]; KSJGigeVisionApi.KSJGIGEVISION_WriteRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0x124, 1); decimal fProfiley; fProfiley = YnumericUpDown.Value; float fZmapLow = (float)(Math.Round(LownumericUpDown.Value * 100)) / 100; float fZmapHigh = (float)(Math.Round(HighnumericUpDown.Value * 100)) / 100; float unit = (fZmapHigh - fZmapLow) / 255; int nWidth = (int)nColSize; int nHeight = (int)nRowSize; int nCount = 0; float[] profile; float[] profilex; Graphics g; int i = 0; Point p1 = new Point(0, 0); Point p2 = new Point(0, 0); int Index = 0; byte[] idata = new byte[nColSize * nRowSize]; //Bitmap bitmap; while (true) { if (exitEvent.WaitOne(0)) { KSJGigeVisionApi.KSJGIGEVISION_WriteRegister(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], 0x124, 0); break; } nRet = KSJGigeVisionApi.KSJGIGEVISION_CameraGet3DGvspData(CamareIndex[m_nDeviceCurSel, 0], CamareIndex[m_nDeviceCurSel, 1], (int)nType, pImageData, ref nWidth, ref nHeight); //采集相机一帧 if (nRet == 1) //采集成功 { if (nType == 0) //gray { bitmap = ToGrayBitmap(pImageData, nWidth, nHeight); if (!init)//未进行过宽高比例计算 { float proportion = (float)PictureBox_PREVIEWWND.Width / (float)PictureBox_PREVIEWWND.Height; float proportion_bmp = (float)nWidth / (float)nHeight; if (proportion < proportion_bmp)//比较控件和图片宽高比,根据不同情况设置位图在控件内初始显示位置 { m_nScale = (float)PictureBox_PREVIEWWND.Width / (float)nWidth; float h = nHeight * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Height - (int)h) / 2; m_ptBmp = new Point(0, (int)(offset / m_nScale)); } else { m_nScale = (float)PictureBox_PREVIEWWND.Height / (float)nHeight; float w = nWidth * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Width - (int)w) / 2; m_ptBmp = new Point((int)(offset / m_nScale), 0); } init = true;//进行设置后标记 } PictureBox_PREVIEWWND.Invalidate(); } else if (nType == 1)//profile { profile = new float[nColSize * nHeight]; profilex = new float[nColSize * nHeight]; for (i = 0; i < nWidth; i++) { profile[i] = BitConverter.ToSingle(pImageData, i * 4); } for (i = 0; i < nWidth; i++) { profilex[i] = BitConverter.ToSingle(pImageData, (i + nWidth) * 4); } bitmap = new Bitmap(nWidth, 256); g = Graphics.FromImage(bitmap); g.Clear(Color.Black); for (i = 1; i < nWidth; ++i)//绘制图片 { if (profile[i - 1] != -1000 && profile[i] != -1000 && profilex[i - 1] > 0 && profilex[i] > 0) { p1.X = (int)(profilex[i - 1] * 1280 / 50); p1.Y = (int)(256 - profile[i - 1] * 256 / 10); p2.X = (int)(profilex[i] * 1280 / 50); p2.Y = (int)(256 - profile[i] * 256 / 10); g.DrawLine(new Pen(Color.White, 1), p1, p2); } } if (!init)//未进行过宽高比例计算 { float proportion = (float)PictureBox_PREVIEWWND.Width / (float)PictureBox_PREVIEWWND.Height; float proportion_bmp = (float)nWidth / (float)nHeight; if (proportion < proportion_bmp)//比较控件和图片宽高比,根据不同情况设置位图在控件内初始显示位置 { m_nScale = (float)PictureBox_PREVIEWWND.Width / (float)nWidth; float h = nHeight * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Height - (int)h) / 2; m_ptBmp = new Point(0, (int)(offset / m_nScale)); } else { m_nScale = (float)PictureBox_PREVIEWWND.Height / (float)nHeight; float w = nWidth * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Width - (int)w) / 2; m_ptBmp = new Point((int)(offset / m_nScale), 0); } init = true;//进行设置后标记 } PictureBox_PREVIEWWND.Invalidate(); //this.PictureBox_PREVIEWWND.Image = bitmap; g.Dispose(); } else if (nType == 2)//3d { nCount = nWidth * nHeight; profile = new float[nCount]; profilex = new float[nCount]; for (i = 0; i < nCount; i++) { profile[i] = BitConverter.ToSingle(pImageData, i * 4); } for (i = 0; i < nCount; i++) { profilex[i] = BitConverter.ToSingle(pImageData, (i + nCount) * 4); } Index = 0; for (i = 0; i < nHeight; ++i) { for (int j = 0; j < nWidth; ++j) { if (profile[Index] <= fZmapHigh && profile[Index] >= fZmapLow) { idata[Index] = (byte)((profile[Index] - fZmapLow) / unit + 0.5); } else { idata[Index] = 0; } ++Index; } } bitmap = ToGrayBitmap(idata, nWidth, nHeight); //this.PictureBox_PREVIEWWND.Image = bitmap; if (!init)//未进行过宽高比例计算 { float proportion = (float)PictureBox_PREVIEWWND.Width / (float)PictureBox_PREVIEWWND.Height; float proportion_bmp = (float)nWidth / (float)nHeight; if (proportion < proportion_bmp)//比较控件和图片宽高比,根据不同情况设置位图在控件内初始显示位置 { m_nScale = (float)PictureBox_PREVIEWWND.Width / (float)nWidth; float h = nHeight * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Height - (int)h) / 2; m_ptBmp = new Point(0, (int)(offset / m_nScale)); } else { m_nScale = (float)PictureBox_PREVIEWWND.Height / (float)nHeight; float w = nWidth * m_nScale; m_ptCanvas = new Point(0, 0); int offset = (PictureBox_PREVIEWWND.Width - (int)w) / 2; m_ptBmp = new Point((int)(offset / m_nScale), 0); } init = true;//进行设置后标记 } PictureBox_PREVIEWWND.Invalidate(); bool bCheck = SavecheckBox.Checked; if (bCheck) { #if halcon12//使用halcon保存om3文件 string t1, filename; t1 = DateTime.Now.ToString("yyyy-MM-dd"); System.Diagnostics.Debug.WriteLine("开始保存"); filename = t1 + "-" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".om3"; HTuple hv_PointCoordX = new HTuple(); HTuple hv_PointCoordY = new HTuple(); HTuple hv_PointCoordZ = new HTuple(); for (i = 0; i < nCount; i++) { hv_PointCoordX[i] = profilex[i]; hv_PointCoordY[i] = (float)fProfiley * (i / 1280); hv_PointCoordZ[i] = profile[i]; } HTuple hv_ObjectModel3D; HOperatorSet.GenObjectModel3dFromPoints(hv_PointCoordX, hv_PointCoordY, hv_PointCoordZ, out hv_ObjectModel3D); HOperatorSet.WriteObjectModel3d(hv_ObjectModel3D, "om3", filename, "invert_normals", "false"); System.Diagnostics.Debug.WriteLine("保存完毕"); #else//不使用halcon保存pcd文件 FileStream fs = new FileStream("test.pcd", FileMode.Create); string temp = "# .PCD v0.7 - Point Cloud Data file format\n"; byte[] data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "VERSION 0.7\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "FIELDS x y z\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "SIZE 4 4 4\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "TYPE F F F\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "COUNT 1 1 1\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = string.Format("WIDTH {0}\n", nColSize); data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = string.Format("HEIGHT {0}\n", nRowSize); data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "VIEWPOINT 0 0 0 1 0 0 0\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = string.Format("POINTS {0}\n", nCount); data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); temp = "DATA ascii\n"; data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); for (i = 0; i < nCount; i++) { if (i != nCount - 1) { if (profilex[i] == -1000) { temp = "nan nan nan\n"; } else { temp = string.Format("{0:N5} {1:N5} {2:N5}\n", profilex[i], fProfiley * (i / 1280), profile[i]); } } else { if (profilex[i] == -1000) { temp = "nan nan nan"; } else { temp = string.Format("{0:N5} {1:N5} {2:N5}", profilex[i], fProfiley * (i / 1280), profile[i]); } } data = System.Text.Encoding.Default.GetBytes(temp); fs.Write(data, 0, data.Length); } //清空缓冲区、关闭流 fs.Flush(); fs.Close(); #endif } } } } }