<font face="微软雅黑">我在linux下读取USB摄像头的数据,最终输出一张JPG图出来,但是读取出来的图片失真。用的是jpeglib的接口进行的转换,我想请问到底哪一步出错了。</font><font color="#333333"><font face="微软雅黑"><font style="font-size:18px"><img id="aimg_mlkQz" onclick="zoom(this, this.src, 0, 0, 0)" class="zoom" src="data/attach/1912/sc79nezbibpiw7jkf1wu9mck7tgv2a93.jpg" onmouseover="img_onmouseoverfunc(this)" lazyloadthumb="1" border="0" alt=""></font></font></font><br>
<br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">//rgb转jpg</font></font></font><br>
<font face="微软雅黑">int save_jpg(unsigned char *pdata, FILE *jpg_file, int width, int height)</font><br>
<font face="微软雅黑">{ //分别为RGB数据,要保存的jpg文件名,图片长宽</font><br>
<font face="微软雅黑"> int depth = 3;</font><br>
<font face="微软雅黑"> JSAMPROW row_pointer[1];//指向一行图像数据的指针</font><br>
<font face="微软雅黑"> struct jpeg_compress_struct cinfo;</font><br>
<font face="微软雅黑"> struct jpeg_error_mgr jerr;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> cinfo.err = jpeg_std_error(&jerr);//要首先初始化错误信息</font><br>
<font face="微软雅黑"> //* Now we can initialize the JPEG compression object.</font><br>
<font face="微软雅黑"> jpeg_create_compress(&cinfo);</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> jpeg_stdio_dest(&cinfo, jpg_file);</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> cinfo.image_width = width; //* image width and height, in pixels</font><br>
<font face="微软雅黑"> cinfo.image_height = height;</font><br>
<font face="微软雅黑"> cinfo.input_components = depth; //* # of color components per pixel</font><br>
<font face="微软雅黑"> cinfo.in_color_space = JCS_RGB; //* colorspace of input image</font><br>
<font face="微软雅黑"> jpeg_set_defaults(&cinfo);</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> jpeg_set_quality(&cinfo, 100, TRUE ); //* limit to baseline-JPEG values</font><br>
<font face="微软雅黑"> jpeg_start_compress(&cinfo, TRUE);</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int row_stride = width * 3;</font><br>
<font face="微软雅黑"> while (cinfo.next_scanline < cinfo.image_height)</font><br>
<font face="微软雅黑"> {</font><br>
<font face="微软雅黑"> row_pointer[0] = (JSAMPROW)(pdata + cinfo.next_scanline * row_stride);//一行一行数据的传,jpeg为大端数据格式</font><br>
<font face="微软雅黑"> jpeg_write_scanlines(&cinfo, row_pointer, 1);</font><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> jpeg_finish_compress(&cinfo);</font><br>
<font face="微软雅黑"> jpeg_destroy_compress(&cinfo);//这几个函数都是固定流程</font><br>
<font face="微软雅黑"> fclose(jpg_file);</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> return 0;</font><br>
<font face="微软雅黑">}</font><br>
<br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">//yUV转rgb</font></font></font><br>
<font face="微软雅黑">void yuyv422toABGRY(unsigned char *src)</font><br>
<font face="微软雅黑">{</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int width=0;</font><br>
<font face="微软雅黑"> int height=0;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> width = IMG_WIDTH;</font><br>
<font face="微软雅黑"> height = IMG_HEIGHT;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int frameSize =width*height*2;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int i;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> IF((!rgb || !ybuf)){</font><br>
<font face="微软雅黑"> return;</font><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"> int *lrgb = NULL;</font><br>
<font face="微软雅黑"> int *lybuf = NULL;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> lrgb = &rgb[0];</font><br>
<font face="微软雅黑"> lybuf = &ybuf[0];</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> if(yuv_tbl_ready==0){</font><br>
<font face="微软雅黑"> for(i=0 ; i<256 ; i++){</font><br>
<font face="微软雅黑"> y1192_tbl<i> = 1192*(i-16);</i></font><i><br>
<font face="微软雅黑"> if(y1192_tbl<i><0){</i></font><i><br>
<font face="微软雅黑"> y1192_tbl<i>=0;</i></font><i><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> v1634_tbl<i> = 1634*(i-128);</i></font><i><br>
<font face="微软雅黑"> v833_tbl<i> = 833*(i-128);</i></font><i><br>
<font face="微软雅黑"> u400_tbl<i> = 400*(i-128);</i></font><i><br>
<font face="微软雅黑"> u2066_tbl<i> = 2066*(i-128);</i></font><i><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"> yuv_tbl_ready=1;</font><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> for(i=0 ; i<framesize ;="" i+="4){</font"><br>
<font face="微软雅黑"> unsigned char y1, y2, u, v;</font><br>
<font face="微软雅黑"> y1 = src<i>;</i></font><i><br>
<font face="微软雅黑"> u = src[i+1];</font><br>
<font face="微软雅黑"> y2 = src[i+2];</font><br>
<font face="微软雅黑"> v = src[i+3];</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int y1192_1=y1192_tbl[y1];</font><br>
<br>
<br>
<font face="微软雅黑"> int r1 = (y1192_1 + v1634_tbl[v])>>10;</font><br>
<font face="微软雅黑"> int g1 = (y1192_1 - v833_tbl[v] - u400_tbl<u>)>>10;</u></font><u><br>
<font face="微软雅黑"> int b1 = (y1192_1 + u2066_tbl<u>)>>10;</u></font><u><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> int y1192_2=y1192_tbl[y2];</font><br>
<font face="微软雅黑"> int r2 = (y1192_2 + v1634_tbl[v])>>10;</font><br>
<font face="微软雅黑"> int g2 = (y1192_2 - v833_tbl[v] - u400_tbl<u>)>>10;</u></font><u><br>
<font face="微软雅黑"> int b2 = (y1192_2 + u2066_tbl<u>)>>10;</u></font><u><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> r1 = r1>255 ? 255 : r1<0 ? 0 : r1;</font><br>
<font face="微软雅黑"> g1 = g1>255 ? 255 : g1<0 ? 0 : g1;</font><br>
<font face="微软雅黑"> b1 = b1>255 ? 255 : b1<0 ? 0 : b1;</font><br>
<font face="微软雅黑"> r2 = r2>255 ? 255 : r2<0 ? 0 : r2;</font><br>
<font face="微软雅黑"> g2 = g2>255 ? 255 : g2<0 ? 0 : g2;</font><br>
<font face="微软雅黑"> b2 = b2>255 ? 255 : b2<0 ? 0 : b2;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> *lrgb++ = 0xff000000 | b1<<16 | g1<<8 | r1;</font><br>
<font face="微软雅黑"> *lrgb++ = 0xff000000 | b2<<16 | g2<<8 | r2;</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑"> if(lybuf!=NULL){</font><br>
<font face="微软雅黑"> *lybuf++ = y1;</font><br>
<font face="微软雅黑"> *lybuf++ = y2;</font><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"> }</font><br>
<font face="微软雅黑"><br>
</font><br>
<font face="微软雅黑">}</font><br>
<br>
<br>
<br>
<br>
<br>
</u></u></u></u></i></framesize></font></i></i></i></i></i></i></i><font face="微软雅黑">
</font><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>