Preliminary Design of Android camera hal

Source: Internet
Author: User

When the zc301 USB camera is used, the camera returns a JPEG image. The preview of camera requires JPEG decoding (not done), but you can take the JPEG image directly.

1. Modify Your boardconfig. mk
Use_camera_stub: = false

Set stub to false, and do not compile in android2.1/frameworks/base/camera/libcameraservice during compilation.
Camerahardwarestub. cpp
Camerahardwarestub. h
Fakecamera. cpp
Fakecamera. h
Several files

2. Create the camera Hal directory under hardware, android2.1/hardware/your board/libcamera
Copy the above files
Camerahardwarestub. cpp
Camerahardwarestub. h
Fakecamera. cpp
Fakecamera. h

You can rename it.
Initi6410camerahardware. cpp
Usbcamera. cpp

The fake and stub in the file can also be replaced.

3. Compile the Android. mk File
========================================================== ======================================
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_src_files: = maid. CPP/
Usbcamera. cpp
Local_c_includes: =/
External/JPEG
Local_shared_libraries: = libutils libbinder libui liblog
Local_static_libraries: =/
Libjpeg
Local_module: = libcamera
Include $ (build_shared_library)
========================================================== ======================================
The JPEG library is used to decode JPEG in the future. Based on this script, you can compile libcamera. So

4. Modify fakecamera. cpp-> usbcamera. cpp
Currently, only basic functions are implemented.
This file can be written in the v4l2 process.
There are also ready-made patches on the Internet, using the MMAP method, but unfortunately it is 1.6, not compiled in the past, to be studied

1) initialize the constructor.
Usbcamera: usbcamera (INT width, int height)
: Mtmprgb16buffer (0)
{
FD = open (default_device, o_rdwr/* required */| o_nonblock, 0 );
LogE ("Open/dev/video0 FD is % d", FD );
IOCTL (FD, vidioc_querycap, & Cap );
Clear (FMT );
FMT. type = v4l2_buf_type_video_capture;
FMT. FMT. pix. width = 640;
FMT. FMT. pix. Height = 480;
FMT. FMT. pix. pixelformat = v4l2_pix_fmt_yuyv;
FMT. FMT. pix. Field = v4l2_field_interlaced;
IOCTL (FD, vidioc_s_fmt, & FMT );
Length = FMT. FMT. pix. bytesperline * FMT. FMT. pix. height;
}

2) Get a frame of data
Void usbcamera: getnextframeasyuv422 (uint8_t * buffer)
{
LogE ("read to get a pic from camera! ");
For (;;)
{
Fd_set FDS;
Struct timeval TV;
Int R;
Fd_zero (& FDs );
Fd_set (FD, & FDs );
/* Timeout .*/
TV. TV _sec = 3;
TV. TV _usec = 0;
R = select (FD + 1, & FDS, null, null, & TV );
If (-1 = r ){
LogE ("select ");}
If (0 = r ){
LogE ("select timeout/N ");}
If (read (FD, buffer, length ));
Break;
}
}

3) destructor
Usbcamera ::~ Usbcamera ()
{
Delete [] mtmprgb16buffer;
Close (FD );
}

5. Modify camerahardwarestub. cpp-> cloud6410camerahardware. cpp

1) Change to 640xlarge.
Void camerahardware: initdefaparameparameters ()
{
Cameraparameters P;
P. setpreviewsize (640,480 );
P. setpreviewframerate (1 );
P. setpreviewformat ("yuv422sp"); // ("yuv422sp ");
P. setpicturesize (640,480 );
P. setpictureformat ("Jpeg"); // ("Jpeg ");
If (setparameters (p )! = No_error ){
LogE ("failed to set default parameters ?! ");
}
}

2) Change the photo part because you can directly obtain the compressed JPEG image.
Int camerahardware: picturethread ()
{
Usbcamera * usbcamera = musbcamera;
If (mmsgenabled & camera_msg_shutter)
Mpolicycb (camera_msg_shutter, 0, 0, mcallbackcookie );
If (mmsgenabled & camera_msg_raw_image ){
// Fixme: Use a canned YUV image!
// In the meantime just make another fake camera picture.
// Int W, h;
// Mparameters. getpicturesize (& W, & H );
// Sp <memorybase> mem = new memorybase (mrawheap, 0, w * 2 * H );
LogE ("camera_msg_raw_image ");
// Usbcamera cam (W, H );
// Cam. getnextframeasyuv422 (uint8_t *) mrawheap-> base ());
// Mdatacb (camera_msg_raw_image, mem, mcallbackcookie );
}
If (mmsgenabled & camera_msg_compressed_image ){
// Sp <memoryheapbase> heap = new memoryheapbase (20000 );
// Sp <memorybase> mem = new memorybase (heap, 0, 20000 );
// Memcpy (heap-> base (), kcannedjpeg, 20000 );
LogE ("camera_msg_compressed_image ");
Int W, h;
Mparameters. getpicturesize (& W, & H );
Sp <memorybase> mem = new memorybase (mrawheap, 0, w * 2 * H );
// Usbcamera cam (W, H );
Usbcamera-> getnextframeasyuv422 (uint8_t *) mrawheap-> base ());
Mdatacb (camera_msg_compressed_image, mem, mcallbackcookie );

}
Return no_error;
}

3) status_t camerahardware: setparameters (const cameraparameters & Params)
There is a place where only the pic of take 320x240 can be commented out.
/* If (W! = 320 & H! = 240 ){
LogE ("still picture size must be size of canned JPEG (% DX % d )",
320,240 );
Return-1;
}*/

Now, you can take a photo of camera Hal.

Problems:

1) JPEG> YUV is required to implement the preview function. libjpeg has never been used.
2) Change USB camera. cpp to the v4l2 standard process. The read mode is too simple and inefficient.


Related Article

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.