In the previous article, we talked about the usb camera uvc standard. By the way, we used CMOS to work together. First, we went to the previous article. In the HAL layer, I already provided the CMOS interface.
JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_yuvtorgb
If the image processing method is the same as that of UVC, the image cannot be displayed, so another method is used. Also, we use the IOT ICOOL210 Development Board for testing. If CMOS is used, we need to modify the HAL layer as follows:
First, add a function as follows:
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;}
In
In init, call init to modify the settings as follows:
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;elsefmt.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;}
There are not many changes here. When c_type is set to 2, it is set to Channel 2 when interface Channel 1 of CMOS is set to 3.
At the same time
Void yuyv422torgb (unsigned char * src, int * mrgb)
Finally, you need to modify the red and blue colors during the test.
*lrgb++ = 0xff000000 | b1<<16 | g1<<8 | r1; *lrgb++ = 0xff000000 | b2<<16 | g2<<8 | r2;
Change
*lrgb++ = 0xff000000 | r1<<16 | g1<<8 | b1;*lrgb++ = 0xff000000 | r2<<16 | g2<<8 | b2;
Finally, the related changes in the application are as follows:
class StartThread extends Thread {@Overridepublic 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;}}}
The last modification of the application is as follows. Here, it is easier to implement the functional interface.
The CMOS effect is as follows: