上一页下一页说usb camera uvc标准 顺便说说CMOS一起做 操作基本一至, 前HAL在那里我已经提供了层CMOS相关接口
JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_yuvtorgb
假设使用和UVC一样的处理 图像显示不出来 所以用第二种方法 同一时候这里使用的是斯道ICOOL210开发板測试的 假设使用CMOS还须要改动一些地方 HAL层改动例如以下
首先添加一个函数例如以下
int select_input(int input){ int ret; ret = ioctl(fd, VIDIOC_S_INPUT, &input); if (ret) { printf("xioctl VIDIOC_S_INPUT failed+++++\n"); } return ret;}在 init里调用 init改动例如以下
JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_init(JNIEnv * env, jclass obj, jint width, jint height,jint numbuf,jint ctype){ int ret; int i; bufnum = numbuf; mwidth = width; mheight = height; c_type = ctype; struct v4l2_format fmt; struct v4l2_capability cap; if(c_type == 2) select_input(0); else if(c_type == 3) select_input(1); ret = ioctl(fd, VIDIOC_QUERYCAP, &cap); if (ret < 0) { LOGE("%d :VIDIOC_QUERYCAP failed\n",__LINE__); return -1; } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { LOGE("%d : no capture devices\n",__LINE__); return -1; } memset( &fmt, 0, sizeof(fmt)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if(c_type > 0) fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; else fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565; if(c_type > 1) { fmt.fmt.pix.field = V4L2_FIELD_NONE;//V4L2_FIELD_INTERLACED;//V4L2_FIELD_NONE; fmt.fmt.pix.priv = 1; } fmt.fmt.pix.width = width; fmt.fmt.pix.height = height; if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) { LOGE("++++%d : set format failed\n",__LINE__); return -1; } struct v4l2_requestbuffers req; req.count = numbuf; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; ret = ioctl(fd, VIDIOC_REQBUFS, &req); if (ret < 0) { LOGE("++++%d : VIDIOC_REQBUFS failed\n",__LINE__); return -1; } buffers = calloc(req.count, sizeof(*buffers)); if (!buffers) { LOGE ("++++%d Out of memory\n",__LINE__); return -1; } for(i = 0; i< bufnum; ++i) { memset(&v4l2_buf, 0, sizeof(v4l2_buf)); v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4l2_buf.memory = V4L2_MEMORY_MMAP; v4l2_buf.index = i; ret = ioctl(fd , VIDIOC_QUERYBUF, &v4l2_buf); if(ret < 0) { LOGE("+++%d : VIDIOC_QUERYBUF failed\n",__LINE__); return -1; } buffers[i].length = v4l2_buf.length; if ((buffers[i].start = (char *)mmap(0, v4l2_buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, v4l2_buf.m.offset)) < 0) { LOGE("%d : mmap() failed",__LINE__); return -1; } } rgb = (int *)malloc(sizeof(int) * (mwidth*mheight)); ybuf = (int *)malloc(sizeof(int) * (mwidth*mheight)); return 0;}改动地方不是非常多 这里当c_type为2的时候是针对CMOS的接口通道1 为3的时候是通道2
同一时候在
void yuyv422torgb(unsigned char *src,int *mrgb)
最后须要改动下 在測试过程中发现红色和蓝色反了 所以把最后的
*lrgb++ = 0xff000000 | b1<<16 | g1<<8 | r1; *lrgb++ = 0xff000000 | b2<<16 | g2<<8 | r2;改动为
*lrgb++ = 0xff000000 | r1<<16 | g1<<8 | b1; *lrgb++ = 0xff000000 | r2<<16 | g2<<8 | b2;最后 应用里的相关改动例如以下
class StartThread extends Thread { @Override public void run() { // TODO Auto-generated method stub //super.run(); while(true) { index = Fimcgzsd.dqbuf(mdata); if(index < 0) { onDestroy(); break; } switch(ctype) { case 0: mHandler.post(mUpdateUI); bitmap = BitmapFactory.decodeByteArray(mdata, 0, width * height); Fimcgzsd.qbuf(index); break; case 1: Fimcgzsd.pixeltobmp(bmp); mHandler.post(mUpdateUI); bitmap = bmp; Fimcgzsd.qbuf(index); break; case 2: case 3: Fimcgzsd.yuvtorgb(mdata, rgb); mHandler.post(mUpdateUI); bitmap = Bitmap.createBitmap(rgb,width,height,Bitmap.Config.ARGB_8888); Fimcgzsd.qbuf(index); break; } } }最后应用改动例如以下 这里主要是去实现功能 界面比較简单
CMOS效果例如以下
版权声明:本文博客原创文章。博客,未经同意,不得转载。