#include "ApiController.h" #include "FrameObserver.h" #include #include namespace AVT { namespace VmbAPI { namespace Examples { ApiController::ApiController() : m_system(VimbaSystem::GetInstance()), m_nWidth(0), m_nHeight(0), m_nPixelFormat(0) { } ApiController::~ApiController() { if (m_pCamera) { StopContinuousImageAcquisition(); } } VmbErrorType ApiController::StartUp() { return m_system.Startup(); } void ApiController::ShutDown() { m_system.Shutdown(); } VmbErrorType ApiController::StartContinuousImageAcquisition(const std::string &rStrCameraID) { VmbErrorType res = m_system.OpenCameraByID(rStrCameraID.c_str(), VmbAccessModeFull, m_pCamera); if (VmbErrorSuccess == res) { std::cerr << "StartContinuousImageAcquisition failed with error code: " << res << std::endl; // 可以添加更多错误信息 if (res == VmbErrorApiNotStarted) { std::cerr << "Vimba API not started" << std::endl; } else if (res == VmbErrorInvalidAccess) { std::cerr << "Invalid access to camera" << std::endl; } FeaturePtr pFormatFeature; res = m_pCamera->GetFeatureByName("PixelFormat", pFormatFeature); if (VmbErrorSuccess == res) { VmbInt64_t pixelFormat; res = pFormatFeature->GetValue(pixelFormat); std::cout << "Camera Pixel Format: " << pixelFormat << std::endl; } } // Optional: Adjust packet size for GigE cameras FeaturePtr pCommandFeature; if (VmbErrorSuccess == m_pCamera->GetFeatureByName("GVSPAdjustPacketSize", pCommandFeature)) { pCommandFeature->RunCommand(); } // Set camera parameters res = SetValueIntMod2(m_pCamera, "Width", m_nWidth); if (VmbErrorSuccess == res) { res = SetValueIntMod2(m_pCamera, "Height", m_nHeight); } if (VmbErrorSuccess == res) { FeaturePtr pFormatFeature; res = m_pCamera->GetFeatureByName("PixelFormat", pFormatFeature); if (VmbErrorSuccess == res) { res = pFormatFeature->GetValue(m_nPixelFormat); } } // Start acquisition with FrameObserver that streams to V4L2 if (VmbErrorSuccess == res) { SP_SET(m_pFrameObserver, new FrameObserver(m_pCamera)); res = m_pCamera->StartContinuousImageAcquisition(3, m_pFrameObserver); } if (VmbErrorSuccess != res && m_pCamera) { m_pCamera->Close(); } return res; } VmbErrorType ApiController::StopContinuousImageAcquisition() { if (m_pCamera) { m_pCamera->StopContinuousImageAcquisition(); return m_pCamera->Close(); } return VmbErrorSuccess; } ApiController::CameraPtrVector ApiController::GetCameraList() { CameraPtrVector cameras; m_system.GetCameras(cameras); return cameras; } // Modified to return nullptr since we're streaming directly to V4L2 ApiController::FramePtr ApiController::GetFrame() { return FramePtr(); // Not supported in this mode } VmbErrorType ApiController::QueueFrame(FramePtr pFrame) { if (!m_pCamera) { return VmbErrorDeviceNotOpen; } return m_pCamera->QueueFrame(pFrame); } // Modified to do nothing since FrameObserver handles streaming void ApiController::ClearFrameQueue() { // No operation needed as frames are directly streamed to V4L2 } int ApiController::GetWidth() const { return static_cast(m_nWidth); } int ApiController::GetHeight() const { return static_cast(m_nHeight); } VmbPixelFormatType ApiController::GetPixelFormat() const { return static_cast(m_nPixelFormat); } VmbErrorType ApiController::SetValueIntMod2(const AVT::VmbAPI::CameraPtr &camera, const std::string &featureName, VmbInt64_t &storage) { AVT::VmbAPI::FeaturePtr pFeature; VmbInt64_t min = 0, max = 0, inc = 0; VmbErrorType res = camera->GetFeatureByName(featureName.c_str(), pFeature); if (VmbErrorSuccess == res) res = pFeature->GetRange(min, max); if (VmbErrorSuccess == res) res = pFeature->GetIncrement(inc); if (VmbErrorSuccess == res) { max = max - (max % inc); if (max % 2 != 0) max -= inc; res = pFeature->SetValue(max); storage = max; } return res; } }}} // namespace AVT::VmbAPI::Examples