android4.0 USB Camera執行個體(五補充)jpg壓縮

來源:互聯網
上載者:User

標籤:android   des   style   blog   java   color   

前一篇最後 我們說了一個直接將yuv轉成jpg的函數 但是轉換沒有成功 原函數是yuv420轉jpg的 研究了下發現

yuv420隔行掃描的的序列是這樣的

YYYY

YYYY

UVUV

而yuv422的隔行掃描的序列是這樣的

YU YV YU YV YU YV

所以將函數作如下修改

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, 80, 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 += 16) {        for (i = 0; i < 16; 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(&cinfo);    memcpy(dest_image,pbuf,jpglen);if(pbuf)free(pbuf);    return jpglen;}
即可將yuv422直接轉化成jpg
介面函數修改如下

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);}
還是要靜下心來研究

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.