android4.0 USB Camera Instance (five additions) JPG compression

Source: Internet
Author: User

In the end, we said a function that directly converts YUV to JPG, but the conversion is not successful. The original function is the research of yuv420 to JPG.

yuv420 the sequence of interlaced scanning is like this

YYYY

YYYY

Uvuv

And the yuv422 sequence of interlaced scans is like this.

Yu yv yu yv yu yv

So modify the function as follows

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)                ;                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); if (i%2 = = 0) CB[I/2] = input_image + width * ((i + j)/2); ELSECR[I/2] = input_image + width * ((i + j)/        2);    } jpeg_write_raw_data (&cinfo, data, 16);    } jpeg_finish_compress (&cinfo); Jpeg_destroy_compress (&Amp;cinfo);    memcpy (Dest_image,pbuf,jpglen); if (pbuf) free (PBUF); return Jpglen;}
You can convert the yuv422 directly into JPG
The interface functions are modified as follows

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;  } unsigned char* DST = malloc (mwidth*mheight*3*sizeof (char)); int size = Put_jpeg_yuv420p_memory (Dst,ydata,mwidth, Mheight), fwrite (Dst,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);}
Or do you want to study it in a quiet mind?

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.