Android Camera (camera) v4l2 C test Code "go"

Source: Internet
Author: User

Turn from: 8745577

In the process of porting Android HAL, the primary task of porting is to ensure that the driver is intact and that the camera is a character device, but it is not as simple to write the test code as the general driver, because the camera's test code must be using the V4L2 API. So the next step is to introduce the process of writing the test code for our camera.

1. Turn on the device

[C]View PlainCopy
  1. static int Open_device (char *dev_name)
  2. {
  3. ASSERT (Dev_name);
  4. int fd =-1;
  5. FD = open (Dev_name, O_RDWR);
  6. if (-1 = = FD)
  7. {
  8. MYLOGD ("open%s fail:%s\n", Dev_name, Strerror (errno));
  9. Exit (Exit_failure);
  10. }
  11. MYLOGD ("The FD of%s is%d", Dev_name, FD);
  12. return FD;
  13. }

2, initialize camera, format camera output image

[C]View PlainCopy
  1. static int init_cam_device (int dev_fd)
  2. {
  3. int ret =-1;
  4. int input_index;
  5. //ret = Fimc_v4l2_querycap (DEV_FD);
  6. //assert (ret = = 0);
  7. //Get to input source channel
  8. Input_index = Cam_v4l2_enuminput (DEV_FD);
  9. ASSERT (Input_index = = 0);
  10. ret = Cam_v4l2_s_input (DEV_FD, Input_index);
  11. ASSERT (ret = = 0);
  12. MYLOGD ("vidioc_s_fmt start ... dev_fd =%d\n", dev_fd);
  13. ret = Cam_v4l2_s_format (DEV_FD, Image_height, Image_width, V4L2_PIX_FMT_YUYV);
  14. ASSERT (ret = = 0);
  15. ret = CAM_V4L2_G_FMT (DEV_FD);
  16. ASSERT (ret = = 0);
  17. Init_cam_mmap (DEV_FD);
  18. return 1;
  19. }


4, request buffer to the kernel and mmap buffer map to reference space

struct buffer
{
void *start; The address after mmap;
size_t length//size;
}USER_BUFFERS[4]; Used to record the address and size of the kernel buffer mapping mmap to the user space

Request 4 Buffer

Cam_v4l2_reqbuf (DEV_FD, 4);
Query the buffer information requested, such as the location and size of each buffer

Cam_v4l2_querybuf (DEV_FD, &buf, Buf_index);

Map buffer to User space

User_buffers[buf_index].length = Buf.length;

User_buffers[buf_index].start = mmap (NULL buf.length,
Prot_read | Prot_write/* required */,
map_shared/* Recommended */,
DEV_FD, Buf.m.offset);

Comment: NULL:/* Start anywhere */

Buf.length:buffer the address of the kernel

Buf.m.offset: Buffer size in the kernel

5, put all the buffer in the loop work queue, centralized management

for (i = 0; i < max_buffers; ++i)
{
Cam_v4l2_qbuf (DEV_FD, i);
}

6. Start getting images:

IOCTL (DEV_FD, Vidioc_streamon, &type);

7, select to monitor camera data is ready

Fd_zero (&rd_set);
Fd_set (DEV_FD, &rd_set);

ret = SELECT (Maxfd + 1, &rd_set, NULL, NULL, NULL);

if (Fd_isset (DEV_FD, &rd_set))//If the camera is ready, you can read the data.

Read_frame (DEV_FD)

Implementation of 8,read_frame (DEV_FD):

Select only knows that there is data to read, but in multiple buffer,

Do not know which buffer is ready, so will be ready to buffer out of the queue, so that the number of buffer

int index = CAM_V4L2_DQBUF (DEV_FD);

Convert the yuv420 data in buffer to rgb565,

Because the LCD is not able to display YUV, it is necessary to convert YUV into rgb565

Yuyv_to_rgb (src_address, data_buf);

The converted rgb565 data is displayed in the framebuffer.

Show_rgb565_img (Data_buf, Lcd_width, lcd_height);

The process is simply to write down a process, each function is encapsulated once, no way to write it out,

So I will upload the source code files for everyone to download. Contains Android.mk

Can be downloaded to this link:

http://download.csdn.net/detail/ldswfun/5208766

And some pictures to get you to understand the process.

Android Camera (camera) v4l2 C test Code "go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.