操作系統: windows 7 旗艦版
1. 編譯libjpeg
- "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
- cd C:\temp\jpegsrc.v6b\jpeg-6b
- nmake -f Makefile
/* INT32 must hold at least signed 32-bit values. */
#ifndef XMD_H /* X11/xmd.h correctly defines INT32 */
typedef long INT32;
#endif
// INT32 must hold at least signed 32-bit values.
//#ifndef XMD_H // X11/xmd.h correctly defines INT32
#if !defined( XMD_H ) && !defined( WIN32 )
typedef long INT32;
#endif
#ifdef NEED_FAR_POINTERS
#define FAR far
#else
#define FAR
#endif
#ifdef NEED_FAR_POINTERS
#define FAR far
#else
#undef FAR // Added by Linden Lab to remove warnings
#define FAR
#endif
"jerror.h"
"jinclude.h"
"jmorecfg.h"
"jpeglib.h"
2. 使用libjpeg
extern "C" {
#include "jpeglib.h"
}
#include <windows.h>
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include "jpeglib.h" //
for jpeglib
};
//實現視頻幀的jpeg壓縮
void saveAsJpeg(AVFrame *pFrameRGB, int width, int height, int framenum)
{
char fname[128];
// AVPicture my_pic ;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
int row_stride;
uint8_t *buffer;
FILE *fp = NULL;
buffer = pFrameRGB->data[0];
int size = sizeof(buffer);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
//_snprintf(fname, sizeof(fname), "frames%d.jpg", framenum);
sprintf(fname, "frames%d.jpg", framenum);
fp = fopen(fname, "wb");
if (fp == NULL)
return;
jpeg_stdio_dest(&cinfo, fp);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components =
3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo,
80, true);
jpeg_start_compress(&cinfo, TRUE);
row_stride = width *
3;
while (cinfo.next_scanline < height)
{
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer,
1);
}
jpeg_finish_compress(&cinfo);
fclose(fp);
jpeg_destroy_compress(&cinfo);
printf("compress %d frame
finished!\n",framenum) ;
return ;
}
|