android4.0 USB Camera实例(五补充)jpg压缩

来源:互联网 发布:蜻蜓fm电脑软件 编辑:程序博客网 时间:2024/05/16 06:10

前一篇最后 我们说了一个直接将yuv转成jpg的函数 但是转换没有成功 原函数是yuv420转jpg的 研究了下发现

yuv420隔行扫描的的序列是这样的

YYYY

YYYY

UVUV

而yuv422的隔行扫描的序列是这样的

YU YV YU YV YU YV

所以将函数作如下修改

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static int put_jpeg_yuv420p_memory(unsigned char *dest_image,  
  2.                                    unsigned char *input_image, int width, int height)  
  3. {  
  4.     int i, j, jpeg_image_size;  
  5.     
  6.     JSAMPROW y[16],cb[16],cr[16]; // y[2][5] = color sample of row 2 and pixel column 5; (one plane)  
  7.     JSAMPARRAY data[3]; // t[0][2][5] = color sample 0 of row 2 and column 5  
  8.     
  9.     struct jpeg_compress_struct cinfo;  
  10.     struct jpeg_error_mgr jerr;  
  11.     char *pbuf = NULL;  
  12.     int jpglen = 0;  
  13.     data[0] = y;  
  14.     data[1] = cb;  
  15.     data[2] = cr;  
  16.     
  17.     cinfo.err = jpeg_std_error(&jerr);  // errors get written to stderr   
  18.         
  19.     jpeg_create_compress(&cinfo);  
  20.     cinfo.image_width = width;  
  21.     cinfo.image_height = height;  
  22.     cinfo.input_components = 3;  
  23.     jpeg_set_defaults (&cinfo);  
  24.     
  25.     jpeg_set_colorspace(&cinfo, JCS_YCbCr);  
  26.     
  27.     cinfo.raw_data_in = TRUE;                  // supply downsampled data  
  28.     cinfo.do_fancy_downsampling = FALSE;       // fix segfaulst with v7  
  29.     cinfo.comp_info[0].h_samp_factor = 2;  
  30.     cinfo.comp_info[0].v_samp_factor = 2;  
  31.     cinfo.comp_info[1].h_samp_factor = 1;  
  32.     cinfo.comp_info[1].v_samp_factor = 1;  
  33.     cinfo.comp_info[2].h_samp_factor = 1;  
  34.     cinfo.comp_info[2].v_samp_factor = 1;  
  35.     
  36.     jpeg_set_quality(&cinfo, 80, TRUE);  
  37.     cinfo.dct_method = JDCT_FASTEST;  
  38.     
  39.     jpeg_mem_dest(&cinfo, &pbuf, &jpglen);    // data written to mem  
  40.         
  41.     jpeg_start_compress (&cinfo, TRUE);  
  42.     
  43.     for (j = 0; j < height; j += 16) {  
  44.         for (i = 0; i < 16; i++) {  
  45.             y[i] = input_image + width * (i + j);  
  46.                 //cb[i/2] = input_image + width * height + width / 2 * ((i + j) / 2);  
  47.                 //cr[i/2] = input_image + width * height + width * height / 4 + width / 2 * ((i + j) / 2);  
  48.             if (i%2 == 0)  
  49.                 cb[i/2] = input_image + width * ((i + j) / 2);  
  50.             else  
  51.                 cr[i/2] = input_image + width * ((i + j) / 2);  
  52.         }  
  53.         jpeg_write_raw_data(&cinfo, data, 16);  
  54.     }  
  55.     
  56.     jpeg_finish_compress(&cinfo);  
  57.     jpeg_destroy_compress(&cinfo);  
  58.     memcpy(dest_image,pbuf,jpglen);  
  59.     if(pbuf)  
  60.         free(pbuf);  
  61.     return jpglen;  
  62. }  
即可将yuv422直接转化成jpg
接口函数修改如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. JNIEXPORT jint JNICALL Java_com_hclydao_usbcamera_Fimcgzsd_writefile(JNIEnv * env, jclass obj,jbyteArray yuvdata,jbyteArray filename)//jintArray rgbdata  
  2. {  
  3.     jbyte *ydata = (jbyte*)(*env)->GetByteArrayElements(env, yuvdata, 0);  
  4.     jbyte *filedir = (jbyte*)(*env)->GetByteArrayElements(env, filename, 0);  
  5.   
  6.     FILE * outfile;  
  7.     if ((outfile = fopen(filedir, "wb")) == NULL) {  
  8.         LOGE("++++++++++++open %s failed\n",filedir);  
  9.         return -1;  
  10.     }  
  11.   
  12.     unsigned char* dst = malloc(mwidth*mheight*3*sizeof(char));  
  13.     int size = put_jpeg_yuv420p_memory(dst,ydata,mwidth,mheight);  
  14.     fwrite(dst,size,1,outfile);  
  15.     if(dst)free(dst);  
  16.     if(jpgdata)free(jpgdata);  
  17.     fclose(outfile);  
  18.     (*env)->ReleaseByteArrayElements(env, yuvdata, ydata, 0);  
  19.     (*env)->ReleaseByteArrayElements(env, filename, filedir, 0);  
  20. }  

还是要静下心来研究


============================================
作者:hclydao
http://blog.csdn.net/hclydao
版权没有,但是转载请保留此段声明

============================================



0 0
原创粉丝点击