v4l.h /* * w3v4l.h * * Copyright (C) 1998 - 2000 Rasca, Berlin * EMail: * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef __W3V4L_H__ #define __W3V4L_H__
class CV4L{ public: CV4L(); CV4L(char *szDevName); ~CV4L();
bool init(int channel, int width, int height); unsigned char *Read(); void destroy(); private: char *szDevName; bool initialized; unsigned char *m_Buff; int m_BuffSize; int fdVideo; int m_Width, m_Height; int m_MapSize; };
#endif
v4l.cpp
/* * v4l.c * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* v4l_init() * function: init the video device * references: * dev: device name. * input: channel number of video_channel structure. * width: width value of video_window structure * height: height value of video_window structure */ bool CV4L::init(int channel, int width, int height) { int fd; struct video_capability vid_caps; struct video_mbuf vid_mbuf; struct video_channel vid_chnl;
// open the video device fd = open (szDevName, O_RDWR); if (fd == -1) { perror (szDevName); return false; } fdVideo = fd;
// get video_capability structrue if (ioctl (fd, VIDIOCGCAP, &vid_caps) == -1) { perror ("ioctl (VIDIOCGCAP)"); return false; }
// get the buffer information in video_mbuf structure // if can't use mmap() if (ioctl (fd, VIDIOCGMBUF, &vid_mbuf) == -1) { struct video_window vid_win; m_MapSize = 0;
// set video window information if (ioctl(fd, VIDIOCGWIN, &vid_win) != -1) { vid_win.width = width; vid_win.height= height; ioctl (fd, VIDIOCSWIN, &vid_win); } } else { m_MapSize = vid_mbuf.size; m_BuffSize = m_MapSize; } #ifdef DEBUG printf ("%s: mbuf.size=%d\n", __FILE__, vid_mbuf.size); #endif
////////////////////////////////////////////////////////////// // 1. Create my video class CV4L. cVid = new CV4L("/dev/video0");
////////////////////////////////////////////////////////////// // 2. Init the video device with channel and map size. if (cVid->init(0, VID_W, VID_H) == false) return -1;
//FillMyBitmap(&myBmp); FillBitmap(&bmp); ////////////////////////////////////////////////////////////// // 3. Read the data from video device. if (buf = cVid->Read ()) { bmp.bmBits = buf; //InvalidateRect (); SendMessage (hMainWnd, MSG_PAINT, 0, 0); }
while (true) { if (!HavePendingMessage (hMainWnd)) { if (!GetMessage (&Msg, hMainWnd)) break; TranslateMessage (&Msg); DispatchMessage (&Msg); } else { ////////////////////////////////////////////////////////////// // 3. Read the data from video device. if (buf = cVid->Read ()) { bmp.bmBits = buf; SendMessage (hMainWnd, MSG_PAINT, 0, 0); } else { // if Buffer is Null, vedeo device have pluged out. PostQuitMessage (hMainWnd); } /* end of read video buffer */ } /* end of HavePendingMessage() */ } /* end of while */
////////////////////////////////////////////////////////////// // 4. destroy the CV4L class, and release resources. cVid->destroy (); UnloadBitmap (&bmp); //free (bmp.bmBits);
MainWindowThreadCleanup (hMainWnd);
return 0; }
#ifndef _LITE_VERSION #include #endif
把这些代码添加到miniugi-1.3.3的helloworld程序中去编译一下就可以了,也可以用如下命令 g++ -o video -I/minigui/include -L/minigui/lib video.cpp v4l.cpp