tx-gxx-linux/device/gxx-linux/testcapimage/main.cpp

213 lines
5.4 KiB
C++

#include <iostream>
#include <memory>
#include "stringex.hpp"
#include <functional>
#include "Capturer.h"
#include "StopWatch.h"
#include "opencv2/opencv.hpp"
#include "config.h"
#include "FpgaComm.h"
#include "usbservice.h"
static void savebitmap(void *imgdata, int width, int height, bool color, std::string path)
{
cv::Mat mat;
cv::Mat saveMat;
std::vector<cv::Mat> ch_mats;
int blockcnt = 12;
int spitWidth = (width * 3) / blockcnt;
int mergewidth = width / 4;
mat = cv::Mat(height, width * 3, CV_8UC1, imgdata);
cv::imwrite("gray" + path, mat);
saveMat = cv::Mat(height, width, CV_8UC3);
StopWatch sw1;
StopWatch sw;
for (int i = 0; i < 4; i++)
{
sw.reset();
for (int j = 0; j < 3; j++)
ch_mats.push_back(mat(cv::Rect(spitWidth * (i * 3 + j), 0, spitWidth, height)));
std::cout << string_format("split: %d image elapsed:%f ms\n ", i, sw.elapsed_ms());
sw.reset();
// if(i == 0)
// {
// swap(ch_mats[0], ch_mats[2]);
// swap(ch_mats[1], ch_mats[2]);
// }
// if (i == 1)
// {
// swap(ch_mats[0], ch_mats[2]);
// swap(ch_mats[1], ch_mats[0]);
// }
if (i == 2)
{
swap(ch_mats[0], ch_mats[2]);
}
if (i == 3)
{
swap(ch_mats[0], ch_mats[2]);
}
cv::merge(ch_mats, saveMat(cv::Rect(mergewidth * i, 0, mergewidth, height)));
std::cout << string_format("merge: %d image elapsed:%f ms\n ", i, sw.elapsed_ms());
ch_mats.clear();
}
std::cout << string_format("merge image elapsed:%f ms\n ", sw1.elapsed_ms());
cv::imwrite("merged" + path, saveMat);
}
unsigned int delay_done(std::shared_ptr<IRegsAccess> regs, int ms)
{
unsigned int val;
unsigned int reg8;
regs->read(8, reg8);
std::cout << "reg[8]:" << string_format("0x%08x", reg8) << std::endl;
regs->write(8, reg8 | 0x8);
std::cout << "sleep:" << ms << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
regs->read(8, reg8);
std::cout << "reg[8]:" << string_format("0x%08x", reg8) << std::endl;
regs->read(14, val);
val &= 0x0000ffff;
std::cout << string_format("leight = %d\n", val);
regs->write(8, reg8 & 0xfffffff7);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
regs->read(14, val);
val &= 0x0000ffff;
regs->read(8, reg8);
std::cout << "reg[8]:" << string_format("0x%08x", reg8) << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
regs->write(8, reg8 | 0x8);
regs->read(8, reg8);
std::cout << "reg[8]:" << string_format("0x%08x", reg8) << std::endl;
return val;
}
int delaytimes[] = {100, 200, 300, 400};
int ws[] = {7344/2, 7344};
void list_regs(std::shared_ptr<IRegsAccess> regs)
{
unsigned int val = 0;
for (int i = 5; i < 8; i++)
{
if (regs->read(i, val))
std::cout << string_format("reg[%d] = 0x%08x\n", i, val);
else
std::cout << "read regs error " << std::endl;
}
}
int main()
{
auto cap = std::shared_ptr<Capturer>(new Capturer());
StopWatch sw;
unsigned int val = 0;
auto regs = cap->regs();
UsbService us(regs, nullptr);
HGScanConfig config;
config.g200params.color=1;
config.g200params.dpi=1;
config.g200params.paper=(unsigned int)PaperSize::G400_A4;
void *data;
cap->open(config);
int ch = 0;
int ixx = 0;
std::cout << "mod1:" << CV_8UC3 << " " << CV_8UC1 << std::endl;
for (int i = 0; i < 0x10; i++)
{
sw.reset();
if (regs->read(i, val))
std::cout << string_format("reg[%d] = 0x%08x\n", i, val);
else
std::cout << "read regs error " << std::endl;
}
std::cout << "type enter to grab" << std::endl;
int ix = 0;
// int width = cap->width();;
// int fixedheight = cap->height() ;
// int type = cap->color();
// int height = fixedheight;
// if(type == CV_8UC3)
// {
// height = fixedheight/3;
// width = width*3*3;
// type = CV_8UC1;
// }
// else
// {
// height = fixedheight;
// width = width*3;
// }
while ((ch = getchar()) != 'q')
{
#ifdef LOOP_DEBUG
for (int i = 0; i < 2000; i++)
#endif
{
ix++;
sw.reset();
for (int j = 0; j < 15; j++)
{
sw.reset();
if (regs->read(j, val))
std::cout << string_format("reg[%d] = 0x%08x, %f\n", j, val, sw.elapsed_ms());
else
std::cout << "read regs error " << std::endl;
}
cap->snap();
#ifdef LOOP_DEBUG
height = delay_done(regs, delaytimes[ix % (sizeof(delaytimes) / sizeof(delaytimes[0]))]);
std::cout << string_format("88888%dth grab time height = %d", ix, height) << std::endl;
height = std::min(fixedheight, height);
#endif
if (data = cap->readFrame(3000))
{
//std::cout << string_format("%dth grab time height = %d: %fms", ix, height, sw.elapsed_ms()) << std::endl;
//std::cout << string_format("grab time width = %d, height = %d, type = %d: %fms", width, height, type, sw.elapsed_ms()) << std::endl;
#ifndef LOOP_DEBUG
cv::Mat mat=GetMergeMat(data,cap->width(),cap->height(),cap->color());
//mat = cv::Mat(height, width, type, data);
cv::imwrite( std::to_string(ix++)+ ".jpg", mat);
//std::cout << string_format("%dth grab time height = %d: %fms", ix, height, sw.elapsed_ms()) << std::endl;
// sw.reset();
// savebitmap(data, width, height, true, std::to_string(ix) + ".jpg");
// std::cout << string_format("%dth grab save time : %fms", ix, sw.elapsed_ms()) << std::endl;
// sw.reset();
// memset(data, 0, width * height * 3);
// std::cout << string_format("%dth memset time : %fms", ix, sw.elapsed_ms()) << std::endl;
#endif
}
else
{
std::cout << "error :" << ix << std::endl;
#ifdef LOOP_DEBUG
break;
#endif
}
}
std::cout << "type enter to continue" << std::endl;
}
return 0;
}