android4.0 USB Camera Instance (v) JPG compression

Source: Internet
Author: User

Most of the time in the recent study of the USB camera by the way the JPG code is also written on the following most of the functions I found on the internet and then a little change can be used, but to find these functions cost a lot of time JPG code online There are many instructions the general process is the same I did not go into the study here. Next several camera articles here mainly with the acquisition of YUV data through JPG compression and compression into the MJPEG video stream first of all, the camera gets to the data format is yuv422 (P16) This is in your initialization camera settings  PixelFormat We initially set the v4l2_pix_fmt_yuyv its actual format is yuv422 the specific format you go online to find it. The process is to first convert yuv422 to rgb888 and then convert rgb888 to JPG You can actually turn yuv422 into JPG, but I didn't implement it, and then we put the code on it.

First yuv422 Turn rgb888

static void yuv422torgb888 (int width, int height, unsigned char *src, unsigned char *dst) {int line, column;unsigned char * PY, *pu, *pv;unsigned char *tmp = dst;/* in the This format are four bytes. Each four bytes is a Y ' s, aCb and a cr.each y goes to one of the pixels, and the Cb and Cr belong to Bothpixels. */py = Src;pu = src + 1;PV = src + 3; #define CLIP (x) ((x) >=0xff? 0xFF: ((x) <= 0x00? 0x00: (x))) for (line = 0; Line < height; ++line) {for (column = 0; column < width; ++column) {*tmp++ = CLIP (double) *py + 1.402* ((double) *pv-128.0)); *tmp++ = CL IP (Double) *py-0.344* ((double) *pu-128.0) -0.714* ((double) *pv-128.0)); *tmp++ = CLIP (double) *py + 1.772* ((double) * pu-128.0));//increase py every timepy + = 2;//Increase PU,PV every second Timeif ((column & 1) ==1) {PU + = 4;PV + 4;} }}}

Then turn the rgb888 into JPG.

static int jpeg_mem_copy (unsigned char* img,unsigned char *dest) {struct jpeg_compress_struct cinfo;struct jpeg_error_ Mgr Jerr;  Jsamprow Row_pointer[1]; unsigned char *pbuf = null;int Jpglen = 0;//Create jpeg datacinfo.err = Jpeg_std_error (&jerr); Jpeg_create_compress ( &cinfo);//jpeg_stdio_dest (&cinfo, FP); Jpeg_mem_dest (&cinfo, &pbuf, &jpglen);//Set image Parameterscinfo.image_width = Mwidth;cinfo.image_height = mheight;cinfo.input_components = 3;cinfo.in_color_space = jcs_rgb;//set JPEG compression parameters to defaultjpeg_set_defaults (&cinfo);//And then adjust quality settingjpeg _set_quality (&cinfo, N, True);//Start Compressjpeg_start_compress (&cinfo, true);//Feed Datawhile ( Cinfo.next_scanline < Cinfo.image_height) {Row_pointer[0] = &img[cinfo.next_scanline * Cinfo.image_width * Cinfo.input_components];jpeg_write_scanlines (&cinfo, Row_pointer, 1);} Finish Compressionjpeg_finish_compress (&cinfo);//Destroy JPEG Datajpeg_destRoy_compress (&cinfo);    memcpy (Dest,pbuf,jpglen);//LOGD ("++++++++++++++++len is%d\n", Jpglen); if (pbuf) free (PBUF); return Jpglen;}
Here I am using the latest JPEG Library 9a has integrated the Jpeg_mem_dest function Libjpeg Transplant Network also has a lot of instructions

Here is an interface I provide to the upper call

Jniexport jint jnicall java_com_hclydao_usbcamera_fimcgzsd_writefile (jnienv * env, Jclass Obj,jbytearray Yuvdata, Jbytearray filename)//jintarray rgbdata{jbyte *ydata = (jbyte*) (*env)->getbytearrayelements (env, yuvdata, 0); jbyte *filedir = (jbyte*) (*env)->getbytearrayelements (env, filename, 0);  FILE * outfile;    if (outfile = fopen (Filedir, "wb") = = = NULL) {LOGE ("++++++++++++open%s failed\n", Filedir);  return-1; }//yuv422_to_jpeg (ydata,mwidth,mheight,outfile,80); unsigned char* src = (unsigned char*) ydata;unsigned char* DST = malloc (mwidth*mheight*3*sizeof (char)); unsigned char* jpgdata = malloc (mwidth*mheight*3*sizeof (char)); yuv422torgb888 (MWIDTH,MHEIGHT,SRC,DST); int size=jpeg_mem_copy (dst,jpgdata); fwrite (jpgdata,size,1,outfile); if ( DST) Free (DST), if (jpgdata) free (jpgdata), fclose (outfile);(*env)->releasebytearrayelements (env, Yuvdata, Ydata, 0 );(*env)->releasebytearrayelements (env, filename, filedir, 0);}
This incoming is the YUV data obtained and the path of the JPG file to be saved there are some parameters I declare the global variables specifically can go to see my previous articles


The following are the related interfaces of the video stream

FILE * video_file;/* *put in frame buffer to queue */jniexport jint Jnicall Java_com_hclydao_usbcamera_fimcgzsd_videoopen ( JNIENV * env, jclass obj,jbytearray filename) {jbyte *filedir = (jbyte*) (*env)->getbytearrayelements (env, filename, 0)  ;    if (Video_file = fopen (Filedir, "wb") = = = NULL) {LOGE ("++++++++++++open%s failed\n", Filedir);  return-1; } (*ENV)->releasebytearrayelements (env, filename, filedir, 0);} Jniexport jint jnicall Java_com_hclydao_usbcamera_fimcgzsd_videostart (jnienv * env, Jclass Obj,jbytearray yuvdata) { Jbyte *ydata = (jbyte*) (*env)->getbytearrayelements (env, yuvdata, 0); unsigned char* src = (unsigned char*) ydata; unsigned char* DST = malloc (mwidth*mheight*3*sizeof (char)), unsigned char* jpgdata = malloc (mwidth*mheight*3*sizeof ( char)); yuv422torgb888 (MWIDTH,MHEIGHT,SRC,DST); int size=jpeg_mem_copy (dst,jpgdata); fwrite (jpgdata,size,1,video_file);// Fwrite (DST, (Mwidth*mheight*3*sizeof (char)), 1,video_file), if (DST) free (DST), if (jpgdata) free (jpgdata);(*env), ReLeasebytearrayelements (env, Yuvdata, Ydata, 0);} Jniexport jint jnicall java_com_hclydao_usbcamera_fimcgzsd_videoclose (jnienv * env, Jclass obj) {fclose (video_file);}
is to continuously save the JPG file to the same file saved files directly can not play with the format factory converted to AVI will be released after


Here is a direct conversion of YUV to JPG function This is to convert the yuv420p to JPG I was yuv422 changed a lot of times to find the picture of the preservation of the image is not to look at the difference between these formats

/* Put_jpeg_yuv420p_memory Converts an input image in the yuv420p format to a JPEG image and puts * it in a memory Buffe R. * Inputs: *-Input_image is the image in yuv420p format. *-width and height are the dimensions of the image * Output: *-Dest_image was a pointer to the JPEG image buffer * Retu                                   RNS buffer size of JPEG image */static int put_jpeg_yuv420p_memory (unsigned char *dest_image,      unsigned char *input_image, int width, int height) {int I, j, Jpeg_image_size; Jsamprow y[16],cb[16],cr[16]; Y[2][5] = Color sample of row 2 and pixel column 5;    (One plane) Jsamparray Data[3];    T[0][2][5] = Color Sample 0 of row 2 and column 5 struct jpeg_compress_struct cinfo;  struct Jpeg_error_mgr jerr;    Char *pbuf = Null;int Jpglen = 0;    Data[0] = y;    DATA[1] = CB;      DATA[2] = CR;  Cinfo.err = Jpeg_std_error (&jerr);    Errors get written to stderr jpeg_create_compress (&cinfo); Cinfo.image_width = WIDth    Cinfo.image_height = height;    Cinfo.input_components = 3;      Jpeg_set_defaults (&cinfo);      Jpeg_set_colorspace (&cinfo, JCS_YCBCR);                  cinfo.raw_data_in = TRUE;       Supply downsampled Data cinfo.do_fancy_downsampling = FALSE;    Fix segfaulst with v7 cinfo.comp_info[0].h_samp_factor = 2;    Cinfo.comp_info[0].v_samp_factor = 2;    Cinfo.comp_info[1].h_samp_factor = 1;    Cinfo.comp_info[1].v_samp_factor = 1;    Cinfo.comp_info[2].h_samp_factor = 1;      Cinfo.comp_info[2].v_samp_factor = 1;    Jpeg_set_quality (&cinfo, N, TRUE);      Cinfo.dct_method = Jdct_fastest;    Jpeg_mem_dest (&cinfo, &pbuf, &jpglen);      Data written to Mem jpeg_start_compress (&cinfo, TRUE); for (j = 0, J < height; J + = +) {for (i = 0; i < +; i++) {Y[i] = input_image + Width * (i + j)            ;      if (i%2 = = 0) {CB[I/2] = input_image + Width * height + width/2 * ((i + j)/2);          CR[I/2] = input_image + Width * height + width * HEIGHT/4 + width/2 * ((i + j)/2);    }} jpeg_write_raw_data (&cinfo, data, 16);    } jpeg_finish_compress (&cinfo);    Jpeg_destroy_compress (&cinfo);    memcpy (Dest_image,pbuf,jpglen); if (pbuf) free (PBUF); return Jpglen;}
I've been confused lately so I've been thinking about some of the things that have to be studied in the end. Next, we're ready to look at the FFmpeg h264 compression


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.