android camera v4l2测试代码

  • R2_932048
    了解作者
  • 4.3KB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-14 08:09
    上传日期
hal层代码,测试前kernel驱动应该调试好。直接测试kernel层驱动,对于学习android cemera数据流buffer很有研究价值。
摄像头预览测试.7z
  • 摄像头预览测试
  • Android.mk
    255B
  • camera_preview_v2.c
    12.5KB
内容介绍
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h rel='nofollow' onclick='return false;'> #include <fcntl.h> /* low-level i/o */ #include <unistd.h> #include <errno.h> #include <malloc.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/time.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <asm/types.h rel='nofollow' onclick='return false;'> /* for videodev2.h */ #include <linux/videodev2.h> #include <linux/fb.h> #define LOG_TAG "CAMERA_TEST" #include <utils/Log.h> //#define SAVE_TO_FILE #define PIC_SET_FILE "/data/set0.yuv" //#define UVC_CAMERA #ifdef UVC_CAMERA #define PREVIEW_DEV "/dev/video1" #else #define PREVIEW_DEV "/dev/video2" #endif #define FRAMEBUFER_DEV "/dev/graphics/fb0" #define IMAGE_WIDTH 640 #define IMAGE_HEIGHT 480 #define LCD_WIDTH 480 #define LCD_HEIGHT 272 #define IMAGE_SIZE_YUV420 ((IMAGE_WIDTH*IMAGE_HEIGHT*3)/2) //460800 #define IMAGE_SIZE_YUV422 (IMAGE_WIDTH*IMAGE_HEIGHT*2) //61440 #define IMAGE_IN_FB_SIZE_RGB888 (LCD_WIDTH*LCD_HEIGHT*3) #define IMAGE_IN_FB_SIZE_RGB565 (LCD_WIDTH*LCD_HEIGHT*2) #define USE_BUF_NUM 4 static unsigned int buf_index = 0; static unsigned int max_buffers = 4; unsigned char *fb_buf = NULL; unsigned char *data_buf = NULL ; static int pic_set_fd; #define APP_ANDROID_DEBUG #ifdef APP_ANDROID_DEBUG #define MYLOGD(format, args...) do{LOGD("%d:%s()", __LINE__, __FUNCTION__); LOGD(format, ##args);}while(0) #else #define MYLOGD(format, args...) #endif struct buffer { void *start; size_t length; }user_buffers[4]; static int open_device(char *dev_name) { assert(dev_name); int fd = -1; fd = open(dev_name , O_RDWR); if( -1 == fd ) { MYLOGD("open %s fail: %s\n", dev_name, strerror (errno)); exit(EXIT_FAILURE); } MYLOGD("the fd of %s is %d ", dev_name, fd); return fd; } static int fimc_v4l2_querycap(int fp) { struct v4l2_capability cap; int ret = 0; ret = ioctl(fp, VIDIOC_QUERYCAP, &cap); if (ret < 0) { LOGE("ERR(%s):VIDIOC_QUERYCAP failed\n", __FUNCTION__); return -1; } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { LOGE("ERR(%s):no capture devices\n", __FUNCTION__); return -1; } return ret; } static int cam_v4l2_enuminput(int fp) { struct v4l2_input input; input.index = 0; if(ioctl(fp, VIDIOC_ENUMINPUT, &input) != 0) { LOGE("ERR(%s):No matching index found\n", __FUNCTION__); return -1; } LOGI("Name of input channel[%d] is %s\n", input.index, input.name); return input.index; } static int cam_v4l2_s_input(int fp, int index) { struct v4l2_input input; int ret; input.index = index; ret = ioctl(fp, VIDIOC_S_INPUT, &input); if (ret < 0) { LOGE("ERR(%s):VIDIOC_S_INPUT failed\n", __FUNCTION__); return ret; } return ret; } static int get_pix_depth(unsigned int pix_fmt) { int depth = 0; switch(pix_fmt) { case V4L2_PIX_FMT_YUV420: depth = 12; break; case V4L2_PIX_FMT_YUYV: depth = 16; break; default: LOGE("wrong pix format"); exit(1); } return depth; } static int cam_v4l2_s_format( int dev_fd, int height, int width, unsigned int fmt) { int ret = -1; struct v4l2_format my_format; bzero(&my_format, sizeof(my_format)); my_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; my_format.fmt.pix.height = height; my_format.fmt.pix.width = width; my_format.fmt.pix.pixelformat = fmt; my_format.fmt.pix.sizeimage = (height * width * get_pix_depth(V4L2_PIX_FMT_YUYV))/8; //my_format.fmt.pix.field = V4L2_FIELD_INTERLACED; // if jpeg //my_format.fmt.pix.bytesperline = 640 * 2 ; // driver will calc //my_format.fmt.pix.priv = 0; // can be zero ret = ioctl(dev_fd, VIDIOC_S_FMT, &my_format); if(-1 == ret ) { LOGE("set format error\n"); exit(1) ; } return ret; } static int cam_v4l2_reqbuf(int dev_fd, int num) { int ret = -1; struct v4l2_requestbuffers reqbuf; bzero(&reqbuf, sizeof(reqbuf)); reqbuf.count = num; reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; reqbuf.memory = V4L2_MEMORY_MMAP;// must be this ret = ioctl(dev_fd, VIDIOC_REQBUFS, &reqbuf);//to allocate buffer with dma in kernel space if( -1 == ret ) { LOGE("VIDIOC_REQBUFS fail: %s\n", strerror (errno)); exit(EXIT_FAILURE); } return ret; } static int cam_v4l2_querybuf(int dev_fd, struct v4l2_buffer *buf, int num) { int ret = -1; assert(buf != NULL); buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf->index = num; ret = ioctl(dev_fd, VIDIOC_QUERYBUF, buf); if(-1 == ret) { LOGE("VIDIOC_QUERYBUF error\n"); exit(EXIT_FAILURE); } return ret; } static void init_cam_mmap(int dev_fd) { MYLOGD("excuting..."); struct v4l2_buffer buf; int ret = -1; MYLOGD("VIDIOC_REQBUFS start ...,we need to do first"); cam_v4l2_reqbuf(dev_fd, USE_BUF_NUM); MYLOGD("mmap start..."); for(buf_index=0 ; buf_index < USE_BUF_NUM; buf_index++) { bzero(&buf, sizeof(struct v4l2_buffer)); cam_v4l2_querybuf(dev_fd, &buf, buf_index); user_buffers[buf_index].length = buf.length; user_buffers[buf_index].start = mmap(NULL /* start anywhere */ , buf.length, PROT_READ | PROT_WRITE /* required */ , MAP_SHARED /* recommended */ , dev_fd, buf.m.offset); if (MAP_FAILED == user_buffers[buf_index].start) { LOGE ("mmap buffers fail: %s\n", strerror (errno)); exit(EXIT_FAILURE); } } LOGD("init_cam_mmap done\n"); } static int cam_v4l2_g_fmt(int dev_fd) { struct v4l2_format my_fmt; int ret = -1; my_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ret = ioctl(dev_fd, VIDIOC_G_FMT, &my_fmt); if ( -1 == ret ) { LOGE("VIDIOC_G_FMT error \n"); return -EINVAL; }else { MYLOGD(" Width = %d\n", my_fmt.fmt.pix.width); MYLOGD(" Height = %d \n", my_fmt.fmt.pix.height); MYLOGD(" Image size = %d\n", my_fmt.fmt.pix.sizeimage); } return ret; } static int init_cam_device(int dev_fd) { int ret = -1; int input_index; //ret = fimc_v4l2_querycap(dev_fd); //assert(ret == 0); //获取到输入源通道 input_index = cam_v4l2_enuminput(dev_fd); assert(input_index == 0); ret = cam_v4l2_s_input(dev_fd, input_index); assert(ret == 0); MYLOGD("VIDIOC_S_FMT start... dev_fd = %d\n", dev_fd); ret = cam_v4l2_s_format(dev_fd, IMAGE_HEIGHT, IMAGE_WIDTH, V4L2_PIX_FMT_YUYV); assert(ret == 0); ret = cam_v4l2_g_fmt(dev_fd); assert(ret == 0); init_cam_mmap(dev_fd); return 1; } static int init_fb_device(int dev_fd) { MYLOGD("After openning FB \n"); int screensize; static struct fb_var_screeninfo vinfo; static struct fb_fix_screeninfo finfo; /*2.获取固定参数*/ if(ioctl(dev_fd,FBIOGET_FSCREENINFO,&finfo)) { printf("Error:failed get the framebuffer device`s fix informations!\n"); return -1; } /*3.获取可变参数*/ if(ioctl(dev_fd,FBIOGET_VSCREENINFO,&vinfo)) { printf("Error:failed get the framebuffer device`s var informations!\n"); return -1; } screensize =(vinfo.xres *vinfo.yres*vinfo.bits_per_pixel/8); printf("screensize =%ld\n",screensize); /*5.映射*/ fb_buf =(char *)mmap( NULL,screensize, PROT_READ|PROT_WRITE, MAP_SHARED, dev_fd, 0); if (MAP_FAILED == fb_buf) { LOGE("Error: failed to map framebuffer device to memory.\n"); close(dev_fd); exit(EXIT_FAILURE); } memset(fb_buf,0,screensize); return 1; } static int cam_v4l2_qbuf(int dev_fd, int index) { struct v4l2_buffer buf; int ret = -1; memset(&buf, 0 ,sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = index; ret = ioctl(dev_fd, VIDIOC_QBUF, &buf); if( -1 == ret ) { LOGE ("VIDIOC_QBUF fail: %s\n", strerror (errno)); exit(EXIT_FAILURE); }else { LOGD("VIDIOC_QBUF OK \n"); } return ret; } static int cam_v4l2_dqbuf(int dev_fd) { struct v4l2_buffer dq_buf; int ret = -1; memset(&dq_buf, 0 ,sizeof(struct v4l2_buffer)); dq_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; dq_buf.memory = V4L2_MEMORY_MMAP; ret = ioctl(dev_fd, VIDIOC_DQBUF, &dq_buf); if( -1 == ret ) { LOGE ("VIDIOC_DQBUF fail: %s\n", strerro
评论
    相关推荐
    • android
      掌握Android Studio开发环境的搭建步骤,能够独立搭建 Android Studio开发环境
    • android
      android
    • android
      Android回购
    • android
      android
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • androidandroid
      androidandroidandroidandroidandroidandroidandroid
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • Android
      Android
    • Android
      Android