I am working on connecting a camera feed using MFC in Visual Studio.
After creating a picture box, I made buttons for open, live, and close.
When I press the open button, the camera connects, and the close button disconnects the camera.
The part where I got stuck is implementing the feature to view the live camera feed when I press the live button.
When I press the live button, I get an alert that says “failed to retrieve grab result.”
I don’t know if this is an issue with the ‘Live’ code or if it’s related to the frame capture code.
My ‘Live’ code :
void Cimageviewer12Dlg::OnBnClickedButtonLive()
{
if (!m_isCameraConnected)
{
AfxMessageBox(_T("Camera is not connected. Please connect the camera first."));
return;
}
try
{
if (!InitializeCamera())
{
AfxMessageBox(_T("Failed to initialize camera."));
return;
}
while (true)
{
BYTE* pFrameData = nullptr;
try
{
pFrameData = CaptureFrame();
}
catch (const GenICam_3_1_Basler_pylon::RuntimeException& e)
{
CString errorMsg;
errorMsg.Format(_T("Failed to capture image from camera: %s"), e.what());
AfxMessageBox(errorMsg);
break;
}
if (pFrameData == nullptr)
{
AfxMessageBox(_T("Failed to capture image from camera."));
break;
}
DisplayFrame(pFrameData);
if (GetAsyncKeyState(VK_ESCAPE))
break;
Sleep(30);
}
}
catch (const GenICam_3_1_Basler_pylon::RuntimeException& e)
{
CString errorMsg;
errorMsg.Format(_T("An error occurred: %s"), e.what());
AfxMessageBox(errorMsg);
}
ReleaseCamera();
}
My ‘initializeCamera’ code :
bool Cimageviewer12Dlg::InitializeCamera()
{
Pylon::PylonInitialize(); // SDK 초기화
try
{
// 카메라 장치 열기
auto device = CTlFactory::GetInstance().CreateFirstDevice();
if (device == nullptr)
{
AfxMessageBox(_T("Failed to create a device."));
return false;
}
m_camera.Attach(device);
m_camera.Open();
if (!m_camera.IsOpen())
{
AfxMessageBox(_T("Failed to open the camera."));
return false;
}
}
catch (const Pylon::GenericException &e) // 1. 변수 사용
{
CString errorMessage;
errorMessage.Format(_T("Error: %s"), e.GetDescription());
AfxMessageBox(errorMessage);
return false;
}
return true; // 초기화 성공 여부 반환
}
My ‘CaptureFrame’ code :
BYTE* Cimageviewer12Dlg::CaptureFrame()
{
try
{
if (!m_camera.IsOpen())
{
AfxMessageBox(_T("Camera is not open."));
return nullptr;
}
// 카메라가 이미 캡처를 시작했는지 확인
if (!m_camera.IsGrabbing())
{
m_camera.StartGrabbing();
AfxMessageBox(_T("Started grabbing."));
}
Pylon::CGrabResultPtr ptrGrabResult;
// 예외 처리 강화
m_camera.RetrieveResult(20000, ptrGrabResult, Pylon::TimeoutHandling_ThrowException);
if (!ptrGrabResult)
{
AfxMessageBox(_T("Failed to retrieve grab result."));
return nullptr;
}
if (ptrGrabResult->GrabSucceeded())
{
return (BYTE*)ptrGrabResult->GetBuffer();
}
else
{
CString errorMsg;
errorMsg.Format(_T("Grab failed: %s"), ptrGrabResult->GetErrorDescription());
AfxMessageBox(errorMsg);
return nullptr;
}
}
catch (const Pylon::GenericException &e)
{
CString errorMsg;
errorMsg.Format(_T("An exception occurred: %s"), CString(e.GetDescription()));
AfxMessageBox(errorMsg);
return nullptr;
}
catch (...)
{
AfxMessageBox(_T("An unexpected error occurred."));
return nullptr;
}
}
I can confirm that there was no problem connecting the camera through the Pylon Viewer app.
If there’s anything I’m missing, I’d appreciate it if you could let me know.
Sanghyeon Woo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3