private void textBox_RedGain_TextChanged(object sender, EventArgs e) { if (this.ActiveControl != sender) { return; } if (textBox_RedGain.Text == "" || textBox_GreenGain.Text == "" || textBox_BlueGain.Text == "" ) { return; } string s1 = textBox_RedGain.Text; int r = Convert.ToInt32(s1); string s2 = textBox_GreenGain.Text; int g = Convert.ToInt32(s2); string s3 = textBox_BlueGain.Text; int b = Convert.ToInt32(s3); //输入框里的值改变后,更新滚动条。 trackBar_RedGain.Value = r; trackBar_GreenGain.Value = g; trackBar_BlueGain.Value = b; MvApi.CameraSetGain(m_hCamera, r, g, b); }
private void InitCamera() { CameraSdkStatus status = 0; tSdkCameraDevInfo[] DevList; MvApi.CameraEnumerateDevice(out DevList); int NumDev = (DevList != null ? DevList.Length : 0); if (NumDev < 1) { MessageBox.Show("未扫描到相机"); return; } else if (NumDev == 1) { status = MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]); } else { status = MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber); } if (status == 0) { MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo); MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera); MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0); MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero); // 黑白相机设置ISP输出灰度图像 // 彩色相机ISP默认会输出BGR24图像 tSdkCameraCapbility cap; MvApi.CameraGetCapability(m_hCamera, out cap); if (cap.sIspCapacity.bMonoSensor != 0) { MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8); // 创建灰度调色板 Bitmap Image = new Bitmap(1, 1, PixelFormat.Format8bppIndexed); m_GrayPal = Image.Palette; for (int Y = 0; Y < m_GrayPal.Entries.Length; Y++) { m_GrayPal.Entries[Y] = Color.FromArgb(255, Y, Y, Y); } } // 设置VFlip,由于SDK输出的数据默认是从底到顶的,打开VFlip后就可以直接转换为Bitmap MvApi.CameraSetMirror(m_hCamera, 1, 1); // 为了演示如何在回调中使用相机数据创建Bitmap并显示到PictureBox中,这里不使用SDK内置的绘制操作 //MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle); MvApi.CameraGrabber_StartLive(m_Grabber); } else { MessageBox.Show(String.Format("打开相机失败,原因:{0}", status)); } }
public void CaptureThreadProc() { CameraSdkStatus eStatus; tSdkFrameHead FrameHead; uint uRawBuffer;//rawbuffer由SDK内部申请。应用层不要调用delete之类的释放函数 while (m_bExitCaptureThread == false) { //500毫秒超时,图像没捕获到前,线程会被挂起,释放CPU,所以该线程中无需调用sleep eStatus = MvApi.CameraGetImageBuffer(m_hCamera, out FrameHead, out uRawBuffer, 500); if (eStatus == CameraSdkStatus.CAMERA_STATUS_SUCCESS)//如果是触发模式,则有可能超时 { //图像处理,将原始输出转换为RGB格式的位图数据,同时叠加白平衡、饱和度、LUT等ISP处理。 MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBuffer, ref FrameHead); //叠加十字线、自动曝光窗口、白平衡窗口信息(仅叠加设置为可见状态的)。 MvApi.CameraImageOverlay(m_hCamera, m_ImageBuffer, ref FrameHead); //调用SDK封装好的接口,显示预览图像 MvApi.CameraDisplayRGB24(m_hCamera, m_ImageBuffer, ref FrameHead); //成功调用CameraGetImageBuffer后必须释放,下次才能继续调用CameraGetImageBuffer捕获图像。 MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer); if (FrameHead.iWidth != m_tFrameHead.iWidth || FrameHead.iHeight != m_tFrameHead.iHeight) { m_bEraseBk = true; m_tFrameHead = FrameHead; } m_iDisplayedFrames++; } } }
private void Form1_Load(object sender, EventArgs e) { //1打开窗口、赋值线程状态 //2枚举设备 //3.初始化设备 //3.申请内存 获取特性 获取位数 //4.设置触发模式 //5.开启绑定线程 //6开始采集 //6开始采集(默认连续,不设置采集模式的话) Control.CheckForIllegalCrossThreadCalls = false; HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); //枚举设备 if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera); //初始化第一个相机 //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera, out tCameraCapability); //申请内存 m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); //MvApi.CameraSetTriggerMode(m_hCamera, 0); m_bExitCaptureThread = false; m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc)); m_tCaptureThread.Start(); MvApi.CameraPlay(m_hCamera); } }
private void BtnSnapshot_Click(object sender, EventArgs e) { tSdkFrameHead tFrameHead; IntPtr uRawBuffer;//由SDK中给RAW数据分配内存,并释放 if (m_hCamera <= 0) { return;//相机还未初始化,句柄无效 } if (MvApi.CameraSnapToBuffer(m_hCamera, out tFrameHead, out uRawBuffer, 500) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //此时,uRawBuffer指向了相机原始数据的缓冲区地址,默认情况下为8bit位宽的Bayer格式,如果 //您需要解析bayer数据,此时就可以直接处理了,后续的操作演示了如何将原始数据转换为RGB格式 //并显示在窗口上。 //将相机输出的原始数据转换为RGB格式到内存m_ImageBufferSnapshot中 MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBufferSnapshot, ref tFrameHead); //CameraSnapToBuffer成功调用后必须用CameraReleaseImageBuffer释放SDK中分配的RAW数据缓冲区 //否则,将造成死锁现象,预览通道和抓拍通道会被一直阻塞,直到调用CameraReleaseImageBuffer释放后解锁。 MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer); //更新抓拍显示窗口。 //m_DlgSnapshot.UpdateImage(ref tFrameHead, m_ImageBufferSnapshot); //m_DlgSnapshot.Show(); } }
public void cameraparamset() { if (!cameraopen()) { return; } try { if (exposuretime > 0) { MvApi.CameraSetExposureTime(m_hCamera, exposuretime); } } catch { } try { if (gain > 0) { MvApi.CameraSetGain(m_hCamera, Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100)); } } catch { } try { if (contrast > 0) { MvApi.CameraSetContrast(m_hCamera, contrast); } } catch { } try { if (gamma > 0) { MvApi.CameraSetGamma(m_hCamera, Convert.ToInt32(gamma)); } } catch { } //try{if (exposuretime > 0) hfg.SetFramegrabberParam("ExposureTime", exposuretime);}catch{} //try{if (gain > 0) hfg.SetFramegrabberParam("Gain", gain / 100);}catch{} //try{if (contrast > 0) hfg.SetFramegrabberParam("Contrast", contrast);}catch{} //try{if (gamma > 0) hfg.SetFramegrabberParam("Gamma", gamma);}catch { } }
public void cameraparam() { if (!cameraopen()) { return; } try { double ept = 0.0; int rgain = 0; int ggain = 0; int bgain = 0; int ctt = 0; int gm = 0; MvApi.CameraGetExposureTime(m_hCamera, ref ept); MvApi.CameraGetGain(m_hCamera, ref rgain, ref ggain, ref bgain); MvApi.CameraGetContrast(m_hCamera, ref ctt); MvApi.CameraGetGamma(m_hCamera, ref gm); exposuretime = ept; gain = rgain; contrast = ctt; gamma = gm; //exposuretime = hfg.GetFramegrabberParam("ExposureTime"); //gain = hfg.GetFramegrabberParam("Gain")*100; //gamma = hfg.GetFramegrabberParam("Gamma"); //contrast = hfg.GetFramegrabberParam("Contrast"); } catch { } }
public void Record_the_picture(uint i) { tSdkFrameHead tFrameHead; uint uRawBuffer;//由SDK中给RAW数据分配内存,并释放 //Bitmap image = new Bitmap(10, 10); //image.Save(save_path+'\\'+"sampe_" + i.ToString()+ '_' + DateTime.Now.ToString("hh_mm_ss") + ".jpg", ImageFormat.Jpeg); if (m_hCamera <= 0) { return;//相机还未初始化,句柄无效 } if (MvApi.CameraSnapToBuffer(m_hCamera, out tFrameHead, out uRawBuffer, 500) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //此时,uRawBuffer指向了相机原始数据的缓冲区地址,默认情况下为8bit位宽的Bayer格式,如果 //您需要解析bayer数据,此时就可以直接处理了,后续的操作演示了如何将原始数据转换为RGB格式 //并显示在窗口上。 //将相机输出的原始数据转换为RGB格式到内存m_ImageBufferSnapshot中 MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBufferSnapshot, ref tFrameHead); //CameraSnapToBuffer成功调用后必须用CameraReleaseImageBuffer释放SDK中分配的RAW数据缓冲区 //否则,将造成死锁现象,预览通道和抓拍通道会被一直阻塞,直到调用CameraReleaseImageBuffer释放后解锁。 MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer); //更新抓拍显示窗口。 Bitmap image1 = new Bitmap(convertImage(ref tFrameHead, m_ImageBufferSnapshot)); image1.Save(save_path + '\\' + "sampe_" + i.ToString() + '_' + DateTime.Now.ToString("HH_mm_ss") + ".jpg", ImageFormat.Jpeg); } }
public Form1() { InitializeComponent(); m_FrameCallback = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback); tSdkCameraDevInfo[] DevList; MvApi.CameraEnumerateDevice(out DevList); int NumDev = (DevList != null ? DevList.Length : 0); if (NumDev < 1) { MessageBox.Show("未扫描到相机"); } else if (NumDev == 1) { MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]); } else { MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber); } if (m_Grabber != IntPtr.Zero) { MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo); MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera); MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0); MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero); MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle); MvApi.CameraGrabber_StartLive(m_Grabber); } }
//设置相机半自动模式下的闪光灯脉冲宽度。注意单位为微秒。 private void button_SetStrobePulseWidth_Click(object sender, EventArgs e) { int iWidth; iWidth = System.Convert.ToInt32(textBox_StrobePulseWidth.Text); MvApi.CameraSetStrobePulseWidth(m_hCamera, (uint)iWidth); }
private void buttonSnap4_Click(object sender, EventArgs e) { if (m_Grabber[3] != IntPtr.Zero) { MvApi.CameraGrabber_SaveImageAsync(m_Grabber[3]); } }
private void buttonStop4_Click(object sender, EventArgs e) { if (m_Grabber[3] != IntPtr.Zero) { MvApi.CameraGrabber_StopLive(m_Grabber[3]); } }
private void UpdateExpsoureControls() { uint uState = 0; MvApi.CameraGetAeState(m_hCamera, ref uState); if (uState == 1) { textBox_AnalogGain.Enabled = false; textBox_ExposureTime.Enabled = false; trackBar_AnalogGain.Enabled = false; } else { textBox_AnalogGain.Enabled = true; textBox_ExposureTime.Enabled = true; trackBar_AnalogGain.Enabled = true; int iAnalogGain = 0; MvApi.CameraGetAnalogGain(m_hCamera, ref iAnalogGain); trackBar_AnalogGain.Value = iAnalogGain; textBox_AnalogGain.Text = iAnalogGain.ToString(); double dCameraExpTime = 0; MvApi.CameraGetExposureTime(m_hCamera, ref dCameraExpTime); textBox_ExposureTime.Text = dCameraExpTime.ToString(); } }
private void buttonPlay3_Click(object sender, EventArgs e) { if (m_Grabber[2] != IntPtr.Zero) { MvApi.CameraGrabber_StartLive(m_Grabber[2]); } }
private void buttonSettings4_Click(object sender, EventArgs e) { if (m_Grabber[3] != IntPtr.Zero) { MvApi.CameraShowSettingPage(m_hCamera[3], 1); } }
//保存相机参数到指定的文件 private void button_SaveParamToFile_Click(object sender, EventArgs e) { string FileName = "c:\\camera.config";//保存参数的路径和文件可以修改,但是后缀必须是config结尾。 MvApi.CameraSaveParameterToFile(m_hCamera, FileName); MessageBox.Show("参数保存成功:" + FileName); }
private void CameraGrabberSaveImageComplete2( IntPtr Grabber, IntPtr Image, // 需要调用CameraImage_Destroy释放 CameraSdkStatus Status, IntPtr Context) { if (Image != IntPtr.Zero) { tSdkCameraDevInfo devInfo; MvApi.CameraGrabber_GetCameraDevInfo(Grabber, out devInfo); Encoding myEncoding = Encoding.GetEncoding("utf-8"); string sData = myEncoding.GetString(devInfo.acSn); sData = sData.TrimEnd('\0'); sData = sData.Substring(0, 12); string filename = System.IO.Path.Combine( AppDomain.CurrentDomain.BaseDirectory.ToString(), string.Format("{0}-{1}-{2}.jpg", System.Environment.TickCount, sData, devInfo.uInstance)); MvApi.CameraImage_SaveAsJpeg(Image, filename, 90); m_saveFilenames += filename + "\r\n"; m_countFiles++; } if (m_countFiles == CAMERA_NUM) { MessageBox.Show(m_saveFilenames); } MvApi.CameraImage_Destroy(Image); }
private void BtnSnapshot_Click(object sender, EventArgs e) { tSdkFrameHead tFrameHead; uint uRawBuffer;//由SDK中给RAW数据分配内存,并释放 if (m_hCamera <= 0) { return;//相机还未初始化,句柄无效 } if (MvApi.CameraSnapToBuffer(m_hCamera, out tFrameHead, out uRawBuffer, 500) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //此时,uRawBuffer指向了相机原始数据的缓冲区地址,默认情况下为8bit位宽的Bayer格式,如果 //您需要解析bayer数据,此时就可以直接处理了,后续的操作演示了如何将原始数据转换为RGB格式 //并显示在窗口上。 //将相机输出的原始数据转换为RGB格式到内存m_ImageBufferSnapshot中 MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBufferSnapshot, ref tFrameHead); //CameraSnapToBuffer成功调用后必须用CameraReleaseImageBuffer释放SDK中分配的RAW数据缓冲区 //否则,将造成死锁现象,预览通道和抓拍通道会被一直阻塞,直到调用CameraReleaseImageBuffer释放后解锁。 MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer); //更新抓拍显示窗口。 Bitmap image1 = new Bitmap(convertImage(ref tFrameHead, m_ImageBufferSnapshot)); saveFileDialog1.Filter = "JPEG files (*.jpg)|*.jpg"; if (saveFileDialog1.ShowDialog() == DialogResult.OK)//Save setting file { image1.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); } } }
//恢复相机默认参数 private void button_DefaultParam_Click(object sender, EventArgs e) { MvApi.CameraLoadParameter(m_hCamera, (int)emSdkParameterTeam.PARAMETER_TEAM_DEFAULT); UpdateControls(); this.Refresh(); MessageBox.Show("相机已经恢复默认参数"); }
private void BasicForm_FormClosing(object sender, FormClosingEventArgs e) { for (int i = 0; i < CAMERA_NUM; i++) { tpvCameraObj cameraObj = cameraObjs[i]; //相机1反初始化 if (cameraObj.m_tCaptureThread != null) { cameraObj.m_bExitCaptureThread = true; cameraObj.m_tCaptureThread.Join(); cameraObj.m_tCaptureThread = null; } if (cameraObj.m_hCamera != 0) { MvApi.CameraUnInit(cameraObj.m_hCamera); cameraObj.m_hCamera = 0; } if (cameraObj.m_ImageBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(cameraObj.m_ImageBuffer); cameraObj.m_ImageBuffer = IntPtr.Zero; } } }
private void BtnSettings_Click(object sender, EventArgs e) { if (m_hCamera > 0) { MvApi.CameraShowSettingPage(m_hCamera, 1);//1 show ; 0 hide } }
//设置相机的采集模式,分为连续、软触发和硬触发3种模式。无论哪种模式,采集图像都是同一个接口。 private void comboBox_TriggerMode_SelectedIndexChanged(object sender, EventArgs e) { int iMode = comboBox_TriggerMode.SelectedIndex; MvApi.CameraSetTriggerMode(m_hCamera, iMode);//0表示连续模式,1是软触发,2是硬触发。 button_SwTrigger.Enabled = (iMode == 1 ? true : false); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (m_bRecording) { MvApi.CameraStopRecord(m_hCamera); } MvApi.CameraGrabber_Destroy(m_Grabber); }
//button1_Click 用于彩色相机的一键白平衡。 private void button1_Click(object sender, EventArgs e) { MvApi.CameraSetOnceWB(m_hCamera); //跟新RGB控件上的值 UpdateRgbGainControls(); this.Invalidate(); }
//从指定文件中加载相机参数 private void button_LoadParamFromeFIle_Click(object sender, EventArgs e) { string FileName = "c:\\camera.config"; MvApi.CameraReadParameterFromFile(m_hCamera, FileName); MessageBox.Show("参数加载成功:" + FileName); UpdateControls(); this.Refresh(); }
//设置为预设分辨率 private void radioButton_ResolutionPreset_CheckedChanged(object sender, EventArgs e) { tSdkImageResolution t; MvApi.CameraGetImageResolution(m_hCamera, out t); t.iIndex = m_iResolutionIndex;//切换预设分辨率, 只需要设定index值就行了。 其余的值可忽略,或者填0 MvApi.CameraSetImageResolution(m_hCamera, ref t); UpdateResolution(); }
public Default() { InitializeComponent(); if (InitCamera() == true) { MvApi.CameraPlay(m_hCamera); BtnPlay.Text = "Pause"; } }
public void SetCameraAttris(int cameraIdx) { Debug.Assert(cameraIdx >= 0 && cameraIdx < 4); if (m_Grabber[cameraIdx] != IntPtr.Zero) { MvApi.CameraShowSettingPage(m_hCamera[cameraIdx], 1); } }
public void CaptureThreadProc() { CameraSdkStatus eStatus; tSdkFrameHead pFrameHead; IntPtr uRawBuffer;//rawbuffer由SDK内部申请。应用层不要调用delete之类的释放函数 while (m_bExitCaptureThread == false) { //500毫秒超时,图像没捕获到前,线程会被挂起,释放CPU,所以该线程中无需调用sleep eStatus = MvApi.CameraGetImageBuffer(m_hCamera, out pFrameHead, out uRawBuffer, 5000); if (eStatus == CameraSdkStatus.CAMERA_STATUS_SUCCESS)//如果是触发模式,则有可能超时 { MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBuffer, ref pFrameHead); MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer); //使用halcon显示 int bytewidth = (pFrameHead.iWidth * 3 + 3) / 4 * 4; //要设置的RGB图 的宽度 保证其是4的倍数 int bytewidthg = (pFrameHead.iWidth + 3) / 4 * 4; //要设置分量图 //实例化申请内存空间 byte[] m_pImageData = new byte[bytewidth * pFrameHead.iHeight]; byte[] m_pImageDataR = new byte[pFrameHead.iWidth * pFrameHead.iHeight]; byte[] m_pImageDataG = new byte[pFrameHead.iWidth * pFrameHead.iHeight]; byte[] m_pImageDataB = new byte[pFrameHead.iWidth * pFrameHead.iHeight]; //复制一张包含RGB的图像缓存(将RGB图像转换成Byte图像缓存) RGB图像缓存,图像byte的图像类型,相机的索引,图像的字节数大小 Marshal.Copy(m_ImageBuffer, m_pImageData, 0, pFrameHead.iWidth * pFrameHead.iHeight * 3); for (int j = 0; j < pFrameHead.iHeight; j++) { for (int i = 0; i < pFrameHead.iWidth; i++) { //将图像的RGB分量 分别分给R分量图,G分量图,B分量图 m_pImageDataB[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 0]; m_pImageDataG[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 1]; m_pImageDataR[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 2]; // m_pImageDatagray[j * bytewidthg + i] = (byte)(0.11 * m_pImageData[j * bytewidth + i * 3 + 0] + 0.59 * m_pImageData[j * bytewidth + i * 3 + 1] + 0.30 * m_pImageData[j * bytewidth + i * 3 + 2]); } } unsafe { fixed(byte *pR = m_pImageDataR, pB = m_pImageDataB, pG = m_pImageDataG) { HOperatorSet.GenImage3(out halcon_image, "byte", pFrameHead.iWidth, pFrameHead.iHeight, new IntPtr(pR), new IntPtr(pB), new IntPtr(pG)); } } //获得图像宽高,然后设置显示图像的宽高 HOperatorSet.GetImageSize(halcon_image, out hv_Width, out hv_Height); HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width); //如果图像反向可以用halcon镜像图像的函数 将图像镜像反转一下 //HOperatorSet.MirrorImage(halcon_image, out halcon_image, "row"); HOperatorSet.DispObj(halcon_image, hv_WindowHandle); halcon_image.Dispose(); } } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { for (int i = 0; i < 4; ++i) { if (m_Grabber[i] != IntPtr.Zero) { MvApi.CameraGrabber_Destroy(m_Grabber[i]); } } }