16,215
社区成员
发帖
与我相关
我的任务
分享
#ifndef CAMERA_H
#define CAMERA_H
#include <QWidget>
#include <QTimer>
#include <QImage>
namespace Ui {
class camera;
}
class camera : public QWidget
{
Q_OBJECT
public:
explicit camera(QWidget *parent = 0);
~camera();
void do_view();
void do_capture();
private:
Ui::camera *ui;
QTimer *timer;
unsigned char *pp;
QImage *frame;
int video_fd ;
public slots:
void readFrame();
private slots:
void on_capButton_clicked();
void on_exitButton_clicked();
void on_pushButton_clicked();
};
#endif // CAMERA_H
#include "camera.h"
#include "ui_camera.h"
#include "mycamera.h"
#include <QMessageBox>
camera::camera(QWidget *parent) :
QWidget(parent),
ui(new Ui::camera)
{
ui->setupUi(this);
video_fd = -1;
}
camera::~camera()
{
delete ui;
}
void camera::readFrame()
{
if(video_fd>0)
{
// update();
read(video_fd,pp,320*240*2);
frame = new QImage(pp,320,240,QImage::Format_RGB16);
frame->loadFromData((unsigned char*)pp,320*240*2*sizeof(char));
ui->label->setPixmap(QPixmap::fromImage(*frame,Qt::AutoColor));
free(pp);
pp = (unsigned char*)malloc(320*240*2*sizeof(char));
}
}
void camera::do_view()
{
int ret ;
// int video_fd =-1;
pp = (unsigned char*)malloc(320*240*2*sizeof(char));
frame = new QImage(pp,320,240,QImage::Format_RGB16);
video_fd = open(VIDEO_DEV_NODE,O_RDWR);
if(video_fd<0)
QMessageBox::warning(this,tr("VIDEO_DEV_NODE"),tr("VIDEO_DEV_NODE OPEN ERROR"));
ret = read(video_fd,pp,320*240*2);
if(ret<0)
QMessageBox::warning(this,tr("VIDEO_READ"),tr("VIDEO_READ ERROR"));
//QPainter *painter =new QPainter(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(readFrame()));
timer->start(30);
// frame->loadFromData((unsigned char*)pp,320*240*2*sizeof(char));
// ui->label->setPixmap(QPixmap::fromImage(*frame,Qt::AutoColor));
// if(video_fd>0)
// close(video_fd);
// closefd(video_fd);
}
void camera::do_capture()
{
int video_rd = -1;
unsigned long dataSize = 0;
unsigned short rgb565Data[WIDTH*HEIGHT];
unsigned char rgb24Data[WIDTH*HEIGHT*2];
if(video_fd>0)
{
dataSize = WIDTH*HEIGHT ;
video_rd = read(video_fd,rgb565Data,dataSize*2);
if(video_rd<0)
QMessageBox::warning(this,tr("Read Waining"),tr("Read Error"));
fflush(stdout);
rgb565Torgb24(rgb565Data,WIDTH,HEIGHT,rgb24Data);
setBmpHeader(rgb24Data,WIDTH,HEIGHT,"lkvv.bmp");
fflush(stdout);
}
}
void camera::on_pushButton_clicked()
{
//QTimer *time = new QTimer(this);
//connect(time,SIGNAL(timeout()),this,SLOT(do_view()))
// time->start()
do_view();
}
void camera::on_exitButton_clicked()
{
closefd(video_fd);
ui->label->clear();
timer->stop();
}
void camera::on_capButton_clicked()
{
do_capture();
}
#ifndef MYCAMERA_H
#define MYCAMERA_H
#include <sys/time.h>
#include <sys/types.h>
#include <asm/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#define VIDEO_DEV_NODE "/dev/video"
#define WIDTH 320
#define HEIGHT 240
#define RGB565_MASK_RED 0xF800 //reg
#define RGB565_MASK_GREEN 0x07E0 //green
#define RGB565_MASK_BLUE 0x001F //blue
typedef struct
{
unsigned short bmpType;
unsigned long bmpSize;
unsigned long bmpRev;
unsigned long bmpOffset;
unsigned long bmpHeaderSize;
long bmpWidth;
long bmpHeight;
unsigned short bmpPlanes;
unsigned short bmpCount;
unsigned long biCompression;
unsigned long biSizeImage;
signed long bmpHPelsPerMeter;
signed long bmpVPelsPerMeter;
unsigned long bmpColors;
unsigned long bmpImportant;
} __attribute__((packed)) bmp_header ;
int setBmpHeader(unsigned char *bmpBuff,unsigned long width,unsigned long height,char *filepath)
{
int imageSize,i;
int fd =-1;
bmp_header bmp;
imageSize = width*height*3;
bmp.bmpType = 0x4D42;
bmp.bmpSize = imageSize + sizeof(bmp_header);
bmp.bmpRev = 0;
bmp.bmpOffset=bmp.bmpSize - imageSize;
bmp.bmpHeaderSize=0x28;
bmp.bmpWidth=width;
bmp.bmpHeight=height;
bmp.bmpPlanes=1;
bmp.bmpCount=24;
bmp.biCompression=0;
bmp.biSizeImage=imageSize;
bmp.bmpHPelsPerMeter=0;
bmp.bmpVPelsPerMeter=0;
bmp.bmpColors=0;
bmp.bmpImportant=0;
fd=open(filepath,O_CREAT|O_RDWR);
i = write(fd,&bmp,sizeof(bmp_header));
printf("Write bmp %d bytes\n",i);
i = write(fd,bmpBuff,imageSize);//read image date to fd
printf("Write bmp date %d bytes\n",i);
//lseek(fd,2,SEEK_SET);
close(fd);
return 1;
}
void rgb565Torgb24(unsigned short rgb565Date[],unsigned long width,unsigned long height,unsigned char rgb24Date[])
{
unsigned short *pRgb565 = rgb565Date ;
int SIZE = width*height;
int i;
for(i=0;i<SIZE;i++)
{
int r,g,b;
b = ((*pRgb565)&RGB565_MASK_BLUE);
g = ((*pRgb565)&RGB565_MASK_GREEN)>>5;
r = ((*pRgb565)&RGB565_MASK_RED)>>11;
rgb24Date[i*3+0] = (b<<3)|(b>>2);//youhua suanfa
rgb24Date[i*3+1] = (g<<2)|(g>>4);
rgb24Date[i*3+2] = (r<<3)|(r>>2);
pRgb565++;
}
}
void closefd(int fd)
{
if(fd>0)
close(fd);
}
//void readFrame(int fd)
//{
// if(fd>0)
//}
#endif // MYCAMERA_H
#include "camera.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
camera w;
w.show();
return a.exec();
}