private void Form1_FormClosed(object sender, FormClosedEventArgs e) { try { KSJApiBase.KSJ_PreviewStart(0, false); KSJApiBase.KSJ_UnInit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } Application.Exit(); }
private void CheckBox_PREVIEWSTART_CheckedChanged(object sender, EventArgs e) { KSJApiBase.KSJ_PreviewSetPos(m_nDeviceCurSel, PictureBox_PREVIEWWND.Handle, 0, 0, PictureBox_PREVIEWWND.Width, PictureBox_PREVIEWWND.Height); bool bCheck = CheckBox_PREVIEWSTART.Checked; KSJApiBase.KSJ_PreviewStart(m_nDeviceCurSel, (bCheck ? true : false)); if (bCheck) { Timer_GET_FRAME_RATE.Start(); } else { Timer_GET_FRAME_RATE.Stop(); } }
private void UpdateTrigger()//更新触发模式 { string[] szMode = new string[4] { "Internal", "External", "Software", "Fixed Frame Rate" }; int nMin = 0; int nMax = 0; KSJApiBase.KSJ_GetParamRange(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJRIGGER_MODE, ref nMin, ref nMax); int i = 0; for (i = 0; i < 4; i++) { comboBox_TriggerMode.Items.Insert(i, szMode[i]); } if (nMax == 4) { comboBox_TriggerMode.Items.Insert(i, "High Low Fixed Frame Rate"); } KSJApiTriggerMode.KSJ_TriggerModeGet(m_nDeviceCurSel, ref m_TriggerMode); comboBox_TriggerMode.SelectedIndex = (int)m_TriggerMode; string[] szMethod = new string[4] { "Falling Edge", "Rising Edge", "High Level", "Low Level" }; for (i = 0; i < 4; i++) { comboBox_TriggerMethod.Items.Insert(i, szMethod[i]); } KSJApiTriggerMode.KSJ_TriggerMethodGet(m_nDeviceCurSel, ref m_TriggerMethod); comboBox_TriggerMethod.SelectedIndex = (int)m_TriggerMethod; KSJApiTriggerMode.KSJ_GetFixedFrameRateEx(m_nDeviceCurSel, ref m_fFixedFrameRate); FrameRate.Value = (decimal)m_fFixedFrameRate; }
private void Button_CAPTURE_FOV_SET_Click(object sender, EventArgs e) { int nColStart = 0; int nRowStart = 0; int nColSize = 0; int nRowSize = 0; KSJApiBase.KSJ_ADDRESSMODE ColAddressMode = KSJApiBase.KSJ_ADDRESSMODE.KSJ_SKIPNONE; KSJApiBase.KSJ_ADDRESSMODE RowAddressMode = KSJApiBase.KSJ_ADDRESSMODE.KSJ_SKIPNONE; nColStart = Convert.ToInt32(TextBox_CAPTURE_COL_START.Text); nRowStart = Convert.ToInt32(TextBox_CAPTURE_ROW_START.Text); nColSize = Convert.ToInt32(TextBox_CAPTURE_COL_SIZE.Text); nRowSize = Convert.ToInt32(TextBox_CAPTURE_ROW_SIZE.Text); KSJApiBase.KSJ_CaptureSetFieldOfView(m_nDeviceCurSel, nColStart, nRowStart, nColSize, nRowSize, ColAddressMode, RowAddressMode); // User may not set FOV correctly, ksjapi will correct it! KSJApiBase.KSJ_CaptureGetFieldOfView(m_nDeviceCurSel, ref nColStart, ref nRowStart, ref nColSize, ref nRowSize, ref ColAddressMode, ref RowAddressMode); TextBox_PREVIEW_COL_START.Text = nColStart.ToString(); TextBox_PREIVEW_ROW_START.Text = nRowStart.ToString(); TextBox_PREIVEW_COL_SIZE.Text = nColSize.ToString(); TextBox_PREIVEW_ROW_SIZE.Text = nRowSize.ToString(); }
public void UpdateInterface() { if (m_nDeviceCurSel == -1) { return; } // Initial Exposure Time int nMin = 0; int nMax = 0; int nCur = 0; KSJApiBase.KSJ_GetParamRange(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_EXPOSURE, ref nMin, ref nMax); KSJApiBase.KSJ_GetParam(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_EXPOSURE, ref nCur); NumericUpDown_EXPOSURE_TIME_MS.Minimum = nMin; NumericUpDown_EXPOSURE_TIME_MS.Maximum = nMax; NumericUpDown_EXPOSURE_TIME_MS.Value = nCur; string szText = String.Format("{0}-{1} ms ({2}sec, {3}min)", nMin, nMax, (float)nMin / 1000.0f, (float)nMax / 60000.0f); Label_EXPOSURE_TIME_RANGE.Text = szText; KSJApiBase.KSJ_GetParamRange(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_EXPOSURE_LINES, ref nMin, ref nMax); KSJApiBase.KSJ_GetParam(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_EXPOSURE_LINES, ref nCur); NumericUpDown_EXPOSURE_LINES.Minimum = nMin; NumericUpDown_EXPOSURE_LINES.Maximum = nMax; NumericUpDown_EXPOSURE_LINES.Value = nCur; szText = String.Format("{0}-{1} Lines", nMin, nMax); Label_EXPOSURE_LINES_RANGE.Text = szText; // GAIN, Because R,G,B Gain has same range. KSJApiBase.KSJ_GetParamRange(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_RED, ref nMin, ref nMax); KSJApiBase.KSJ_GetParam(m_nDeviceCurSel, KSJApiBase.KSJ_PARAM.KSJ_RED, ref nCur); NumericUpDown_GAIN.Minimum = nMin; NumericUpDown_GAIN.Maximum = nMax; NumericUpDown_GAIN.Value = nCur; szText = String.Format("{0}-{1} Lines", nMin, nMax); Label_GAIN_RANGE.Text = szText; // FOV int nColStart = 0; int nRowStart = 0; int nColSize = 0; int nRowSize = 0; KSJApiBase.KSJ_ADDRESSMODE ColAddressMode = KSJApiBase.KSJ_ADDRESSMODE.KSJ_SKIPNONE; KSJApiBase.KSJ_ADDRESSMODE RowAddressMode = KSJApiBase.KSJ_ADDRESSMODE.KSJ_SKIPNONE; KSJApiBase.KSJ_PreviewGetDefaultFieldOfView(m_nDeviceCurSel, ref nColStart, ref nRowStart, ref nColSize, ref nRowSize, ref ColAddressMode, ref RowAddressMode); // Preview, Capture they are same, so you should get one is ok! szText = String.Format("{0}-{1} Lines", nColSize, nRowSize); Label_FOV_RANGE.Text = szText; TextBox_PREVIEW_COL_START.Text = nColStart.ToString(); TextBox_PREIVEW_ROW_START.Text = nRowStart.ToString(); TextBox_PREIVEW_COL_SIZE.Text = nColSize.ToString(); TextBox_PREIVEW_ROW_SIZE.Text = nRowSize.ToString(); KSJApiBase.KSJ_CaptureGetDefaultFieldOfView(m_nDeviceCurSel, ref nColStart, ref nRowStart, ref nColSize, ref nRowSize, ref ColAddressMode, ref RowAddressMode); TextBox_CAPTURE_COL_START.Text = nColStart.ToString(); TextBox_CAPTURE_ROW_START.Text = nRowStart.ToString(); TextBox_CAPTURE_COL_SIZE.Text = nColSize.ToString(); TextBox_CAPTURE_ROW_SIZE.Text = nRowSize.ToString(); }
private unsafe void Button_CAPTURE_Click(object sender, EventArgs e) { if (m_nDeviceCurSel == -1) { return; } int nCaptureWidth = 0; int nCaptureHeight = 0; int nCaptureBitCount = 0; int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount); PrintErrorMessage(nRet); byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)]; long counterStart = 0; KSJApiBase.QueryPerformanceCounter(ref counterStart); nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData); PrintErrorMessage(nRet); if (nRet != KSJCode.RET_SUCCESS) { TextBox_ELAPSE_TIME.Text = "Capture Fail."; } long counterEnd = 0; KSJApiBase.QueryPerformanceCounter(ref counterEnd); long nFreq = 0; KSJApiBase.QueryPerformanceFrequency(ref nFreq); float fInterval = (float)(counterEnd - counterStart); float fElapse = fInterval / (float)nFreq * 1000; // MS bool bCheck = CheckBox_SAVE.Checked; if (bCheck) { string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse); KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName); } TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse); Bitmap bitmap = new Bitmap(nCaptureWidth, nCaptureHeight, PixelFormat.Format8bppIndexed); //获取图像的BitmapData对像 BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); byte *ptr = (byte *)(bmpData.Scan0); for (int i = 0; i < nCaptureHeight; i++) { for (int j = 0; j < nCaptureWidth; j++) { int offset = (nCaptureHeight - i - 1) * bmpData.Width + j; int val = pImageData[offset]; *(ptr + j) = (byte)val; } ptr = ptr + bmpData.Stride; } //Marshal.Copy(bmpData, 0, ptr, scanBytes); bitmap.UnlockBits(bmpData); // 解锁内存区域 // 修改生成位图的索引表,从伪彩修改为灰度 ColorPalette palette; // 获取一个Format8bppIndexed格式图像的Palette对象 using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed)) { palette = bmp.Palette; } for (int i = 0; i < 256; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } // 修改生成位图的索引表 bitmap.Palette = palette; Cognex.VisionPro.CogImage8Grey G8 = new Cognex.VisionPro.CogImage8Grey(bitmap); cogDisplay1.Image = G8; }
unsafe private void Button_CAPTURE_Click(object sender, EventArgs e) { if (m_nDeviceCurSel == -1) { return; } int nCaptureWidth = 0; int nCaptureHeight = 0; int nCaptureBitCount = 0; int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount); PrintErrorMessage(nRet); byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)]; long counterStart = 0; KSJWin.QueryPerformanceCounter(ref counterStart); nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData); PrintErrorMessage(nRet); if (nRet != KSJCode.RET_SUCCESS) { TextBox_ELAPSE_TIME.Text = "Capture Fail."; } bool bCheck = CheckBox_SAVE.Checked; HObject HobjConvert; if (nCaptureBitCount == 8) { int size = pImageData.Length; IntPtr ptrdata = Marshal.AllocHGlobal(size); Marshal.Copy(pImageData, 0, ptrdata, size); HObject Hobj; HOperatorSet.GenImage1(out Hobj, "byte", nCaptureWidth, nCaptureHeight, ptrdata); HOperatorSet.MirrorImage(Hobj, out HobjConvert, "row"); if (bCheck) { HOperatorSet.WriteImage(HobjConvert, "bmp", 0, "KSJToHalcon.bmp"); } } else { byte[] arrayR = new byte[nCaptureWidth * nCaptureHeight]; //红色数组 byte[] arrayG = new byte[nCaptureWidth * nCaptureHeight]; //绿色数组 byte[] arrayB = new byte[nCaptureWidth * nCaptureHeight]; //蓝色数组 int nPixel = nCaptureBitCount / 8; for (int i = 0; i < nCaptureHeight; i++) { int nOffset = i * nCaptureWidth; int nRealOffset = i * nCaptureWidth * 3; for (int j = 0; j < nCaptureWidth; j++) { int nPixelOffset = j * nPixel; arrayR[nOffset + j] = pImageData[nRealOffset + nPixelOffset + 2]; arrayG[nOffset + j] = pImageData[nRealOffset + nPixelOffset + 1]; arrayB[nOffset + j] = pImageData[nRealOffset + nPixelOffset]; } } HObject Hobj; fixed(byte *pR = arrayR, pG = arrayG, pB = arrayB) { HOperatorSet.GenImage3(out Hobj, "byte", nCaptureWidth, nCaptureHeight, new IntPtr(pR), new IntPtr(pG), new IntPtr(pB)); } HOperatorSet.MirrorImage(Hobj, out HobjConvert, "row"); if (bCheck) { HOperatorSet.WriteImage(HobjConvert, "bmp", 0, "KSJToHalcon.bmp"); } } long counterEnd = 0; KSJWin.QueryPerformanceCounter(ref counterEnd); long nFreq = 0; KSJWin.QueryPerformanceFrequency(ref nFreq); float fInterval = (float)(counterEnd - counterStart); float fElapse = fInterval / (float)nFreq * 1000; // MS HOperatorSet.SetPart(this.hWindowControl1.HalconWindow, 0, 0, nCaptureHeight - 1, nCaptureWidth - 1); HOperatorSet.DispObj(HobjConvert, this.hWindowControl1.HalconWindow); //if (bCheck) //{ // string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, // DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse); // KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName); //} //TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse); //Bitmap bitmap = BytesToBitmap(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount); //PictureBox_PREVIEWWND.Image = Image.FromHbitmap(bitmap.GetHbitmap()); }