


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、qt 下利用 opencv 从摄像头读出图片并显示源码2012-09-30 11:59:32| 分类: qt and qtopia | 标签:camera opencv qt |举报|字号 订阅/*camera.h*/#ifndef _demo_h_ #define _demo_h_#include <qwidget>#include <highgui.h> #include <cv.h> #include <qtimer> #include <qimage> #include <qpainter> #include &l
2、t;qtgui> #include <qthread> #include <qtnetwork> #include <qapplication>#include “./jpeglib.h“#include “operation.h“ #include “tcpsocket.h“#define port3343/线程类class sendthread : public qthreadpublic:sendthread(void); void run();void setimage(qimage & image);public:send_pictu
3、re *socket; qimage val;/*camera_operation 类,用来显示摄像头图像采集界面*1. 打开摄像头*2. 开头采集*3. 显示*4. 暂停*/class camera_operation : public qdialogq_objectpublic:camera_operation(qwidget *parent = 0,int i=0);camera_operation();qtabwidget *tabwidget; qwidget *mainwidget; qwidget *twowidget; cvcapture *capture; iplimage
4、*frame;qimage image; qtimer *timer; qpushbutton *save; qpushbutton *play;qpushbutton *puspend; qpushbutton *learn; qpushbutton *judge; qpushbutton *add; qpushbutton *creturn;/ qudpsocket *socket;/udp 协议,传图片send_picture *socket; sendthread *thread;public slots:void save_camera(void); void play_camera
5、(void); void puspend_camera(void); void read_camera(void); void return_home(void);protected:void timerevent(qtimerevent *event); void paintevent(qpaintevent *);#endif/-/*camera.cpp*/#include <qtgui> #include “highgui.h“ #include “cv.h“ #include <qtimer> #include <qimage> #include &
6、lt;qpainter> #include <qtgui>#include <qapplication> #include <qdebug>#include “camera.h“ #include “./def.h“#define jpeg_quality 80char * image_buffer; /* points to large array of r,g,b-order data */ int image_height; /* number of rows in image */int image_width; /* number of co
7、lumns in image */*函数名字:write_jpeg_file函数功能:压缩一张图片到内存参数:返 回 值:*/ unsigned long write_jpeg_file (unsigned char *target,char *buffer, int width,int height)struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; jsamprow row_pointer1;int row_stride; unsigned long size;/指定错误处理器cinfo.err = jpeg_std
8、_error(&jerr); jpeg_create_compress(&cinfo); jpeg_mem_dest(&cinfo,&target,&size);/指定宽度,高度,rgbcinfo.image_width = 320;cinfo.image_height = 240;cinfo.input_components = 3; cinfo.in_color_space = jcs_rgb;/接受默认设置jpeg_set_defaults(&cinfo);/设置图片质量jpeg_set_quality(&cinfo, 80, tr
9、ue);/开头压缩jpeg_start_compress(&cinfo, true); row_stride = cinfo.image_width * 3;while (cinfo.next_scanline < cinfo.image_height) row_pointer0 =(jsamprow) & buffercinfo.next_scanline * row_stride; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);jpeg_finish_compress(&cinfo); jpeg
10、_destroy_compress(&cinfo);return size;sendthread : sendthread(void)socket = new send_picture(3343);/*线程 run 方法,用来发送图片到上位机*/void sendthread : run(void)if(socket -> state = connect_on) qbytearray pic;qbuffer buffer(&pic); buffer.open(qiodevice:readwrite); val.save(&buffer, “png“);qdebug
11、() << buffer.size(); socket -> write(pic);void sendthread : setimage(qimage & image)val = image;camera_operation : camera_operation(qwidget *parent,int i) : qdialog(parent)setgeometry(winxstart,winystart,usewinwide,usewinhigh);/ mainwidget = new qwidget();play = new qpushbutton(“play“,t
12、his); play -> setgeometry(10,20,75,40);puspend = new qpushbutton(“puspend“,this); puspend -> setgeometry(10,70,75,40);save = new qpushbutton(“save“,this); save -> setgeometry(10,120,75,40);creturn = new qpushbutton(“return“,this); creturn -> setgeometry(10,170,75,40);learn = new qpushbut
13、ton(“learn“,this); learn -> setgeometry(270,220,65,40);judge = new qpushbutton(“judge“,this); judge -> setgeometry(340,220,65,40);add = new qpushbutton(“add“,this); add -> setgeometry(410,220,65,40);timer = new qtimer(this);/ if(i=0)if(!(capture = cvcreatecameracapture(-1)qmessagebox:about(
14、this,“warning“,“cant open the camero“); return;/ connect(save,signal(clicked(),this,slot(save_camera(); connect(play,signal(clicked(),this,slot(play_camera(); connect(puspend,signal(clicked(),this,slot(puspend_camera(); connect(creturn,signal(clicked(),this,slot(return_home(); connect(timer,signal(t
15、imeout(),this,slot(read_camera();/ socket = new send_picture(3343); timer -> start(30);camera_operation : camera_operation()cvreleaseimage(&frame);/释放cvreleasecapture(&capture);/*保存图片到 nand flash 下/home/picture 名目,文件名从 1.png 开头递增,文件最大数量 199*/void camera_operation : save_camera(void)static
16、 int i=0;char filename25; if(i>200)i=0;i+;sprintf(filename,“/home/picture/%d.bmp“,i); qdebug() << filename; image.save(filename);/*开头播放,打开定时器*/void camera_operation : play_camera(void)/ starttimer(50); timer -> start(30);/*暂停播放,停止定时器*/void camera_operation : puspend_camera(void)timer -&g
17、t; stop();/ cvreleaseimage(&frame);void camera_operation : return_home(void)timer -> stop(); cvreleaseimage(&frame);/释放cvreleasecapture(&capture); close();/*从摄像头中读取一张图片,并保存到 qimage 中*/void camera_operation : read_camera(void)char *buffer; frame=cvqueryframe(capture); if(frame=0)qmessa
18、gebox:about(this,“warning“,“can”t query the frame“); timer -> stop();return;/图像编码image_buffer = (char*)malloc(512*1025);struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; jsamprow row_pointer1;int row_stride; unsigned long size;/指定错误处理器cinfo.err = jpeg_std_error(&jerr); jpeg_creat
19、e_compress(&cinfo);jpeg_mem_dest(&cinfo,(unsigned char*)&(image_buffer),&size);/指定宽度,高度,rgb cinfo.image_width = frame->width; cinfo.image_height = frame->height; cinfo.input_components = 3; cinfo.in_color_space = jcs_rgb;/接受默认设置jpeg_set_defaults(&cinfo);/设置图片质量jpeg_set_qual
20、ity(&cinfo, 80, true);/开头压缩jpeg_start_compress(&cinfo, true); row_stride = cinfo.image_width * 3;while (cinfo.next_scanline < cinfo.image_height) row_pointer0 =(jsamprow) & frame->imagedatacinfo.next_scanline * row_stride;(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);jpeg
21、_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo);printf(“%dn“,size);qdebug() << “size = “ ; qdebug() << size;qdebug() << “width = “ ; qdebug() << frame->width; qdebug() << “height = “ ; qdebug() << frame->height;qimage qimage(uchar *)frame->imagedata, frame->width, frame->height, fra
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第14课 光荣的少先队 课件-2024-2025学年道德与法治一年级下册统编版
- 酒店培训感想心得体会模版
- 医院庭院绿化的生态效益与社会价值
- 周围型肺癌的临床护理
- 黄绿卡通动物交通安全模板
- 医疗大数据与健康保险的联动发展
- 婴儿脐疝的临床护理
- 区块链技术助力文字作品版权保护
- 健身房加设施合同范例
- 安全管理知识培训
- 《纺织材料生产》课件-项目6:纺丝工段
- 车辆维修保养服务 投标方案(技术方案)
- 2023-2024学年人教版八年级下册数学期中复习试卷
- 高考数学专题:导数大题专练(含答案)
- 部编版八年级语文下册第11课《核舟记》教学课件
- 角膜溃疡护理常规
- GB/T 43706-2024资产管理战略资产管理计划(SAMP)实施指南
- 护理交接班不全课件
- 腹痛的护理措施
- 2023年-2024年职业卫生检测考试题库及答案
- 《我家漂亮的尺子》课件-定稿
评论
0/150
提交评论