我写的视频显示程序

 //mainwindow.h
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(unsigned char *);

public slots:
void grab();

private:
unsigned char *grabdata;
QMenuBar *menubar;
QMenu *fileMenu;
QLabel *label;
QAction *openAct;
QAction *exitAct;
};



//mainwindow.cpp
#i nclude "mainwindow.h"

MainWindow::MainWindow(unsigned char * data)
{
grabdata=data;

QFont font;
font.setFamily(QString::fromUtf8("Nimbus Sans L"));
font.setPointSize(12);
font.setBold(false);
font.setItalic(false);
font.setUnderline(false);
font.setWeight(50);
font.setStrikeOut(false);
setFont(font);

QWidget *w=new QWidget;
setCentralWidget(w);

QWidget *topFiller=new QWidget;
topFiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

label=new QLabel(this);
setCentralWidget(label);

QWidget *bottomFiller=new QWidget;
bottomFiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

QVBoxLayout *vbox=new QVBoxLayout;
vbox->setMargin(5);
vbox->addWidget(topFiller);
vbox->addWidget(label);
vbox->addWidget(bottomFiller);
w->setLayout(vbox);

openAct=new QAction(tr("&Open..."),this);
openAct->setShortcut(tr("Ctrl+0"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct,SIGNAL(triggered()),this,SLOT(grab()));

exitAct=new QAction(tr("E&xit"),this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct,SIGNAL(triggered()),this,SLOT(close()));

fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(exitAct);

setWindowTitle(tr("MainWindow Template"));
setMinimumSize(320,240);
resize(320,240);
}


void MainWindow::grab()
{
//QPixmap pic(QString::fromUtf8(grab_data));
QImage image(grabdata,320,240,QImage::Format_RGB32);
QPixmap pixmap;
pixmap=pixmap.fromImage(image);
//Pixmap pic(image);
/*QRgb value;
value=qRgb(189,149,39);//函数参数表示r,g,b的值
image.setPixel(1,1,value);
QPaniter(this);
painter.drawImage(label,image,
*/
label->setPixmap(pixmap);
label->setFixedSize(pixmap.width(),pixmap.height());
}


//main.cpp
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

#i nclude
#i nclude
#i nclude

#define PAL_NC_WIDTH 320
#define PAL_NC_HEIGHT 240

#i nclude "mainwindow.h"

int main(int argc,char **argv)
{
//FILE * fp;
int grab_fd;
unsigned char * grab_data;

struct video_capability grab_cap;//包含摄像头的基本信息
struct video_picture grab_pic;//包含设备采集图像的各种属性 亮度 色调..
struct video_mmap grab_buf;//用于内存映射
struct video_mbuf grab_vm;//输入到摄像头存储器缓冲中的帧信息
grab_fd=open("/dev/video0",O_RDWR);
if(grab_fd==-1)
{
printf("Cann't open file/n");
exit(0);
}
else
printf("open successful/n");

ioctl(grab_fd,VIDIOCGCAP,&grab_cap);//读取struct video_capability中有关摄像头的信息
//printf("maxheight=%d/n",grab_cap.maxheight);
ioctl(grab_fd,VIDIOCGPICT,&grab_pic); //读取摄像头缓冲中voideo_picture信息
grab_pic.depth=3;//?
if(ioctl(grab_fd,VIDIOCSPICT,&grab_pic)<0)//用户空间程序中改变这些信息
{
perror("VIDIOCSPICT");
return -1;
}
ioctl(grab_fd,VIDIOCGMBUF,&grab_vm);//函数获得摄像头存储缓冲区的帧信息
grab_buf.height=240;
grab_buf.width=320;
grab_buf.format=VIDEO_PALETTE_RGB24;
grab_data=(unsigned char *)mmap(0,grab_vm.size,PROT_READ|PROT_WRITE,MAP_SHARED,grab_fd,0);
//把摄像头对应的设备文件映射到内存区
grab_buf.frame=0;//设置单帧采集
ioctl(grab_fd,VIDIOCMCAPTURE,&grab_buf); //调用成功,激活设备开始一帧图像的截取,非阻塞
ioctl(grab_fd,VIDIOCSYNC,&grab_buf.frame);//判断该帧图像是否截取完毕
/* fp=fopen("test24.ppm","w");
if(fp==NULL) return -1;
fprintf(fp,"P6/n%d %d/n255/n",PAL_NC_WIDTH,PAL_NC_HEIGHT);//什么意思
fwrite(grab_data,PAL_NC_HEIGHT,3*PAL_NC_WIDTH,fp);
fclose(fp);
*/
QApplication app(argc,argv);
MainWindow window(grab_data);
window.show();

close(grab_fd);
return app.exec();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值