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.
 
 
 
 
 
 

168 lines
4.5 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();
}
//void datathread::slotsetid(QString _id)
//{
// id = _id;
//}
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] == 0x00&& data[i+2] == 0x00&& data[i+3] == 0x00 ){
if(data[i] == 0xff && data[i+1] ==0xff && data[i+2] == 0xff && data[i+3] == 0xff){
onImage = true;
// emit signalSetID( QByteArray((char*) data+i+4,8));
// i+=8;
// qDebug()<<QByteArray((char*) data+i-4,8);
// 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;
}
}
// if(pixNum<rows*cols){
// image.at<ushort>(pixNum/cols,pixNum%cols) = data[i+2]*256 +data[i+3];
// pixNum +=1;
// }
// if(pixNum == rows*cols){
// image.copyTo(temp);
// temp = temp*4;
// emit signalGetImage(temp);
// pixNum = 0;
// onImage = false;
// // qDebug()<<__FILE__<<__LINE__;
// }
}
}
}
dataMutex.lock();
memcpy(pureData,pureData+i,datalen-i);
datalen -=i;
dataMutex.unlock();
}
}