320 lines
8.7 KiB
C++
320 lines
8.7 KiB
C++
#include "HBDCamera.h"
|
|
#include "../config/ConfigManager.h"
|
|
#include <gl/GL.h>
|
|
#include <GL/khronos_glext.h>
|
|
#include "../protobuf/stream.pb.h"
|
|
#include "../logger.h"
|
|
|
|
#include <fstream>
|
|
|
|
PointImageShowInfo::~PointImageShowInfo()
|
|
{
|
|
if (m_ShowTex > 0) {
|
|
glDeleteTextures(1, &m_ShowTex);
|
|
m_ShowTex = 0;
|
|
}
|
|
m_ImageMat.release();
|
|
}
|
|
|
|
void PointImageShowInfo::Init(cv::Mat image, unsigned char* imageData)
|
|
{
|
|
m_ImageMat = image;
|
|
m_ImageData = imageData;
|
|
glGenTextures(1, &m_ShowTex);
|
|
m_Width = m_ImageMat.cols;
|
|
m_High = m_ImageMat.rows;
|
|
m_PixelRatio = (float)m_Width / m_High;
|
|
if (m_ShowTex > 0) {
|
|
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
|
|
|
if (m_IsColor) {
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_High, 0, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
else {
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
//glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
}
|
|
|
|
void PointImageShowInfo::Init()
|
|
{
|
|
if (m_ShowTex > 0) {
|
|
glDeleteTextures(1, &m_ShowTex);
|
|
m_ShowTex = 0;
|
|
}
|
|
if (m_ShowTex == 0) {
|
|
glGenTextures(1, &m_ShowTex);
|
|
m_Width = m_ImageMat.cols;
|
|
m_High = m_ImageMat.rows;
|
|
m_PixelRatio = (float)m_Width / m_High;
|
|
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
|
if (m_IsColor) {
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_High, 0, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
else {
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
//glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
}
|
|
|
|
void PointImageShowInfo::Update()
|
|
{
|
|
if (m_ShowTex > 0) {
|
|
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
|
if (m_IsColor) {
|
|
//glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
else {
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
}
|
|
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
}
|
|
|
|
HBDCamera::HBDCamera()
|
|
: m_DemandFlag(false)
|
|
//, m_ShowFlag(false)
|
|
, m_OriginalData(NULL)
|
|
, m_ImageChanged(false)
|
|
, m_RunFlag(false)
|
|
, m_CatpureThread(INVALID_HANDLE_VALUE)
|
|
, m_Aspect(1.0f)
|
|
, m_ShowFlag(new BoolData("ShowFlag"))
|
|
, m_LastMouRefImgPosX(new IntData("LastMouRefImgPosX"))
|
|
, m_LastMouRefImgPosY(new IntData("LastMouRefImgPosY"))
|
|
, m_PointImage(nullptr)
|
|
{
|
|
m_ExtCfg = ConfigManager::GetInstance()->GetExtCfg();
|
|
m_CameraCalibrationCfg = ConfigManager::GetInstance()->GetCameraCalibrationCfg();
|
|
m_tjinstance = tjInitCompress();
|
|
InitializeCriticalSection(&m_OriginalDataCS);
|
|
//m_LastMouRefImgPosX = 0;
|
|
//m_LastMouRefImgPosY = 0;
|
|
|
|
//InsertMp(&m_ShowFlag);
|
|
//InsertMp(&m_LastMouRefImgPosX);
|
|
//InsertMp(&m_LastMouRefImgPosY);
|
|
}
|
|
|
|
HBDCamera::~HBDCamera()
|
|
{
|
|
StopCamera();
|
|
if (m_tjinstance)tjDestroy(m_tjinstance);
|
|
DeleteCriticalSection(&m_OriginalDataCS);
|
|
if (m_GLTex) {
|
|
glDeleteTextures(1, &m_GLTex);
|
|
m_GLTex = 0;
|
|
}
|
|
if (m_LogGLTex) {
|
|
glDeleteTextures(1, &m_LogGLTex);
|
|
m_LogGLTex = 0;
|
|
}
|
|
if (m_LogImage)tjFree(m_LogImage);
|
|
}
|
|
|
|
void HBDCamera::StartUp()
|
|
{
|
|
StopCamera();
|
|
m_RunFlag = true;
|
|
m_CatpureThread = AtlCreateThread(CatpureProc, this);
|
|
}
|
|
|
|
unsigned int HBDCamera::GetShowImage(unsigned char * data, unsigned long size, int *width, int *height)
|
|
{
|
|
if (data == NULL || size == 0)
|
|
return 0;
|
|
|
|
int inSubsamp, inColorspace;
|
|
tjhandle tjInstance = tjInitDecompress();
|
|
if (tjDecompressHeader3(tjInstance, data, size, width, height,
|
|
&inSubsamp, &inColorspace) < 0) {
|
|
tjDestroy(tjInstance);
|
|
return 0;
|
|
}
|
|
|
|
unsigned char * ptmp = tjAlloc((*width) * (*height) * tjPixelSize[TJPF_GRAY]);
|
|
if (tjDecompress2(tjInstance, data, size, ptmp, *width, 0, *height, TJPF_GRAY, 0) < 0) {
|
|
tjFree(ptmp);
|
|
tjDestroy(tjInstance);
|
|
return 0;
|
|
}
|
|
|
|
tjDestroy(tjInstance);
|
|
glDeleteTextures(1, &m_LogGLTex);
|
|
m_LogGLTex = 0;
|
|
GenTex(&m_LogGLTex, *width, *height, ptmp);
|
|
tjFree(ptmp);
|
|
return m_LogGLTex;
|
|
}
|
|
|
|
void HBDCamera::StopCamera()
|
|
{
|
|
if (m_CatpureThread != INVALID_HANDLE_VALUE) {
|
|
m_RunFlag = false;
|
|
if (WaitForSingleObject(m_CatpureThread, 500) == WAIT_TIMEOUT)
|
|
{
|
|
TerminateThread(m_CatpureThread, 1);
|
|
}
|
|
CloseHandle(m_CatpureThread);
|
|
m_CatpureThread = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
|
|
DWORD WINAPI HBDCamera::CatpureProc(HBDCamera* _this)
|
|
{
|
|
if (_this) {
|
|
_this->CatpureRun();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool HBDCamera::GetRawImage(unsigned char* img)
|
|
{
|
|
if (!IsConnect())
|
|
return false;
|
|
|
|
if (!m_DemandFlag)
|
|
return false;
|
|
|
|
if (!m_OriginalData)
|
|
return false;
|
|
|
|
EnterCriticalSection(&m_OriginalDataCS);
|
|
memcpy_s(img, m_OriginalDataSize, m_OriginalData, m_OriginalDataSize);
|
|
LeaveCriticalSection(&m_OriginalDataCS);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void HBDCamera::TakePhotos() {
|
|
|
|
unsigned char* imagetemp;
|
|
int width = 0, height = 0, datasize = 0;
|
|
GetOriginSize(width, height, datasize);
|
|
if (datasize > 0) {
|
|
imagetemp = new unsigned char[datasize];
|
|
if (GetRawImage(imagetemp))
|
|
{
|
|
cv::Mat image(height, width, CV_8U, imagetemp);
|
|
if (m_PointImage) {
|
|
delete m_PointImage;
|
|
m_PointImage = NULL;
|
|
}
|
|
|
|
PointImageShowInfo* info = new PointImageShowInfo();
|
|
info->Init(image, image.data);
|
|
m_PointImage = info;
|
|
delete[] imagetemp;
|
|
}
|
|
else {
|
|
delete[]imagetemp;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void HBDCamera::GetImageSize(int rowindex,::stream::ResponseAny** response) {
|
|
stream::ImgInfoResponce result;
|
|
unsigned char* image = nullptr;
|
|
unsigned long image_size = 0;
|
|
int width = 0, height = 0;
|
|
if (g_log->m_LogDao->FindImage(rowindex, &image, &image_size)) {
|
|
result.set_levelimage(GetShowImage(image, image_size, &width, &height));
|
|
result.set_height(height);
|
|
result.set_width(width);
|
|
}
|
|
if (image != nullptr)delete[] image;
|
|
(*response)->mutable_data()->PackFrom(result);
|
|
|
|
}
|
|
|
|
void HBDCamera::CallFunc(const ReadData& rd, ::stream::ResponseAny** response) {
|
|
stream::ImgInfoResponce result;
|
|
|
|
CameraFunc func = (CameraFunc)ConverType::TryToI(rd.nameKey);
|
|
switch (func) {
|
|
case SETDEMANDCATPURE:
|
|
SetDemandCatpure((bool)ConverType::TryToI(rd.strValue));
|
|
break;
|
|
case SETEXPOSUREAUTOBYCFG:
|
|
SetExposureAutoByCfg();
|
|
break;
|
|
case SETGAINAUTOBYCFG:
|
|
SetGainAutoByCfg();
|
|
break;
|
|
case SETEXPOSURETIMEBYCFG:
|
|
SetExposureTimeByCfg();
|
|
break;
|
|
case SETFRAMERATEENABLE:
|
|
SetFrameRateEnable();
|
|
break;
|
|
case SETFRAMERATE:
|
|
SetFrameRate();
|
|
break;
|
|
case GETSHOWIMAGE:
|
|
result.set_levelimage(GetShowImage());
|
|
(*response)->mutable_data()->PackFrom(result);
|
|
break;
|
|
case GETSHOWIMAGES:
|
|
GetImageSize(ConverType::TryToI(rd.strValue),response);
|
|
break;
|
|
case TAKEPHOTOS:
|
|
TakePhotos();
|
|
break;
|
|
default: break;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void HBDCamera::SendToClients(WRITETYPE type, const std::string& addKey) {
|
|
std::list<Item> its;
|
|
its.emplace_back(Item{ "IsConnect",to_string(IsConnect()),iBOOL });
|
|
its.emplace_back(Item{ m_ShowFlag->GetCode(),m_ShowFlag->GetValueStr(),m_ShowFlag->GetDataType()});
|
|
its.emplace_back(Item{ m_LastMouRefImgPosX->GetCode(),m_LastMouRefImgPosX->GetValueStr(),m_LastMouRefImgPosX->GetDataType()});
|
|
its.emplace_back(Item{ m_LastMouRefImgPosY->GetCode(),m_LastMouRefImgPosY->GetValueStr(),m_LastMouRefImgPosY->GetDataType()});
|
|
|
|
if (m_PointImage) {
|
|
its.emplace_back(Item{ "High",to_string(m_PointImage->m_High),iINT });
|
|
its.emplace_back(Item{ "Width",to_string(m_PointImage->m_Width),iINT });
|
|
its.emplace_back(Item{ "ShowTex",to_string(m_PointImage->GetTex()),iUINT });
|
|
}
|
|
|
|
ClientWrapper::Instance()->PushAllClient(WriteData(CAMERAPARAM, its));
|
|
}
|
|
|
|
|
|
void HBDCamera::Update(const ReadData& rd, WRITETYPE type) {
|
|
//这3参数只在客户端修改
|
|
if (rd.nameKey == "LastMouRefImgPosX") {
|
|
m_LastMouRefImgPosX->SetValue(stoi(rd.strValue));
|
|
}
|
|
else if (rd.nameKey == "LastMouRefImgPosY") {
|
|
m_LastMouRefImgPosY->SetValue(stoi(rd.strValue));
|
|
}
|
|
else if (rd.nameKey == "ShowFlag") {
|
|
m_ShowFlag->SetValue((bool)stoi(rd.strValue));
|
|
}
|
|
|
|
SendToClients(); //客户端更新后在发送给客户端
|
|
} |