/**************************************************************************************************
* 这是从OpenCV的源代码包里找到的代码示例
* 检测摄像头,捕捉图像
*
* clyfk 19:09 2016-4-30
*************************************************************************************************/
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void drawText(Mat & image);
int main()
{
cout << "Built with OpenCV " << CV_VERSION << endl;
Mat image;
VideoCapture capture;
capture.open(0); // 打开设备-即摄像头
if(capture.isOpened()) // 如果成功打开摄像设备
{
cout << "Capture is opened" << endl;
for(;;)
{
capture >> image; //捕捉到的图像给image
if(image.empty())
break;
drawText(image);
imshow("Sample", image); // 显示图像在Sample窗口上
if(waitKey(10) >= 0)
break;
}
}
else // 没有捕捉到摄像-打不开摄像头
{
cout << "No capture" << endl;
image = Mat::zeros(480, 640, CV_8UC1);
drawText(image);
imshow("Sample", image);
waitKey(0);
}
return 0;
}
void drawText(Mat & image)
{
// 插入文字 参数为:承载的图片,插入的文字,文字的位置,字体,大小,颜色......
putText(image, "Hello OpenCV",
Point(20, 50),
FONT_HERSHEY_COMPLEX, 1, // font face and scale
Scalar(255, 255, 255), // white
1, LINE_AA); // line thickness and type
}