- int main(int argc, const char* argv[])
- {
- int fp = 0;
- unsigned int i;
- /*
- * init struct camera
- */
- struct camera *cam;
- //這個是我自定義的結構,代表一個攝像頭,定義在v4l2.h中
- struct timeval tpstart,tpend;
- float timeuse;
- //以上變量是爲了統計每幀採集的時間
- unsigned short *pbuf;
- //幀緩存地址
- cam = malloc(sizeof(struct camera));
- if (!cam) {
- printf("malloc camera failure!\n");
- exit(1);
- }
- cam->device_name = "/dev/video0";
- cam->buffers = NULL;
- cam->width = IMAGE_WIDTH;
- cam->height = IMAGE_HEIGHT;
- cam->display_depth = 3; /* RGB24 */
- cam->rgbbuf = malloc(cam->width * cam->height * cam->display_depth);
- if (!cam->rgbbuf) {
- printf("malloc rgbbuf failure!\n");
- exit(1);
- }
- open_camera(cam);
- get_cam_cap(cam);
- get_cam_pic(cam);
- get_cam_win(cam);
- cam->video_win.width = cam->width;
- cam->video_win.height = cam->height;
- set_cam_win(cam);
- close_camera(cam);
- open_camera(cam);
- get_cam_win(cam);
- init_camera(cam);
- start_capturing (cam);
- //以上初始化攝像頭,設置採集圖像格式爲YUYV,採集圖像大小爲IMAGE_WIDTH×IMAGE_HEIGHT,mmap方式讀取數據
- fp = fb_init ("/dev/fb0");
- //打開初始化frambuffer設備,用mmap映射幀緩存地址爲fbbuf
- if (fp < 0){
- printf("Error : Can not open framebuffer device\n");
- exit(1);
- }
- pbuf = (unsigned short *)fbbuf;
- for (i = 0; i < 320 *240; i++) {
- pbuf[i] = make_pixel(0, 0x0, 0x0, 0xff);
- }
- //清屏成藍色
- #ifdef DEBUG_GTIME
- gettimeofday(&tpstart,NULL);
- #endif
- for (;;) {
- if (read_frame (cam)) {
- draw_image(pbuf, cam->rgbbuf);
- //將採集的數據顯示到屏幕上
- #ifdef DEBUG_GTIME
- gettimeofday(&tpend,NULL);
- timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);
- timeuse /= 1000000;
- printf("Used Time:%10f s\n",timeuse);
- gettimeofday(&tpstart,NULL);
- #endif
- //以上用gettimeofday函數統計採集一幀的時間
- }
- }
- return 0;
- }
int main(int argc, const char* argv[])
{
int fp = 0;
unsigned int i;
/*
* init struct camera
*/
struct camera *cam;
//這個是我自定義的結構,代表一個攝像頭,定義在v4l2.h中
struct timeval tpstart,tpend;
float timeuse;
//以上變量是爲了統計每幀採集的時間
unsigned short *pbuf;
//幀緩存地址
cam = malloc(sizeof(struct camera));
if (!cam) {
printf("malloc camera failure!\n");
exit(1);
}
cam->device_name = "/dev/video0";
cam->buffers = NULL;
cam->width = IMAGE_WIDTH;
cam->height = IMAGE_HEIGHT;
cam->display_depth = 3; /* RGB24 */
cam->rgbbuf = malloc(cam->width * cam->height * cam->display_depth);
if (!cam->rgbbuf) {
printf("malloc rgbbuf failure!\n");
exit(1);
}
open_camera(cam);
get_cam_cap(cam);
get_cam_pic(cam);
get_cam_win(cam);
cam->video_win.width = cam->width;
cam->video_win.height = cam->height;
set_cam_win(cam);
close_camera(cam);
open_camera(cam);
get_cam_win(cam);
init_camera(cam);
start_capturing (cam);
//以上初始化攝像頭,設置採集圖像格式爲YUYV,採集圖像大小爲IMAGE_WIDTH×IMAGE_HEIGHT,mmap方式讀取數據
fp = fb_init ("/dev/fb0");
//打開初始化frambuffer設備,用mmap映射幀緩存地址爲fbbuf
if (fp < 0){
printf("Error : Can not open framebuffer device\n");
exit(1);
}
pbuf = (unsigned short *)fbbuf;
for (i = 0; i < 320 *240; i++) {
pbuf[i] = make_pixel(0, 0x0, 0x0, 0xff);
}
//清屏成藍色
#ifdef DEBUG_GTIME
gettimeofday(&tpstart,NULL);
#endif
for (;;) {
if (read_frame (cam)) {
draw_image(pbuf, cam->rgbbuf);
//將採集的數據顯示到屏幕上
#ifdef DEBUG_GTIME
gettimeofday(&tpend,NULL);
timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);
timeuse /= 1000000;
printf("Used Time:%10f s\n",timeuse);
gettimeofday(&tpstart,NULL);
#endif
//以上用gettimeofday函數統計採集一幀的時間
}
}
return 0;
}
- static void draw_image(unsigned short *pbuf, unsigned char *buf)
- {
- unsigned int x,y;
- unsigned int pixel_num;
- if (WINDOW_W <= 240) {
- for (y = WINDOW_Y; y < WINDOW_Y + WINDOW_H; y++) {
- for (x = WINDOW_X; x < WINDOW_X + WINDOW_W; x++) {
- pixel_num = ((y - WINDOW_Y) * IMAGE_WIDTH + x - WINDOW_X) * 3;
- pbuf[y * 240 + x] = make_pixel(0, (unsigned int)buf[pixel_num],
- (unsigned int)buf[pixel_num + 1], (unsigned int)buf[pixel_num + 2]);
- }
- }
- } else { /* reverse */
- for (x = 0; x < WINDOW_W; x++) {
- for (y = 0; y < WINDOW_H; y++) {
- pixel_num = (y * IMAGE_WIDTH + x) * 3;
- pbuf[x * 240 + y] = make_pixel(0, (unsigned int)buf[pixel_num],
- (unsigned int)buf[pixel_num + 1], (unsigned int)buf[pixel_num + 2]);
- }
- }
- }
- }
static void draw_image(unsigned short *pbuf, unsigned char *buf)
{
unsigned int x,y;
unsigned int pixel_num;
if (WINDOW_W <= 240) {
for (y = WINDOW_Y; y < WINDOW_Y + WINDOW_H; y++) {
for (x = WINDOW_X; x < WINDOW_X + WINDOW_W; x++) {
pixel_num = ((y - WINDOW_Y) * IMAGE_WIDTH + x - WINDOW_X) * 3;
pbuf[y * 240 + x] = make_pixel(0, (unsigned int)buf[pixel_num],
(unsigned int)buf[pixel_num + 1], (unsigned int)buf[pixel_num + 2]);
}
}
} else { /* reverse */
for (x = 0; x < WINDOW_W; x++) {
for (y = 0; y < WINDOW_H; y++) {
pixel_num = (y * IMAGE_WIDTH + x) * 3;
pbuf[x * 240 + y] = make_pixel(0, (unsigned int)buf[pixel_num],
(unsigned int)buf[pixel_num + 1], (unsigned int)buf[pixel_num + 2]);
}
}
}
}
- #define IMAGE_WIDTH 320 //採集視頻的寬度
- #define IMAGE_HEIGHT 240 //採集視頻的高度
- #define WINDOW_W 176 //顯示視頻的寬度
- #define WINDOW_H 144 //顯示視頻的高度
- #define WINDOW_X 40 //顯示起始橫座標
- #define WINDOW_Y 60 //顯示起始縱座標
#define IMAGE_WIDTH 320 //採集視頻的寬度
#define IMAGE_HEIGHT 240 //採集視頻的高度
#define WINDOW_W 176 //顯示視頻的寬度
#define WINDOW_H 144 //顯示視頻的高度
#define WINDOW_X 40 //顯示起始橫座標
#define WINDOW_Y 60 //顯示起始縱座標
- static inline int make_pixel(unsigned int a, unsigned int r, unsigned int g, unsigned int b)
- {
- return (unsigned int)(((r >> 3) << 11) | ((g >> 2) << 5 | (b >> 3)));
- }
static inline int make_pixel(unsigned int a, unsigned int r, unsigned int g, unsigned int b)
{
return (unsigned int)(((r >> 3) << 11) | ((g >> 2) << 5 | (b >> 3)));
}
這個函數就是將RGB24格式轉換成RGB16的格式。3 性能分析