You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 lines
3.4 KiB

#include "datathread.h"
#include <QDebug>
#include "protocol.h"
datathread::datathread(QObject *parent)
: QThread{parent}
{
recvImage = (unsigned char*)malloc(2880*1600*4);
sendImage = (unsigned char*)malloc(2880*1600*4);
pureData = (unsigned char*)malloc(4*1024*1024*16);
data = (unsigned char*)malloc(4*1024*1024*16);
datalen = 0;
}
void datathread::SetImageInfo(int _type, int _rows, int _cols)
{
type = _type;
rows = _rows;
cols = _cols;
switch (type) {
case gray8:
bitsPerPix=1;
break;
case gray16:
bitsPerPix=2;
break;
default:
break;
}
msleep(100);
onImage = false;
if(type == gray8){
image = Mat(rows,cols,CV_8UC1);
}
else if(type == gray16){
image = Mat(rows,cols,CV_16UC1);
}
}
void datathread::stopProcess()
{
}
void datathread::setStatu(bool _run)
{
isRun = _run;
}
void datathread::appendData(char *data, int len)
{
// qDebug()<<"get data with "<<len;
dataMutex.lock();
if(datalen+len>4*1024*1024*16){
dataMutex.unlock();
return;
}
memcpy(pureData+datalen,data,len);
datalen += len;
dataMutex.unlock();
}
int sizeofhead = 64;
void datathread::run(){
minLen = MINFrameSize;
datalen = 0;
onImage = false;//是否传输图像中
onLine = false;
lineNum = 0;
pixNum = 0;
while(isRun){
dataMutex.lock();
int len = datalen;
// qDebug()<<"data len "<<len;
memcpy(data,pureData,len);
dataMutex.unlock();
int i = 0;
for(;i<(len-sizeofhead);i+=4){
if(!onImage){
if(data[i] == 0xff&& data[i+1] == 0xff&& data[i+2] == 0xff&& data[i+3] == 0xff ){
onImage = true;
// qDebug()<<"get head";
}
}
else{
if(!onLine){
if(data[i] == 0xf0){
lineNum = (data[i+1]*256+data[i+2])*256+data[i+3];
pixNum = 0;
onLine = true;
}
}
else{
if(lineNum<rows && pixNum<cols){
for(int j =0;j<bitsPerPix;j++){
recvImage[(lineNum*cols+pixNum)*bitsPerPix+j] = data[i+4-1-j];
}
if(type == gray8 && image.type() == CV_8UC1){
image.at<uchar>(lineNum,pixNum) = data[i+3];
}
else if(type == gray16 && image.type() == CV_16UC1){
image.at<ushort>(lineNum,pixNum) = data[i+2]*256+data[i+3];
}
}
pixNum += 1;
if(pixNum==cols){
onLine = false;
if(lineNum+1 == rows){
memcpy(sendImage,recvImage,rows*cols*bitsPerPix);
// emit(signalGetImage(sendImage));
image.copyTo(temp);
emit(signalGetImage(temp));
// qDebug()<<"send image";
onImage = false;
}
}
}
}
}
dataMutex.lock();
memcpy(pureData,pureData+i,datalen-i);
datalen -=i;
dataMutex.unlock();
}
}