#include <iostream>
#include"SapClassBasic.h"
#include <SapBuffer.h>
#include<conio.h>
#include <string>
#include <vector>
#include <io.h>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <QImage>
using namespace std;
SapManager* m_pManager;
SapAcquisition* m_Acquisition;
SapBufferWithTrash* m_Buffers;
SapTransfer* m_Xfer;
SapView* m_View;
SapAcqDevice *m_AcqDevice;
static void XferCallBack(SapXferCallbackInfo* pInfo)
{
m_View->Show();
}
bool initDevice(char* m_serverName, const char*ccfpath)
{
SapLocation loc(m_serverName, 0);//初始化采集卡
m_Acquisition = new SapAcquisition(loc, ccfpath);
m_Buffers = new SapBufferWithTrash(3, m_Acquisition);
m_Xfer = new SapAcqToBuf(m_Acquisition, m_Buffers, XferCallBack, m_View);
m_View = new SapView(m_Buffers, SapHwndAutomatic);
if (m_Acquisition && !*m_Acquisition && !m_Acquisition->Create()) return FALSE;
if (m_Buffers && !*m_Buffers)
{
if (!m_Buffers->Create())
{
return FALSE;
}
m_Buffers->Clear();
}
if (m_View && !*m_View && !m_View->Create())
{
return FALSE;
}
// Set next empty with trash cycle mode for transfer
if (m_Xfer && m_Xfer->GetPair(0))
{
if (!m_Xfer->GetPair(0)->SetCycleMode(SapXferPair::CycleNextWithTrash))
{
return FALSE;
}
}
// Create transfer object
if (m_Xfer && !*m_Xfer && !m_Xfer->Create())
{
return FALSE;
}
return true;
}
int main(int argc, char** argv)
{
char* m_SerName = new char[MAX_PATH];//采集卡名称
SapBuffer sapBuffer;
int framcount = 0;
int flag = 0;
string ccf_path = "D:\\test\\ds\\ds\\2023_06_29.ccf";
//vector<cv::Mat> m_Mats;
BYTE *pData;
uchar *imgData = new uchar[150 * 100/1.75*7000];
cv::Mat m_Mat_all = cv::Mat::zeros(cv::Size(700,85700), CV_8U);
int SerCount = m_pManager->GetServerCount(SapManager::ResourceAcq);
m_pManager->GetServerName(0, SapManager::ResourceAcq, m_SerName);//获取采集卡名称
m_pManager->GetServerType(m_SerName);//获取采集卡类型
//初始化设备
if (initDevice(m_SerName, ccf_path.c_str()))
{
cout << "Open " << m_SerName << " Success!" << endl;
}
else
{
cout << "Open " << m_SerName << " Failed!" << endl;
}
//开始采集
if (!m_Xfer->IsGrabbing())
{
m_Xfer->Grab();
flag = 1;
m_Buffers->GetAddress((void**)&pData);
//int width = m_Buffers->GetWidth();
//int height = m_Buffers->GetHeight();
}
while (true)
{
if (flag == 1)
{
std::cout << "Grab" << std::endl;
//Sleep(200);
std::stringstream ss;
ss << "D:\\test\\ds\\ds\\bmp1\\" << framcount << ".bmp";
std::string name = ss.str();
const char* savename = name.c_str();
//cv::Mat m_Mat = cv::Mat::zeros(cv::Size(640, 480), CV_8U);
//QImage image(640, 480, QImage::Format_Grayscale8);
//m_Xfer->Freeze();
//memcpy(m_Mat.data, pData, 640 * 480);
memcpy(imgData + framcount*7000,pData,7000);
//cv::imwrite(name,m_Mat);
//memcpy(image.bits(), pData, 640 * 480);
//image.save(savename);
//m_Buffers->Save(savename, "-format bmp");
//m_Buffers->Clear();
//m_Xfer->Grab();
//m_Buffers->Save(savename, "-format bmp");
//m_Mats.push_back(m_Mat);
framcount++;
if (framcount > int(150 * 100 / 1.75 - 1))
{
framcount = 0;
cout << "Grab Finished" << endl;//停止采集
m_Xfer->Freeze();
for (int i = 0; i < int(150 * 100 / 1.75 - 1); i++)
{
cv::Rect roiRect(0, 10 * i,700, 10);
cv::Mat img = cv::Mat::zeros(cv::Size(700,10), CV_8U);
memcpy(img.data, imgData + i * 7000,7000);
//std::stringstream ss;
//ss << "D:\\test\\ds\\ds\\bmp1\\" << i << ".bmp";
//std::string name = ss.str();
//cv::imwrite(name, img);
cv::Mat roi = m_Mat_all(roiRect);
img.copyTo(roi);
cv::imshow("1", m_Mat_all);
cv::waitKey(0);
}
cv::imwrite("test.bmp", m_Mat_all);
break;
}
}
}
delete imgData;
// Destroy transfer object
if (m_Xfer && *m_Xfer) m_Xfer->Destroy();
if (m_View && *m_View) m_View->Destroy();
if (m_Buffers && *m_Buffers) m_Buffers->Destroy();
if (m_Acquisition && *m_Acquisition) m_Acquisition->Destroy();
//Delete all pointer
if (m_View) delete m_View;
if (m_Buffers) delete m_Buffers;
if (m_Xfer) delete m_Xfer;
if (m_Acquisition) delete m_Acquisition;
}
要用回调函数取图:
#include <iostream>
#include"SapClassBasic.h"
#include <SapBuffer.h>
#include<conio.h>
#include <string>
#include <vector>
#include <io.h>
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace std;
SapManager* m_pManager;
SapAcquisition* m_Acquisition;
SapBufferWithTrash* m_Buffers;
SapTransfer* m_Xfer;
SapView* m_View;
SapAcqDevice *m_AcqDevice;
BYTE *pData;
int framcount = 1;
static void XferCallBack(SapXferCallbackInfo* pInfo)
{
cv::Mat m_Mat = cv::Mat::zeros(cv::Size(640, 480), CV_8U);
memcpy(m_Mat.data, pData, 640 * 480);
std::stringstream ss;
ss << "D:\\test\\ds\\ds\\bmp\\" << framcount << ".bmp";
std::string name = ss.str();
cv::imwrite(name,m_Mat);
framcount++;
//Sleep(100);
//m_View->Show();
}
bool initDevice(char* m_serverName, const char*ccfpath)
{
SapLocation loc(m_serverName, 0);//初始化采集卡
m_Acquisition = new SapAcquisition(loc, ccfpath);
m_Buffers = new SapBufferWithTrash(3, m_Acquisition);
m_Xfer = new SapAcqToBuf(m_Acquisition, m_Buffers, XferCallBack, m_View);
m_View = new SapView(m_Buffers, SapHwndAutomatic);
if (m_Acquisition && !*m_Acquisition && !m_Acquisition->Create()) return FALSE;
if (m_Buffers && !*m_Buffers)
{
if (!m_Buffers->Create())
{
return FALSE;
}
m_Buffers->Clear();
}
if (m_View && !*m_View && !m_View->Create())
{
return FALSE;
}
// Set next empty with trash cycle mode for transfer
if (m_Xfer && m_Xfer->GetPair(0))
{
if (!m_Xfer->GetPair(0)->SetCycleMode(SapXferPair::CycleNextWithTrash))
{
return FALSE;
}
}
// Create transfer object
if (m_Xfer && !*m_Xfer && !m_Xfer->Create())
{
return FALSE;
}
return true;
}
int main(int argc, char** argv)
{
char* m_SerName = new char[MAX_PATH];//采集卡名称
SapBuffer sapBuffer;
int flag = 0;
string ccf_path = "D:\\test\\ds\\ds\\2023_06_29.ccf";
vector<cv::Mat> m_Mats;
cv::Mat m_Mat_all = cv::Mat::zeros(cv::Size(640, 4800), CV_8U);
int SerCount = m_pManager->GetServerCount(SapManager::ResourceAcq);
m_pManager->GetServerName(0, SapManager::ResourceAcq, m_SerName);//获取采集卡名称
m_pManager->GetServerType(m_SerName);//获取采集卡类型
cout << "Opening " << m_SerName << endl;
//初始化设备
if (initDevice(m_SerName, ccf_path.c_str()))
{
cout << "Open " << m_SerName << " Success!" << endl;
}
else
{
cout << "Open " << m_SerName << " Failed!" << endl;
}
//开始采集
if (!m_Xfer->IsGrabbing())
{
m_Xfer->Grab();
flag = 1;
m_Buffers->GetAddress((void**)&pData);
//int width = m_Buffers->GetWidth();
//int height = m_Buffers->GetHeight();
}
while (true)
{
if (flag == 1)
{
std::cout << "Grab" << std::endl;
//m_Mats.push_back(m_Mat);
if (framcount > 1000)
{
framcount = 0;
cout << "Grab Finished" << endl;//停止采集
m_Xfer->Freeze();
break;
}
}
}
// Destroy transfer object
if (m_Xfer && *m_Xfer) m_Xfer->Destroy();
if (m_View && *m_View) m_View->Destroy();
if (m_Buffers && *m_Buffers) m_Buffers->Destroy();
if (m_Acquisition && *m_Acquisition) m_Acquisition->Destroy();
//Delete all pointer
if (m_View) delete m_View;
if (m_Buffers) delete m_Buffers;
if (m_Xfer) delete m_Xfer;
if (m_Acquisition) delete m_Acquisition;
}