OpenCV 筆記 4 控制兩個相機 Class化

Camera控制的延續: https://dotblogs.com.tw/April_Notes/2025/01/06/093151

DLL製作的相關背景: https://dotblogs.com.tw/April_Notes/2025/01/07/095021

 

 

DLL的: CameraLibrary.h

#pragma once
#include <opencv2/opencv.hpp>
#ifdef CAMERALIBRARY_EXPORTS
#define CAMERALIBRARY_API __declspec(dllexport)
#else
#define CAMERALIBRARY_API __declspec(dllimport)
#endif




using namespace std;
using namespace cv;

class CAMERALIBRARY_API CameraRGB_Control {
public:
    CameraRGB_Control(int deviceID);
    bool VideoCapture_Open();
    void Auto_Exposure_OnOff(bool TurnOn = true, int exposure_number = 0);
    bool Check_VideoCapture_read();

    void Camera_image_show(string title = "live");
    int Camera_image_cols();
    int Camera_image_rows();
    
    string Version();
private:
    Mat frame;
    VideoCapture cap;

    vector<vector<int>> DataR, DataG, DataB;

    int CameraID = 0;
    
};

DLL的: CameraLibrary.cpp

#include "pch.h" 
#include <utility>
#include <limits.h>
#include <opencv2/opencv.hpp>
#include "CameraLibrary.h"

using namespace std;
using namespace cv;




CameraRGB_Control::CameraRGB_Control(int deviceID)
{
    CameraID = deviceID;
}

string CameraRGB_Control::Version()
{
    return "Version 2025/01/06 \n";
}

//////////////////////////////////////

bool CameraRGB_Control::VideoCapture_Open()
{
    cap.open(CameraID);
    return cap.isOpened();
}

int CameraRGB_Control::Camera_CameraID()
{
    return CameraID;
}


void CameraRGB_Control::Auto_Exposure_OnOff(bool TurnOn , int exposure_number)
{
    int auto_mode = 3;
    int manual_mode = 1;
    cap.set(cv::CAP_PROP_AUTO_EXPOSURE, auto_mode);
    if (!TurnOn)
    {
        cap.set(cv::CAP_PROP_AUTO_EXPOSURE, manual_mode);
        cap.set(cv::CAP_PROP_EXPOSURE, exposure_number);
    }
}


bool CameraRGB_Control::Check_VideoCapture_read()
{
    return cap.read(frame);
}

void CameraRGB_Control::Camera_image_show(string title)
{
    imshow("live", frame);
}

int CameraRGB_Control::Camera_image_cols()
{
    return frame.cols;
}

int CameraRGB_Control::Camera_image_rows()
{
    return frame.rows;
}



 

測試的範例: 

#include <iostream>
#include <vector>
#include "CameraLibrary.h"


int main()
{
    std::cout << "0";
	CameraRGB_Control CameraRGB_state0(0);
	CameraRGB_Control CameraRGB_state1(1);

    if (!CameraRGB_state0.VideoCapture_Open()) std::cout << "Open Error";
    if (!CameraRGB_state1.VideoCapture_Open()) std::cout << "Open Error";

    std::cout << "1";


	CameraRGB_state0.Auto_Exposure_OnOff(true);
	CameraRGB_state1.Auto_Exposure_OnOff(false, -5);

    std::cout << "2";

	int Camera_Number = CameraRGB_state0.Camera_CameraID();

    std::cout << "3";

    while (true)
    {
    
        if (Camera_Number == CameraRGB_state0.Camera_CameraID())
        {
            CameraRGB_state0.Check_VideoCapture_read();
            CameraRGB_state0.Camera_image_show();
        }
        else
        {
            CameraRGB_state1.Check_VideoCapture_read();
            CameraRGB_state1.Camera_image_show();
        }

        
    }
	std::cin.get();
}

 

這樣就可以測試了