-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPubliserStreamRetrieve.cpp
executable file
·141 lines (117 loc) · 3.44 KB
/
PubliserStreamRetrieve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifdef __unix__
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "PubliserStreamRetrieve.h"
#include "GenICam/Frame.h"
#include<Media/ImageConvert.h>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<ros/ros.h>
#include<opencv2/highgui/highgui.hpp>
#include<image_transport/image_transport.h>
#include<cv_bridge/cv_bridge.h>
#include<string>
#include<time.h>
double lastClockTime,t_start,t_end;
void imageCallback(uint8_t* image,int image_size){
}
PubliserStreamRetrieve::PubliserStreamRetrieve(IStreamSourcePtr& streamSptr,ros::NodeHandle nh)
: CThread("PubliserStreamRetrieve")
, m_isLoop(false)
, m_streamSptr(streamSptr)
{
image_transport::ImageTransport it(nh);
pub = it.advertise("camera/leftRaw",1);
}
bool PubliserStreamRetrieve::start()
{
m_isLoop = true;
return createThread();
}
bool PubliserStreamRetrieve::stop()
{
m_isLoop = false;
m_streamSptr.reset();
return destroyThread();
}
void PubliserStreamRetrieve::threadProc()
{
while (m_isLoop)
{
t_start = clock();
std::cout<<"-------------------------------------------------------------"<<std::endl;
CFrame frame;
//获取一帧
if (!m_streamSptr)
{
printf("m_streamPtr is NULL.\n");
return;
}
bool isSuccess = m_streamSptr->getFrame(frame, 300);
if (!isSuccess)
{
printf("getFrame fail.\n");
continue;
}
//判断帧的有效性
bool isValid = frame.valid();
if (!isValid)
{
printf("frame is invalid!\n");
continue;
}
IMGCNV_SOpenParam conv_param;
conv_param.width = frame.getImageWidth();
conv_param.height = frame.getImageHeight();
if (conv_param.width%4 != 0){
conv_param.paddingX = 4 - conv_param.width%4;
}else
{
conv_param.paddingX = 0;
}
if (conv_param.height%2 != 0){
conv_param.paddingY = 2 - conv_param.height%2;
}else
{
conv_param.paddingY = 0;
}
//转换图片格式
int nRgbBufferSize = 0;
nRgbBufferSize = conv_param.height*conv_param.width*3;
uint8_t* imgBuff = (uint8_t*)frame.getImage();
uint8_t* img_out = (uint8_t*)malloc(nRgbBufferSize);
conv_param.dataSize =frame.getImageSize();
conv_param.pixelForamt = frame.getImagePixelFormat();
IMGCNV_EErr status = IMGCNV_ConvertToBGR24_Ex(imgBuff,&conv_param,img_out,&nRgbBufferSize,IMGCNV_DEMOSAIC_BILINEAR);
if (IMGCNV_SUCCESS != status)
{
/* 释放内存 */
printf("IMGCNV_ConvertToBGRA24_Ex failed.\n");
free(imgBuff);
free(img_out);
return;
}
std::cout<<"[Time]Time stamp:"<<frame.getImageTimeStamp()<<std::endl;
std::cout<<"[Size]Source image size: "<<frame.getImageSize()<<std::endl;
std::cout<<"[Size]CV image size: "<<nRgbBufferSize<<std::endl;
cv::Mat Image(conv_param.height,conv_param.width,CV_8UC3,img_out);
//cv::imshow("view",Image);
//cv::waitKey(10);
/*pub*/
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(),"bgr8",Image).toImageMsg();
pub.publish(msg);
//ros::spinOnce();
//打印FrameID
t_end = clock();
std::cout<<"[Time]pub total time:"<<(t_end-t_start)/CLOCKS_PER_SEC<<std::endl;
printf("[INFO]get frame %lld successfully thread ID :%d\n", frame.getBlockId(), CThread::getCurrentThreadID());
std::cout<<"[INFO]Press ctrl+z to stop"<<std::endl;
/* 释放内存 */
//free(imgBuff);
free(img_out);
}
cv::destroyAllWindows();
}