在《移植OpenCV3.1到BBB-Debian》一文件中配置好的环境为前提(后期不特别说明,ARM版的例子均以该环境为基础),编写使用OpenCV库保存WEBCAM的数据为图片的代码,下面是相应的源码和编译配置文件(都保存在SaveCamPic目录下):
1.源码scp.cpp
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <sys/timeb.h>
#include <stdlib.h>
#include <cv.h>
#include <cxcore.h>
//#include <highgui.h>
int main( int argc,char ** argv )
{
//声明IplImage指针
IplImage* pFrame=NULL;
IplImage* pSaveFrame=NULL;
CvCapture* pCapture=NULL;
static char filename[40];
struct tm * tm_ptr;
struct timeb tp;
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 10; //质量值
p[2] = 0;
//cvNamedWindow("video",1);
if( !(pCapture=cvCaptureFromCAM(0 /*-1*/)) ) //此处值为-1表示自动搜索到的第一个摄象头,而0则表示第一个,1则表示第2个,以此类推。
{
fprintf(stderr,"Can not open camera.\n");
return -1;
}
if(mkdir("./Alert",0755)==-1)
{
//创建时存在该目录会返回错误码,如不存在则创建它,但由于我们需要该目录,则出错也不处理
}
//逐帧读取视频
while(pFrame=cvQueryFrame(pCapture))
{
//cvShowImage("video",pFrame);
//if(cvWaitKey(2)>=0) break;
pSaveFrame=cvCreateImage(cvSize(1024,768),pFrame->depth,pFrame->nChannels);
//get file name
ftime(&tp);
tm_ptr=localtime(&tp.time);
snprintf(filename, 40, "./Alert/%04d%02d%02d%02d%02d%02d%04d.jpg", tm_ptr->tm_year-100+2000, tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_hour,tm_ptr->tm_min, tm_ptr->tm_sec,tp.millitm);
cvResize(pFrame,pSaveFrame,CV_INTER_LINEAR);
cvSaveImage(filename,pSaveFrame,p);
cvReleaseImage(&pSaveFrame);
}
//cvDestroyWindow("video");
cvReleaseCapture(&pCapture);
return 0;
}
2.编译配置文件CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
PROJECT(SaveCamPic)
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_C_COMPILER /home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-g++)
#SET(CMAKE_LINKER /home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-ld)
#SET(CMAKE_FIND_ROOT_PATH /home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/)
SET(CMAKE_FIND_ROOT_PATH
/home/guochongxin/bbb-debian/thirds/opencv-3.1.0/install/
/home/guochongxin/bbb-debian/thirds/opencv-3.1.0/install/include/
/home/guochongxin/bbb-debian/thirds/opencv-3.1.0/install/lib/
/home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/lib/
/home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/include/
/home/guochongxin/bbb-debian/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/
/home/guochongxin/bbb-debian/thirds/ffmpeg-2.8.6/)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
find_package(OpenCV REQUIRED)
set(NAME_SRC scp.cpp)
ADD_EXECUTABLE(scp ${NAME_SRC})
target_link_libraries(scp ${OpenCV_LIBS})
3.编译测试
进入到SaveCamPic目录下,执行如下命令进行编译:
cmake .
make
将编译生成的scp文件复制到BBB Debian系统根文件系统的$HOME目录下,并修改该文件的权限为可执行,并执行./scp命令进行测试(注意要先将USB摄相头接到BBB板上)。
网友评论