opencv 摄像头 线程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include "opencv.hpp"
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#define OUTPUT_VIDEO_FILE "webcam.avi"
#define OUTPUT_VIDEO_FILE1 "webcam1.avi"
#define OUTPUT_VIDEO_FILE2 "a.k"

using namespace std;
using namespace cv;

char message[32] = "Hello World";
//子进程 先父进程
void *thread_function(void *arg){

string outputVideoFile =OUTPUT_VIDEO_FILE;
VideoCapture videoCapture(0); //初始化摄像头
if (!videoCapture.isOpened()){
cout << "Failed to open the default camera" << endl;
return NULL;
}
/* 获取视频帧的宽度和高度 */
double frameWidth = videoCapture.get(CV_CAP_PROP_FRAME_WIDTH);
double frameHeight = videoCapture.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame size is [" << frameWidth << "x" << frameHeight << "]" << endl;
/* 创建一个VideoWriter的对象将视频流写入文件 */
Size frameSize(static_cast<int>(frameWidth), static_cast<int>(frameHeight));
/* videoWriter(输出的文件名,输出文件的编码,帧率,帧的尺寸,是否是彩色(尽支持win)) */
//('D', 'I', 'V', 'X') mp4
VideoWriter videoWriter(outputVideoFile.c_str(), CV_FOURCC('D', 'I', 'V', 'X'), 20, frameSize, true);
if (!videoWriter.isOpened()){
cout << "Failed to initialize the VideoWriter" << endl;
return NULL;
}
while(true){ //图片
Mat frame;
if (!videoCapture.read(frame)){ // 抓住,解码并返回下一个视频帧
cout << "Failed to read a video frame" << endl;
break;
}
videoWriter.write(frame); //写入文件的框架
if(27 == waitKey(30)){ //等待按下“ESC”键(注意:不是工作如果没有窗口被创建!)
cout << "ESC key pressed, stop capturing video" << endl;
break;
}
}
return 0;
printf("子进程 %s\n", (char *)arg);
//pthread_exit("Thank you for the cpu time");
}

int main(int argc, char *argv[]){
pthread_t a_thread; //线程id
void *thread_result;


/*使用缺省属性创建线程*/
if (pthread_create(&a_thread, NULL,thread_function, (void *)argv[1]) < 0){
perror("fail to pthread_create");
exit(-1);
}
printf("父进程\n");
// 等待线程结束(资源回收)
if (pthread_join(a_thread, &thread_result) < 0){
perror("fail to pthread_join");
exit(-1);
}
printf("MESSAGE is now %s\n", message);
return 0;
}

posted @ 2016-08-01 13:57  极客先锋  阅读(1670)  评论(0编辑  收藏  举报