基于树莓派的数码相机———C/C++
去年做的一个小相机,当然网上有很多大佬做的很不错。就是想起来做的时候查看各种资料比较辛苦,所以就分享出来让大家做个参考,因为用的开发平台是QT程序是能运行的就是大家下载下来的时候可能环境配置和路径等方面的原因无法在自己的电脑上编译,程序可以下下来做个参考。若有不足之处望大家提出建议,在此谢过了...
下面就来介绍一下相机的具体功能吧:
(1):打开摄像头
(2):拍照(图片预览)
(3):连拍、延时拍照
(4):图片处理、加滤镜(灰度、冷色、暖色、冷色)
(5):录像
(6):图片上传微博
开发环境QT5图像处理方面用的有opencv还有QT自带的图像处理库函数QImage等,上传微博需要自己申请微博的API接口和制作跳转网页。
开始前先安装opencv库,树莓派内存一定要大最少16G吧8G肯定不够用,安装过程自行百度。然后在项目的pro文件中手动添加opencv库的链接路径,如下图所示:
(1):void openCamaraSlot()函数用于打开树莓派外接的摄像头模块
void Widget::openCamaraSlot()
{
capture.open(-1); //打开摄像头(-1默认为树莓派的摄像头)
if( !capture.isOpened()) //若摄像头打开失败
{
qDebug()<< "open camera error"<<endl; //输出“open camera error”
this->close();
}
else
{
timer->start(33);//若打开摄像头成功,则33毫秒后启动摄像头
}
}
(2)图片的处理,在ui上设置gry、warm等按钮,当按钮别点击时则对图片进行相应的处理。
if(gry=='t'){ //若灰度按钮被点击
int w,h;
w = img.width(); //获取图片的宽度值
h = img.height(); //获取图片的高度值
QImage iGray(w,h,QImage::Format_RGB32);
for(int i=0; i<w; i++){
for(int j=0;j<h; j++){
QRgb pixel = img.pixel(i,j); //获取图片的每一个像素点的值
int gray = qGray(pixel); //计算像素点灰度值的函数
QRgb grayPixel = qRgb(gray,gray,gray); //把像素点的r、g、b全设置为gray并转化为像素点值
iGray.setPixel(i,j,grayPixel); //设置图片iGray的像素点的值为新的像素值grayPixel
}
else if(warm=='t'){ //若暖色调按钮被点击
int w,h;
int delta=30;
w = img.width();
h = img.height();
QImage iwarm(w,h,QImage::Format_RGB32);
QColor oldColor;
int r,g,b;
for(int x=0; x<w; x++){ //设置暖色调的算法
for(int y=0; y<h; y++){
oldColor = QColor(img.pixel(x,y));
r = oldColor.red() + delta;
g = oldColor.green() + delta;
b = oldColor.blue();
r = qBound(0, r, 255);
g = qBound(0, g, 255);
iwarm.setPixel(x,y, qRgb(r,g,b));
}
}
ui->cameraPic->setPixmap(QPixmap::fromImage(iwarm));
}
(3):连拍、延时拍照
void Widget::on_LianP_clicked() //5连拍
{
for(int n=5;n>0;n--){
QTimer::singleShot(200,this,SLOT(takingPicturesSlot()));//200毫秒后触发拍照
}
}
void sleep(unsigned int ms)//延时拍照
{
QTime reachTime =QTime::currentTime().addMSecs(ms); //设置定时器reachTime,reachTime等于当前时间QTime::currentTime()加
//需要延缓时间addMSecs(ms)
while(QTime::currentTime()<reachTime)//防止死循环
QCoreApplication::processEvents(QEventLoop::AllEvents,1000);
}
(5):录像,自动创建avi格式的文件并以本地时间命名保存到相应的文件夹中
void Widget::on_video_clicked()//录像
{
CvVideoWriter* vvideo; //创建视频文件写入器
int n;
IplImage edges=(IplImage)frame;
IplImage* fframe=&edges;
vvideo=cvCreateVideoWriter("video.avi", CV_FOURCC('X', 'V', 'I', 'D'), 25,
cvSize(fframe->width,fframe->height));
if(vvideo)
{
cout<<"VideoWriter has created."<<endl;
}
int i=0;
while(i<=200)
{
fframe=&edges;
if(!fframe)
{
cout<<"finish video."<<endl;
break;
}
n=cvWriteFrame(vvideo,fframe);
cout<<n<<endl;
i++;
if(cvWaitKey(2)>0)
break;
}
cvReleaseVideoWriter(&vvideo);
cout<<"finish video"<<endl;
}
(6):图片上传微博,这个URL是自己写的网页地址可以自行修改,网页中选择登录并跳转到微博
void Widget::on_pushButton_clicked()//上传微博
{
QUrl url("http://127.0.0.1/weibo.php?filename="+dt+".png");
bool is_open = QDesktopServices::openUrl(url);
}
最终实现(UI丑了点效果还是可以的):
源码下载地址:https://download.****.net/my/uploads