linux 相机 读取图像转换为RGB后再转换为JPG图像失真

2019-12-03 13:55发布

<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="微软雅黑">{&nbsp;&nbsp;//分别为RGB数据,要保存的jpg文件名,图片长宽</font><br> <font face="微软雅黑">&nbsp; &nbsp; int depth = 3;</font><br> <font face="微软雅黑">&nbsp; &nbsp; JSAMPROW row_pointer[1];//指向一行图像数据的指针</font><br> <font face="微软雅黑">&nbsp; &nbsp; struct jpeg_compress_struct cinfo;</font><br> <font face="微软雅黑">&nbsp; &nbsp; struct jpeg_error_mgr jerr;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; cinfo.err = jpeg_std_error(&amp;jerr);//要首先初始化错误信息</font><br> <font face="微软雅黑">&nbsp; &nbsp; //* Now we can initialize the JPEG compression object.</font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_create_compress(&amp;cinfo);</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_stdio_dest(&amp;cinfo, jpg_file);</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; cinfo.image_width = width;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //* image width and height, in pixels</font><br> <font face="微软雅黑">&nbsp; &nbsp; cinfo.image_height = height;</font><br> <font face="微软雅黑">&nbsp; &nbsp; cinfo.input_components = depth;&nbsp; &nbsp; //* # of color components per pixel</font><br> <font face="微软雅黑">&nbsp; &nbsp; cinfo.in_color_space = JCS_RGB;&nbsp; &nbsp;&nbsp;&nbsp;//* colorspace of input image</font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_set_defaults(&amp;cinfo);</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_set_quality(&amp;cinfo, 100, TRUE ); //* limit to baseline-JPEG values</font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_start_compress(&amp;cinfo, TRUE);</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; int row_stride = width * 3;</font><br> <font face="微软雅黑">&nbsp; &nbsp; while (cinfo.next_scanline &lt; cinfo.image_height)</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;row_pointer[0] = (JSAMPROW)(pdata + cinfo.next_scanline * row_stride);//一行一行数据的传,jpeg为大端数据格式</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;jpeg_write_scanlines(&amp;cinfo, row_pointer, 1);</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_finish_compress(&amp;cinfo);</font><br> <font face="微软雅黑">&nbsp; &nbsp; jpeg_destroy_compress(&amp;cinfo);//这几个函数都是固定流程</font><br> <font face="微软雅黑">&nbsp; &nbsp; fclose(jpg_file);</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp; 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="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int width=0;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int height=0;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;width = IMG_WIDTH;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;height = IMG_HEIGHT;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int frameSize =width*height*2;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int i;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;IF((!rgb || !ybuf)){</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int *lrgb = NULL;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int *lybuf = NULL;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;lrgb = &amp;rgb[0];</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;lybuf = &amp;ybuf[0];</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(yuv_tbl_ready==0){</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for(i=0 ; i&lt;256 ; i++){</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;y1192_tbl<i> = 1192*(i-16);</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(y1192_tbl<i>&lt;0){</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;y1192_tbl<i>=0;</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;v1634_tbl<i> = 1634*(i-128);</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;v833_tbl<i> = 833*(i-128);</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;u400_tbl<i> = 400*(i-128);</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;u2066_tbl<i> = 2066*(i-128);</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; yuv_tbl_ready=1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(i=0 ; i<framesize ;="" i+="4){</font"><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; unsigned char y1, y2, u, v;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; y1 = src<i>;</i></font><i><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; u = src[i+1];</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; y2 = src[i+2];</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; v = src[i+3];</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int y1192_1=y1192_tbl[y1];</font><br> <br> <br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;int r1 = (y1192_1 + v1634_tbl[v])&gt;&gt;10;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int g1 = (y1192_1 - v833_tbl[v] - u400_tbl<u>)&gt;&gt;10;</u></font><u><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int b1 = (y1192_1 + u2066_tbl<u>)&gt;&gt;10;</u></font><u><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int y1192_2=y1192_tbl[y2];</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int r2 = (y1192_2 + v1634_tbl[v])&gt;&gt;10;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int g2 = (y1192_2 - v833_tbl[v] - u400_tbl<u>)&gt;&gt;10;</u></font><u><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; int b2 = (y1192_2 + u2066_tbl<u>)&gt;&gt;10;</u></font><u><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; r1 = r1&gt;255 ? 255 : r1&lt;0 ? 0 : r1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; g1 = g1&gt;255 ? 255 : g1&lt;0 ? 0 : g1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; b1 = b1&gt;255 ? 255 : b1&lt;0 ? 0 : b1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; r2 = r2&gt;255 ? 255 : r2&lt;0 ? 0 : r2;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; g2 = g2&gt;255 ? 255 : g2&lt;0 ? 0 : g2;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; b2 = b2&gt;255 ? 255 : b2&lt;0 ? 0 : b2;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; *lrgb++ = 0xff000000 | b1&lt;&lt;16 | g1&lt;&lt;8 | r1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; *lrgb++ = 0xff000000 | b2&lt;&lt;16 | g2&lt;&lt;8 | r2;</font><br> <font face="微软雅黑"><br> </font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(lybuf!=NULL){</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*lybuf++ = y1;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*lybuf++ = y2;</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }</font><br> <font face="微软雅黑">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</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>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
科源机电
1楼-- · 2019-12-03 16:28
 精彩回答 2  元偷偷看……
zxc199108
2楼-- · 2019-12-03 16:48
照片居然没上传上去。。。。转换完成的照片我大概形容一下,就是我拍自己的脸,结果转换成JPG用电脑看是重影那种的,有三个脸。我是在linux下转换完成后传输到电脑端查看的。

一周热门 更多>