android下将v4l2采集的yuv数据转成jpg图片


  

2013-03-20 22:41:39|  分类: Android |  标签:yuv转jpg  |字号 订阅

接上篇,在取得yuv420的数据后,移植libjpeg库,实现将yuv转换成jpg图片保存。

1.移植libjpeg到android,其实就是在cygwin下来编译

 先看我的工程

android下将v4l2采集的yuv数据转成jpg图片 - 风间沧月 - 风间沧月
 
其中的 jpeg目录就是jpeg库的源文件
test.c就是上篇中的代码,实现取得一帧数据并转换后保存为jpg图片。
Android.mk为编译脚本,内容如下

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := test LOCAL_SRC_FILES := test.c LOCAL_STATIC_LIBRARIES := jpeg LOCAL_C_INCLUDES += $(LOCAL_PATH)/jpeg LOCAL_LDLIBS := -llog include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) include $(LOCAL_PATH)/jpeg/Android.mk

这样就可以把jpeg一同打包进test可执行文件,也可以单独编译jpeg生成.a文件,再进行链接编译。
再看jpeg目录
android下将v4l2采集的yuv数据转成jpg图片 - 风间沧月 - 风间沧月
主要是libjpeg的源文件和头文件,需要的文件可以在下载的libjpeg库的makefile.ansi中找到(LIBSOURCES宏定义)
Android.mk文件如下

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := jpeg LOCAL_SRC_FILES := jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \ jquant2.c jutils.c jmemmgr.c jmemansi.c include $(BUILD_STATIC_LIBRARY)

注意,jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c这几个文件需要一个, LIBSOURCES
 中并没有其中的任何文件,不加的话编译会报错。具体作用还不知道,据说是和内存模型有关的,不懂。

之后编译就可以了。

下边是 yuv转jpg的函数,参数就不用介绍了,一看就知道。

int write_JPEG_file (const char * filename, unsigned char* yuvData, int quality,int image_width,int image_height) { LOG("write_JPEG_file 1 \n"); struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE * outfile; // target file // JSAMPROW row_pointer[1]; // pointer to JSAMPLE row[s] // int row_stride; // physical row width in image buffer JSAMPIMAGE buffer; unsigned char *pSrc,*pDst; int band,i,buf_width[3],buf_height[3]; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "can't open %s\n", filename); return -1; } LOG("write_JPEG_file 2 \n"); jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = image_width; // image width and height, in pixels cinfo.image_height = image_height; cinfo.input_components = 3; // # of color components per pixel cinfo.in_color_space = JCS_RGB; //colorspace of input image jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE ); // cinfo.raw_data_in = TRUE; cinfo.jpeg_color_space = JCS_YCbCr; cinfo.comp_info[0].h_samp_factor = 2; cinfo.comp_info[0].v_samp_factor = 2; / jpeg_start_compress(&cinfo, TRUE); buffer = (JSAMPIMAGE) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_IMAGE, 3 * sizeof(JSAMPARRAY)); for(band=0; band <3; band++) { buf_width[band] = cinfo.comp_info[band].width_in_blocks * DCTSIZE; buf_height[band] = cinfo.comp_info[band].v_samp_factor * DCTSIZE; buffer[band] = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, buf_width[band], buf_height[band]); } unsigned char *rawData[3]; rawData[0]=yuvData; rawData[1]=yuvData+image_width*image_height; rawData[2]=yuvData+image_width*image_height*5/4; int src_width[3],src_height[3]; for(i=0;i<3;i++) { src_width[i]=(i==0)?image_width:image_width/2; src_height[i]=(i==0)?image_height:image_height/2; } LOG("write_JPEG_file 3 \n"); //max_line一般为16,外循环每次处理16行数据。 int max_line = cinfo.max_v_samp_factor*DCTSIZE; int counter; for(counter=0; cinfo.next_scanline < cinfo.image_height; counter++) { //buffer image copy. for(band=0; band <3; band++) //每个分量分别处理 { int mem_size = src_width[band];//buf_width[band]; pDst = (unsigned char *) buffer[band][0]; pSrc = (unsigned char *) rawData[band] + counter*buf_height[band] * src_width[band];//buf_width[band]; //yuv.data[band]分别表示YUV起始地址 for(i=0; i <buf_height[band]; i++) //处理每行数据 { memcpy(pDst, pSrc, mem_size); pSrc += src_width[band];//buf_width[band]; pDst += buf_width[band]; } } jpeg_write_raw_data(&cinfo, buffer, max_line); } jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); return 0; }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值