QT调用摄像头问题

qq_22575563 2018-12-08 09:54:53

本人使用的使用飞凌OKMX6UL-C的开发板,摄像头使用飞凌配套的ov9650,系统已经做好驱动程序,通过查看手册,输入命令可以看到在屏幕上有图像,但是无法采集图像,最近在学QT界面编程,想做一个摄像头的界面,参考了网上的一些程序,不知怎么回事出现段错误,还是无法实现摄像头的调用,
本人小白,不知道该怎么改,跪求大神的指教
#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();
}
...全文
533 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
_松 2019-01-30
  • 打赏
  • 举报
回复
qt摄像头的demo是哪里获取的?@不易易
不易易 2018-12-12
  • 打赏
  • 举报
回复
我运行过qt的摄像头demo,不管是笔记本自带的摄像头还是usb插入的摄像头,都能识别显示;
不清楚你这个情况。
kerwin liu 2018-12-10
  • 打赏
  • 举报
回复
1.为什么是320*240*2而不是320*240*3 ?
2.frame明显的会出现内存泄露.程序出错是可以通过GDB调试。
3.如果没有猜错,你的摄像头模块应该是个USB设备,检查一下read这一块
我也没用过这个模块,也没相关的环境。只是根据你的代码说说我的看法。希望能帮到你
彩阳 2018-12-08
  • 打赏
  • 举报
回复
先占用一个楼,我们没有开发环境,感觉这个比较难解决。

16,215

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧