//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();
}
#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();
}