Scanner: 扫描流程开启采集控制代码

MultiFrameCapture 增加模拟数据传输接口实现,读取图像路径为/home/root/color.jpg 或/home/root/gray.jpg
This commit is contained in:
lovelyyoung 2023-07-18 10:29:03 +08:00
parent 70354ab6cc
commit 355b44018a
3 changed files with 137 additions and 120 deletions

View File

@ -1,6 +1,6 @@
#include "MultiFrameCapture.h"
#include <thread>
//#include <opencv2/opencv.hpp>
// #include <opencv2/opencv.hpp>
#include "DevUtil.h"
#include "Gpio.h"
#include "FpgaComm.h"
@ -20,23 +20,25 @@
const int vsp_A = 45;
const int vsp_B = 45;
//using namespace cv;
// using namespace cv;
MultiFrameCapture::MultiFrameCapture(ScannerGlue glue,
std::shared_ptr<FpgaComm> fpga,
CISVendor vendor) :
reset_pin(new GpioOut(Fpga_Reset)),
fpgaLoad(new Gpio(Fpga_Load)),
fpga_conf_initn(new Gpio(Fpga_InitN)),
snaped_index(0),
frame_count(1),
b_snap_run(true),
b_stop_snap(false),
b_imgproc(true),
m_glue(glue),
pimgdata_info({0}),
iImageremain(0),
bScanning(false)
std::shared_ptr<FpgaComm> fpga,
CISVendor vendor) : reset_pin(new GpioOut(Fpga_Reset)),
fpgaLoad(new Gpio(Fpga_Load)),
fpga_conf_initn(new Gpio(Fpga_InitN)),
snaped_index(0),
frame_count(1),
b_snap_run(true),
b_stop_snap(false),
b_imgproc(true),
m_glue(glue),
pimgdata_info({0}),
iImageremain(0),
bScanning(false)
#ifdef TEST_SIMCAP
,
m_test_pimg(nullptr), m_test_pimg_len(0)
#endif
{
m_capFpageregs = fpga;
video.reset(new HCamDevice);
@ -49,6 +51,7 @@ MultiFrameCapture::MultiFrameCapture(ScannerGlue glue,
// fpgaLoad->setValue(Gpio::High);
reset_fpga();
m_snap_thread.reset(new std::thread(&MultiFrameCapture::snaprun, this));
m_imgproc_thread.reset(new std::thread(&MultiFrameCapture::procimage, this));
}
@ -76,17 +79,21 @@ MultiFrameCapture::~MultiFrameCapture()
free(pimgdata_info.pdata);
pimgdata_info.pdata = nullptr;
}
#ifdef TEST_SIMCAP
if (m_test_pimg)
free(m_test_pimg);
#endif
}
void MultiFrameCapture::SetParent(void *scanner)
{
}
void MultiFrameCapture::open()
void MultiFrameCapture::open_video()
{
//reset_fpga();
// reset_fpga();
m_capFpageregs->resetADC();
m_capFpageregs->set_cis_type(false);//适配A4 CIS
m_capFpageregs->set_cis_type(false); // 适配A4 CIS
const bool dunnancis = true;
is_size_error = false;
// m_frames.clear();
@ -96,7 +103,7 @@ void MultiFrameCapture::open()
if (((PaperSize)m_config.params.pageSize != PaperSize::G400_AUTO) && ((PaperSize)m_config.params.pageSize != PaperSize::G400_MAXAUTO))
{
LOG("init CSizedetect \n");
//m_preproclist.push_back(shared_ptr<IPreproc>(new CSizedetect(m_config.params.pageSize, m_config.params.dpi)));
// m_preproclist.push_back(shared_ptr<IPreproc>(new CSizedetect(m_config.params.pageSize, m_config.params.dpi)));
}
}
int dpi = m_config.params.dpi == 0x02 ? 2 : (m_config.params.dpi == 0x03 ? 3 : 2);
@ -114,7 +121,7 @@ void MultiFrameCapture::open()
unsigned int sp_dst = m_fpgaparam.Sp;
if (m_config.params.pageSize == 17 || m_config.params.pageSize == 19)
{
sp_dst = mode == 1 ? 4144 : 12432; //sp_dst = mode == 1 ? 3327 : 9981;
sp_dst = mode == 1 ? 4144 : 12432; // sp_dst = mode == 1 ? 3327 : 9981;
tdpi = 300;
}
pixheight = ((int)((phyHeight / 25.4 * tdpi + 2) / 3)) * 3;
@ -125,13 +132,13 @@ void MultiFrameCapture::open()
printf("########## pixheight = %d phyHeight = %d m_config.params.pageSize %d dpi = %d\n", pixheight, phyHeight, m_config.params.pageSize, dpi);
frame_height = 300 * channels;
//frame_height = (dpi > 2 ? 300 : 180) * channels;
// frame_height = (dpi > 2 ? 300 : 180) * channels;
frame_count = ceil((mode == 0x01 ? pixheight * 3 : pixheight) / (float)(frame_height)); // 彩色配置fpga 高度要为目标图像高度的3倍
frame_count += 1; // 最后一帧丢帧,多采集一帧防止图像数据缺失
frame_count += 1; // 最后一帧丢帧,多采集一帧防止图像数据缺失
if (frame_height * frame_count > FPGA_MAX_HEIGHT_SUP)
frame_count = FPGA_MAX_HEIGHT_SUP / frame_height;
int startsample = 202; //205
int startsample = 202; // 205
ModeFpga fpgamod = {
.colorMode = mode,
.dpi = dpi,
@ -141,8 +148,7 @@ void MultiFrameCapture::open()
.adcB = 0,
.selftest = 0,
.sp = sp_dst}; // 600DPI 0x1450 300DPI 0xe10
readframe_timeout = sp_dst / 4 /(mode == 1 ? 1 :3);
readframe_timeout = sp_dst / 4 / (mode == 1 ? 1 : 3);
m_capFpageregs->setRegs(0x01, *((int *)(&fpgamod)));
m_capFpageregs->setSample(startsample);
@ -154,11 +160,11 @@ void MultiFrameCapture::open()
m_capFpageregs->setEnTestCol(false);
m_capFpageregs->setEnTestBit(false);
//m_capFpageregs->setFrame_interval_max(static_cast<int>(sp_dst * 4 / (mode ? 1 : 3))); //dpi > 2 ? 7200 : 3600
//m_capFpageregs->setFrame_interval_min(static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3) )); //1540
m_capFpageregs->setFrame_interval_max(0x1010); //dpi > 2 ? 7200 : 3600
m_capFpageregs->setFrame_interval_min(0xa98); //dpi > 2 ? 900 : 1540 a98
printf("interval_max = %d interval_min = %d \n", static_cast<int>(sp_dst * 4 / (mode ? 1 : 3)), static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3) ));
// m_capFpageregs->setFrame_interval_max(static_cast<int>(sp_dst * 4 / (mode ? 1 : 3))); //dpi > 2 ? 7200 : 3600
// m_capFpageregs->setFrame_interval_min(static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3) )); //1540
m_capFpageregs->setFrame_interval_max(0x1010); // dpi > 2 ? 7200 : 3600
m_capFpageregs->setFrame_interval_min(0xa98); // dpi > 2 ? 900 : 1540 a98
printf("interval_max = %d interval_min = %d \n", static_cast<int>(sp_dst * 4 / (mode ? 1 : 3)), static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3)));
// m_capFpageregs->setFrameNum(1);
configFPGAParam(mode, m_config.params.dpi);
StopWatch swwv4l2open;
@ -170,14 +176,14 @@ void MultiFrameCapture::open()
m_capFpageregs->setFrameHeight(frame_height);
m_capFpageregs->update();
m_capFpageregs->capture(); // abort first frame
printf(" getFrame_counter_val %d \n",m_capFpageregs->getFrame_counter_val());
// video->read_frame(,400);
unsigned char *buf = NULL;
video->HtCamReadCaptureFrame((void **)&buf,400);
//std::this_thread::sleep_for(std::chrono::milliseconds(50));
printf(" getFrame_counter_val %d \n", m_capFpageregs->getFrame_counter_val());
// video->read_frame(,400);
unsigned char *buf = NULL;
video->HtCamReadCaptureFrame((void **)&buf, 400);
// std::this_thread::sleep_for(std::chrono::milliseconds(50));
printf("abort first frame \n");
//printf(" getFrame_counter_val %d \n",m_capFpageregs->getFrame_counter_val());
// printf(" getFrame_counter_val %d \n",m_capFpageregs->getFrame_counter_val());
m_capFpageregs->setFrameNum(frame_count);
printf("frame count = %d height = %d \n", frame_count, frame_height);
m_capFpageregs->setFrameHeight(frame_height);
@ -193,11 +199,13 @@ void MultiFrameCapture::open()
void MultiFrameCapture::snap()
{
#ifndef TEST_SIMCAP
std::lock_guard<std::mutex> m_lock(m_mtx_snap);
b_stop_snap = b_end_snap = false;
m_capFpageregs->capture();
snaped_index++;
m_cv_snap.notify_all();
#endif
}
void MultiFrameCapture::stopsnap(bool autosize)
@ -230,13 +238,29 @@ void *MultiFrameCapture::readFrameTest(int timeout)
void MultiFrameCapture::UpdateScanParam(HG_ScanConfiguration config)
{
m_config = config;
printf("pageSize:%d\r\n",m_config.params.pageSize);
printf("isColor:%d\r\n",m_config.params.isColor);
printf("dpi:%d\r\n",m_config.params.dpi);
printf("pageSize:%d\r\n", m_config.params.pageSize);
printf("isColor:%d\r\n", m_config.params.isColor);
printf("dpi:%d\r\n", m_config.params.dpi);
#ifdef TEST_SIMCAP
if (m_test_pimg)
free(m_test_pimg);
FILE *f_img;
if (m_config.params.isColor == 1) // color
f_img = fopen("/home/root/color.jpg", "rb");
else
f_img = fopen("/home/root/gray.jpg", "rb");
// m_config.params.pageSize = 1;
if (f_img == nullptr)
printf("!!!!!!!!!! error while reading image \r\n");
fseek(f_img, 0, SEEK_END);
m_test_pimg_len = ftell(f_img);
m_test_pimg = malloc(m_test_pimg_len);
printf("!!!!!!!!!! info: reading image len = %d m_test_pimg = %p \r\n", f_len, m_test_pimg);
fclose(f_img);
#endif
}
void MultiFrameCapture::createCorrect(int correctmode)
@ -310,26 +334,34 @@ bool MultiFrameCapture::capturerImage()
void MultiFrameCapture::waitsnapdone(int state)
{
// printf("!!!!!!!!!!!!!!!! m_cv_snapdone wait \n");
// V4L2_DATAINFO_Ex info;
// info.snaped_index = snaped_index;
// info.snap_end = true;
// info.error_code = state;
// if (b_end_snap)
// {
// m_frameinfos.Put(info);
// return;
// }
// std::unique_lock<std::mutex> lock(m_mtx_snapdone);
// m_cv_snapdone.wait(lock);
// b_end_snap = true;
// m_frameinfos.Put(info);
// printf("!!!!!!!!!!!!!!!! m_cv_snapdone wait done \n");
#ifdef TEST_SIMCAP
HG_JpegCompressInfo info;
info.DataLength = m_test_pimg_len;
info.pJpegData = m_test_pimg;
m_glue.m_imageready(&info);
#else
printf("!!!!!!!!!!!!!!!! m_cv_snapdone wait \n");
V4L2_DATAINFO_Ex info;
info.snaped_index = snaped_index;
info.snap_end = true;
info.error_code = state;
if (b_end_snap)
{
m_frameinfos.Put(info);
return;
}
std::unique_lock<std::mutex> lock(m_mtx_snapdone);
m_cv_snapdone.wait(lock);
b_end_snap = true;
m_frameinfos.Put(info);
printf("!!!!!!!!!!!!!!!! m_cv_snapdone wait done \n");
#endif
}
bool MultiFrameCapture::IsImageQueueEmpty()
{
printf(" m_frameinfos.Size = %d iImageremain = %d bScanning = %d\n",m_frameinfos.Size(),iImageremain.operator int(),bScanning);
printf(" m_frameinfos.Size = %d iImageremain = %d bScanning = %d\n", m_frameinfos.Size(), iImageremain.operator int(), bScanning);
return (m_frameinfos.Size() == 0 && iImageremain == 0) && !bScanning;
}
@ -446,23 +478,22 @@ void MultiFrameCapture::configFPGAParam(int mode, int dpi)
set_expo(i, fpgaparam.ExposureB[i % 3]);
LOG("fpgaparam.ExposureB[%d] = %d\n", i % 3, fpgaparam.ExposureB[i % 3]);
}
int A_value,B_value;
int A_value, B_value;
std::this_thread::sleep_for(std::chrono::milliseconds(3));
m_capFpageregs->setAOffset(i, fpgaparam.OffsetF[i]);
LOG("fpgaparam.setAOffset[%d] = %d \n", i, fpgaparam.OffsetF[i]);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
m_capFpageregs->getAOffset(i,A_value,B_value);
LOG("fpgaparam.getAOffset[%d] = A_value = %d B_value = %d \n", i, A_value,B_value);
m_capFpageregs->getAOffset(i, A_value, B_value);
LOG("fpgaparam.getAOffset[%d] = A_value = %d B_value = %d \n", i, A_value, B_value);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
m_capFpageregs->setBOffset(i, fpgaparam.OffsetB[i]);
LOG("fpgaparam.setBOffset[%d] = %d \n", i, fpgaparam.OffsetB[i]);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
m_capFpageregs->getBOffset(i,A_value,B_value);
LOG("fpgaparam.getBOffset[%d] = A_value = %d B_value = %d \n", i, A_value,B_value);
m_capFpageregs->getBOffset(i, A_value, B_value);
LOG("fpgaparam.getBOffset[%d] = A_value = %d B_value = %d \n", i, A_value, B_value);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
m_capFpageregs->setAGain(i, fpgaparam.GainF[i]);
@ -479,8 +510,7 @@ void MultiFrameCapture::configFPGAParam(int mode, int dpi)
// LOG("fpgaparam.getBGain[%d] = A_value = %d B_value = %d \n", i, A_value,B_value);
// std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
}
;
};
int MultiFrameCapture::width()
{
int dpi = m_capFpageregs->getDpi();
@ -502,16 +532,16 @@ void MultiFrameCapture::snaprun()
{
auto snap_func = [this](V4L2_DATAINFO_Ex frame_info, int channels, bool last_frame, unsigned int frame_index)
{
//void *data = video->read_frame(500);
// void *data = video->read_frame(500);
char *data = NULL;
video->HtCamReadCaptureFrame((void **)&data,400);
video->HtCamReadCaptureFrame((void **)&data, 400);
frame_info.lost_frame = data ? false : true;
frame_info.last_frame = last_frame;
frame_info.frame_index = frame_index;
if (data)
{
// cv::Mat mat(frame_info.height, frame_info.width, CV_8UC1, data);
// cv::Mat mat(frame_info.height, frame_info.width, CV_8UC1, data);
frame_info.data = (char *)data;
m_frameinfos.Put(frame_info);
}
@ -540,7 +570,6 @@ void MultiFrameCapture::snaprun()
{
snap_func(frame_info, channels, (i == frame_count), i);
if (b_stop_snap)
{
int snaped_frame_count = m_capFpageregs->getFrame_counter_val();
@ -557,10 +586,10 @@ void MultiFrameCapture::snaprun()
}
}
//iImageremain++;
// iImageremain++;
m_cv_snapdone.notify_all();
b_end_snap = true;
//printf("!!!!!!!!!!!!!!!! m_cv_snapdone notify_all \n");
// printf("!!!!!!!!!!!!!!!! m_cv_snapdone notify_all \n");
}
}
@ -574,7 +603,7 @@ void MultiFrameCapture::procimage()
{
static int idx = 0;
ThreadPool prc_pool(4);
//td::queue<std::future<cv::Mat>> prc_fu;
// td::queue<std::future<cv::Mat>> prc_fu;
unsigned int frames_height;
unsigned int frames_width = 0;
while (b_imgproc)
@ -582,9 +611,9 @@ void MultiFrameCapture::procimage()
V4L2_DATAINFO_Ex frame = m_frameinfos.Take();
if (!frame.data)
{
HG_JpegCompressInfo info ;
HG_JpegCompressInfo info;
info.pJpegData = (unsigned char *)frame.data;
info .DataLength = frame.width *frame.height;
info.DataLength = frame.width * frame.height;
m_glue.m_imageready(info);
}
@ -671,7 +700,6 @@ void MultiFrameCapture::procimage()
// if (frames_height > 0 && frames_width > 0)
// m_mat = cv::Mat(frames_height, frames_width, CV_8UC(m_config.params.isColor ? 3 : 1), pimgdata_info.pdata);
// float vratio = *((float *)&m_fpgaparam.VRatio);
// float hratio = *((float *)&m_fpgaparam.HRatio);
// if (!(vratio >= 0.8f && vratio <= 1.20f && hratio > 0.8f && hratio < 1.2f))
@ -1075,7 +1103,7 @@ void MultiFrameCapture::openDevice(int dpi, int mode)
int channels = mode == 0x01 ? 3 : 1;
int width = channelwidth * channels;
int frame_height = mode == 0x01 ? 60 * 3 : 60;
int startsample = 202; //205
int startsample = 202; // 205
auto fpgaparam = GetFpgaparam(dpi, mode);
int t_real_dpi = dpi == 1 ? 2 : (dpi == 2 ? 2 : 3);
ModeFpga fpgamod = {
@ -1096,19 +1124,19 @@ void MultiFrameCapture::openDevice(int dpi, int mode)
// m_capFpageregs->setEnTestCol(false);
// m_capFpageregs->setEnTestBit(false);
//m_capFpageregs->setFrame_interval_max(static_cast<int>(sp_dst * 4 / (mode ? 1 : 3))); //dpi > 2 ? 7200 : 3600
//m_capFpageregs->setFrame_interval_min(static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3) )); //1540
// 2023 7-15 屏蔽
// m_capFpageregs->setFrame_interval_max(0x1010); //dpi > 2 ? 7200 : 3600
// m_capFpageregs->setFrame_interval_min(0xa98); //dpi > 2 ? 900 : 1540
configFPGAParam(mode, dpi);
// m_capFpageregs->setFrame_interval_max(static_cast<int>(sp_dst * 4 / (mode ? 1 : 3))); //dpi > 2 ? 7200 : 3600
// m_capFpageregs->setFrame_interval_min(static_cast<int>(sp_dst * 0.1 / (mode ? 1 : 3) )); //1540
.
// 2023 7-15 屏蔽
// m_capFpageregs->setFrame_interval_max(0x1010); //dpi > 2 ? 7200 : 3600
// m_capFpageregs->setFrame_interval_min(0xa98); //dpi > 2 ? 900 : 1540
configFPGAParam(mode, dpi);
StopWatch swwv4l2open;
int ret = video->open_video(width, 60 * 2); // 300dpi 7344/2 600dpi 7344 //FRAME_HEIGHT * 2
//int ret = video->open_video(); // 300dpi 7344/2 600dpi 7344 //FRAME_HEIGHT * 2
// int ret = video->open_video(width, 60 * 2); // 300dpi 7344/2 600dpi 7344 //FRAME_HEIGHT * 2
int ret = video->open_video(); // 300dpi 7344/2 600dpi 7344 //FRAME_HEIGHT * 2
if (ret < 0)
{
return ;
return;
}
printf("opened video with width = %d height = %d time eplased = %.2f \n", width, 60 * 2, swwv4l2open.elapsed_ms());
@ -1119,9 +1147,9 @@ void MultiFrameCapture::openDevice(int dpi, int mode)
{
m_capFpageregs->capture(); // abort first frame
//video->read_frame(200);
// video->read_frame(200);
char *buf = NULL;
video->HtCamReadCaptureFrame((void **)&buf,200);
video->HtCamReadCaptureFrame((void **)&buf, 200);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("abort first frame \n");
}
@ -1173,8 +1201,6 @@ void MultiFrameCapture::creatcorrectconfig(int dpi, int mode)
video->close_video();
}
// void MultiFrameCapture::myFloodFill(cv::Mat& image, bool isTwoSide)
// {
// int w = image.cols;
@ -1239,6 +1265,7 @@ void MultiFrameCapture::creatcorrectconfig(int dpi, int mode)
void MultiFrameCapture::init_imagedatabuffer()
{
#ifndef TEST_SIMCAP
printf("pimgdata_info.pdata = %p \n", pimgdata_info.pdata);
if (pimgdata_info.pdata)
{
@ -1264,6 +1291,7 @@ void MultiFrameCapture::init_imagedatabuffer()
pimgdata_info.frame_count = 0;
printf("pimgdata_info.pdata = %p malloc_size = %d per_frame_bytes = %d \n", pimgdata_info.pdata, total_buffer_need, per_frame_bytes);
}
#endif
}
// void MultiFrameCapture::update_imgdatainfo(cv::Mat itemmat)

View File

@ -4,6 +4,8 @@
#include "CorrectParam.h"
#include "IPreproc.h"
#define TEST_SIMCAP
class FpgaComm;
class gVideo;
class Gpio;
@ -51,7 +53,10 @@ private:
int offsetStep[12];
int expStep[2][3];
double radio = 1;
#ifdef TEST_SIMCAP
void* m_test_pimg;
unsigned int m_test_pimg_len;
#endif
CamZ_Reg_4 reg_;
private:
void reset_fpga();

View File

@ -138,22 +138,6 @@ void Scanner::startScan()
threadRunScan = std::thread(&Scanner::runScan, this);
writesyslog(LOG_INFO, getmeminfo());
// HG_ScanConfiguration cfg;
// cfg.params.dpi=2;
// cfg.params.isColor = 1;
// cfg.params.pageSize = 1;
// capturer->UpdateScanParam(cfg);
// for(int i = 0;i<1;i++)
// {
// capturer->open();
// capturer->snap();
// capturer->capturerImage();
// capturer->close();
// // std::this_thread::sleep_for(std::chrono::milliseconds(4000));//避免scanpin io 开始异常
// }
// //capturer->close();
}
void Scanner::runMessageLoop()
@ -305,7 +289,7 @@ void Scanner::runScan()
break;
}
//capturer->snap();
capturer->snap();
motorCuozhi.pauseWaitForThread();
int i = 0;
int max = 7;
@ -355,9 +339,9 @@ void Scanner::runScan()
m_scaninfo.DoubleNum++;
writesyslog(LOG_INFO, "paper double paper error\n");
}
//capturer->stopsnap(b_autosize);
//capturer->waitsnapdone(1);//等待采集完成
//this_thread::sleep_for(std::chrono::milliseconds(500));//200 80ms 600 260ms
capturer->stopsnap(b_autosize);
capturer->waitsnapdone(1);//等待采集完成
this_thread::sleep_for(std::chrono::milliseconds(500));//200 80ms 600 260ms
LOG("\n ----------------EXIT 11111---------------- \n");
break;
}