自己寫一個(gè)上層的類似于適配器一樣的字符驅(qū)動(dòng),應(yīng)用層用IOCTL與該層聯(lián)系,再由該層往下調(diào)用,根據(jù)命令的不同調(diào)用攝像頭的不同函數(shù) 用什么軟件寫。這個(gè)隨意啊,gedit,vi,eclipse都可以。 弄到開發(fā)板就是通過網(wǎng)線或者SD卡啊
目前創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、云州網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
說明書上,應(yīng)該有一個(gè)管理攝像頭的IP地址,你用網(wǎng)線把攝像頭直接連到你電腦上,你在電腦上輸入IP地址段與攝像頭的管理IP地址段一個(gè)網(wǎng)段就可以了。然后在瀏覽器里輸入攝像頭的IP地址,就應(yīng)該可以進(jìn)入到攝像頭的管理界面了。
在linux下所有設(shè)備都是文件。所以對(duì)攝像頭的操作其實(shí)就是對(duì)文件的操作。USB攝像頭的設(shè)備文件就是在/dev目錄下的video0(假如只有一個(gè)攝像頭)。在linux下操作攝像頭就是使用v4l2對(duì)攝像頭進(jìn)行的操作,操作步驟如下
打開設(shè)備文件。
int fd=open(”/dev/video0″,O_RDWR);
2. 取得設(shè)備的capability,看看設(shè)備具有什么功能,比如是否具有輸入,或者音頻輸入輸出等。VIDIOC_QUERYCAP,struct v4l2_capability
v4l2_std_id std;
do {
ret= ioctl(fd, VIDIOC_QUERYSTD, std);
} while (ret == -1 ?errno == EAGAIN);
switch (std) {
case V4L2_STD_NTSC:
//……
case V4L2_STD_PAL:
//……
}
3. 選擇輸入,一個(gè)設(shè)備可以有多個(gè)輸入。VIDIOC_S_INPUT,struct v4l2_input(可不要)
4. 設(shè)置的制式和幀格式,制式包括PAL,NTSC,幀的格式個(gè)包括寬度和高度等。
VIDIOC_S_STD,VIDIOC_S_FMT,struct v4l2_std_id,struct v4l2_format
struct v4l2_format fmt;
memset ( fmt, 0, sizeof(fmt) );
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 320;
fmt.fmt.pix.height = 240;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
if (ioctl(fd, VIDIOC_S_FMT, fmt) 0)
{
printf("set format failed\n");
//return 0;
}
5. 向驅(qū)動(dòng)申請(qǐng)幀緩沖,一般不超過5個(gè)。struct v4l2_requestbuffers
struct v4l2_requestbuffers req;
memset(req, 0, sizeof (req));
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd,VIDIOC_REQBUFS,req) == -1)
{
perror("VIDIOC_REQBUFS error \n");
//return -1;
}
6.申請(qǐng)物理內(nèi)存
將申請(qǐng)到的幀緩沖映射到用戶空間,這樣就可以直接操作采集到的幀了,而不必去復(fù)制。將申請(qǐng)到的幀緩沖全部入隊(duì)列,以便存放采集到的數(shù)據(jù).VIDIOC_QBUF,struct v4l2_buffer
VideoBuffer* buffers = calloc( req.count, sizeof(VideoBuffer) );
printf("sizeof(VideoBuffer) is %d\n",sizeof(VideoBuffer));
struct v4l2_buffer buf;
for (numBufs = 0; numBufs req.count; numBufs++)
{
memset( buf, 0, sizeof(buf) );
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = numBufs;
if (ioctl(fd, VIDIOC_QUERYBUF, buf) 0)
{
printf("VIDIOC_QUERYBUF error\n");
//return -1;
}
printf("buf len is %d\n",sizeof(buf));
//內(nèi)存映射
buffers[numBufs].length = buf.length;
buffers[numBufs].offset = (size_t) buf.m.offset;
buffers[numBufs].start = mmap (NULL, buf.length,PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
printf("buffers.length = %d,buffers.offset = %d ,buffers.start[0] = %d\n",buffers[numBufs].length,buffers[numBufs].offset,buffers[numBufs].start[0]);
printf("buf2 len is %d\n",sizeof(buffers[numBufs].start));
if (buffers[numBufs].start == MAP_FAILED)
{
perror("buffers error\n");
//return -1;
}
if (ioctl (fd, VIDIOC_QBUF, buf) 0)
{
printf("VIDIOC_QBUF error\n");
//return -1;
}
}
7. 開始的采集。
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl (fd, VIDIOC_STREAMON, type) 0)
{
printf("VIDIOC_STREAMON error\n");
// return -1;
}
8. 出隊(duì)列以取得已采集數(shù)據(jù)的幀緩沖,取得原始采集數(shù)據(jù)。VIDIOC_DQBUF, 將緩沖重新入隊(duì)列尾,這樣可以循環(huán)采集。VIDIOC_QBUF
if (ioctl(fd, VIDIOC_DQBUF, buf) 0)
{
perror("VIDIOC_DQBUF failed.\n");
//return -1;
}
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
unsigned char *ptcur = buffers[numBufs].start;
DEBUG("buf.bytesused = %d \n",buf.bytesused);
int i1;
for(i1=0; i1buf.bytesused; i1++)
{
if((buffers[numBufs].start[i1] == 0xFF) ?(buffers[numBufs].start[i1+1] == 0xC4))
{
DEBUG("huffman table finded! \nbuf.bytesused = %d\nFFC4 = %d \n",buf.bytesused,i1);
break;
}
}
if(i1 == buf.bytesused)printf("huffman table don't exist! \n");
int i;
for(i=0; ibuf.bytesused; i++)
{
if((buffers[numBufs].start[i] == 0xFF) ?(buffers[numBufs].start[i+1] == 0xD8)) break;
ptcur++;
}
DEBUG("i=%d,FF=%02x,D8=%02x\n",i,buffers[numBufs].start[i],buffers[numBufs].start[i+1]);
int imagesize =buf.bytesused - i;
DEBUG("buf.bytesused = %d \n",buf.bytesused);
DEBUG ("imagesize = %d \n",imagesize);
9. 停止的采集。VIDIOC_STREAMOFF
10. 關(guān)閉設(shè)備。close(fd);