在网上找了好几套,结果都不对。后来终于找到一套,还缺了一点。于是连蒙带猜,终于保存成功了。
- github的开源
吾已将此代码开源到github,会进行维护工作。请优待去github:
GitHub - quantum6/NV12ToJpgByJpegLib
- 编译jpeglib
LINUX下载编译jpeglib_柳鲲鹏的博客-CSDN博客_jpeglib库linux下载
- 源码
#include
#include
#include
#include
#define JPEG_QUALITY 75
int Nv12ToJpgFile(const char *pJpgFile,
const char* pYUVBuffer, const int nWidth, const int nHeight)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
FILE * pJpegFile = NULL;
unsigned char *yuvbuf = NULL;
unsigned char *ybase = NULL, *ubase = NULL;
int i=0, j=0;
int idx=0;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((pJpegFile = fopen(pJpgFile, "wb")) == NULL)
{
return -1;
}
jpeg_stdio_dest(&cinfo, pJpegFile);
// image width and height, in pixels
cinfo.image_width = nWidth;
cinfo.image_height = nHeight;
cinfo.input_components = 3; // # of color components per pixel
cinfo.in_color_space = JCS_YCbCr; //colorspace of input image
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, JPEG_QUALITY, 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);
yuvbuf=(unsigned char *)malloc(nWidth*3);
if(yuvbuf == NULL)
{
return -1;
}
memset(yuvbuf, 0, nWidth*3);
ybase=(unsigned char *)pYUVBuffer;
ubase=(unsigned char *)(pYUVBuffer+nWidth*nHeight);
while (cinfo.next_scanline < cinfo.image_height)
{
idx=0;
for(i=0;i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?