110,545
社区成员
发帖
与我相关
我的任务
分享
// Create a camera object that selects the first camera device found.
// More constructors are available for selecting a specific camera device.
using (Camera camera = new Camera())
{
// Print the model name of the camera.
camera_info.Text=camera.CameraInfo[CameraInfoKey.ModelName];
// Set the acquisition mode to free running continuous acquisition when the camera is opened.
camera.CameraOpened += Configuration.AcquireContinuous;
// Open the connection to the camera device.
camera.Open();
// The parameter MaxNumBuffer can be used to control the amount of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.Parameters[PLCameraInstance.MaxNumBuffer].SetValue(5);
// Start grabbing.
camera.StreamGrabber.Start();
// Grab a number of images.
for (int i = 0; i < 10; ++i)
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
using (grabResult)
{
// Image grabbed successfully?
if (grabResult.GrabSucceeded)
{
ImagePersistence.Save(ImageFileFormat.Bmp, "C:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp", grabResult);
// Access the image data.
buffer = grabResult.PixelData as byte[];
// Display the grabbed image.
//ImageWindow.DisplayImage(0, grabResult);
}
else
{
MessageBox.Show(grabResult.ErrorCode+","+grabResult.ErrorDescription);
}
}
}
//将获得的图片转换为Halcon对象
HImageConvertFromBytes(buffer);
// Stop grabbing.
camera.StreamGrabber.Stop();
// Close the connection to the camera device.
camera.Dispose();
camera.Close();
}
try
{
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResultLeft;
mCamera->RetrieveResult( 5000, ptrGrabResultLeft, TimeoutHandling_ThrowException);//获取结果
// Image grabbed successfully?
if (ptrGrabResultLeft->GrabSucceeded())
{
// Access the image data.
const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResultLeft->GetBuffer();//获取图像在内存中的地址
GenImage1(pDstImg,"byte",(Hlong)ptrGrabResult->GetWidth(),(Hlong)ptrGrabResult->GetHeight(),(Hlong)pImageBuffer);//直接用halcon算子转化为HObject 格式 (此算子只适用单个通道)
}
}
catch (...)
{
return FALSE;
}
return TRUE;