歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Camera 驅動和編程

Camera 驅動和編程

日期:2017/3/1 10:04:03   编辑:Linux編程

了解了framebuffer,攝像頭便只是fb的數據來源而已。

先了解些相關的概念:

V4L2(video 4 linux 2)  

可以支持多種設備,它可以有以下幾種接口:

  1. 視頻采集接口(video capture interface):這種應用的設備可以是高頻頭或者攝像頭.V4L2的最初設計就是應用於這種功能的.

  2. 視頻輸出接口(video output interface):可以驅動計算機的外圍視頻圖像設備--像可以輸出電視信號格式的設備.

  3. 直接傳輸視頻接口(video overlay interface):它的主要工作是把從視頻采集設備采集過來的信號直接輸出到輸出設備之上,而不用經過系統的CPU.

  4. 視頻間隔消隱信號接口(VBI interface):它可以使應用可以訪問傳輸消隱期的視頻信號.

  5. 收音機接口(radio interface):可用來處理從AM或FM高頻頭設備接收來的音頻流.

Video4linux下視頻編程的流程:

(1)打開視頻設備:

(2)讀取設備信息

(3)更改設備當前設置(沒必要的話可以不做)

(4)進行視頻采集,兩種方法:

a.內存映射

b.直接從設備讀取

(5)對采集的視頻進行處理

(6)關閉視頻設備。

/* 這樣的流程地球人都曉得 */

關鍵步驟介紹 :

(1)打開視頻:

Open(”/dev/video0”,vdàfd);

關閉視頻設備用close(”/dev/video0”,vdàfd);

(2)讀video_capability中信息

ioctl(vd->fd, VIDIOCGCAP, &(vd->capability))

成功後可讀取vd->capability各分量 eg.

(3)讀video_picture中信息

ioctl(vd->fd, VIDIOCGPICT, &(vd->picture));

(4)改變video_picture中分量的值 (可以不做的)

先為分量賦新值,再調用VIDIOCSPICT

Eg.

vd->picture.colour = 65535;

if(ioctl(vd->fd, VIDIOCSPICT, &(vd->picture)) < 0)

{

perror("VIDIOCSPICT");

return -1;

}

(5)初始化channel (可以不做的)

必須先做得到vd->capability中的信息

for (i = 0; i < vd->capability.channels; i++)

{

vd->channel[i].channel = i;

if (ioctl(vd->fd, VIDIOCGCHAN, &(vd->channel[i])) < 0)

{

perror("v4l_get_channel:");

return -1;

}

}

/* 通過ioctl,將struct v4l_struct作為參數的設備操作而已,那麼重點將是整個結構體:struct v4l_struct*/

typedef struct v4l_struct

{

int fd;

struct video_capability capability;

struct video_channel channel[4];

struct video_picture picture;

struct video_window window;

struct video_capture capture;

struct video_buffer buffer;

struct video_mmap mmap;

struct video_mbuf mbuf;

unsigned char *map;

int frame;

int framestat[2];

}vd;

/* 那麼,對該結構體的了解便成了關鍵*/

Video4linux支持的數據結構及其用途

(1)video_capability 包含設備的基本信息(設備名稱、支持的最大最小分辨率、信號源信息等),包含的分量:

name[32] //設備名稱

maxwidth ,

maxheight,

minwidth,

minheight

Channels //信號源個數

type //是否能capture,彩色還是黑白,是否能裁剪等等。值如VID_TYPE_CAPTURE等

(2)video_picture設備采集的圖象的各種屬性

brightness 0~65535

hue

colour

contrast

whiteness

depth // 24

palette // VIDEO_PALETTE_RGB24

(3)video_channel 關於各個信號源的屬性

Channel //信號源的編號

name

tuners

Type // VIDEO_TYPE_TV | IDEO_TYPE_CAMERA

Norm制式

(4)video_window //包含關於capture area的信息

x x windows 中的坐標.

y x windows 中的坐標.

width The width of the image capture.

height The height of the image capture.

chromakey A host order RGB32 value for the chroma key.

flags Additional capture flags.

clips A list of clipping rectangles. (Set only)

clipcount The number of clipping rectangles. (Set only)

(5)video_mbuf //利用mmap進行映射的幀的信息

size //每幀大小

Frames //最多支持的幀數

Offsets //每幀相對基址的偏移

(6)video_buffer 最底層對buffer的描述

void *baseBase // physical address of the buffer

int height // Height of the frame buffer

int width // Width of the frame buffer

int depth // Depth of the frame buffer

int bytesperline // Number of bytes of memory between the start of two adjacent lines

實際顯示的部分一般比它描述的部分小

(7)video_mmap //用於mmap

Copyright © Linux教程網 All Rights Reserved