⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpegwriter.cpp

📁 服务端: 这是一个完整的基于Wince 4.1图形图象,网络通讯(C/S)模式下的商用程序源代码包.
💻 CPP
字号:
#include "StdAfx.h"
#include "JpegWriter.h"
#include "STScreenBuffer.h"

extern "C" {
	#include "../JpegLib/jpeglib.h"
}

static CString strError;

const static CString CANT_OPEN_FILE = TEXT("Can't open file\"%s\"");
const static CString EMPTY_STRING = TEXT("");


struct extended_error_mgr {
  struct jpeg_error_mgr pub;
  jmp_buf setjmp_buffer;
};

typedef struct extended_error_mgr * extended_error_ptr;

METHODDEF(void)
extended_error_exit(j_common_ptr cinfo)
{
	extended_error_ptr err = (extended_error_ptr)cinfo->err;
	(*cinfo->err->output_message)(cinfo);
	
	longjmp(err->setjmp_buffer, 1);
}

METHODDEF(void)
extended_reset_error_mgr(j_common_ptr cinfo)
{
	cinfo->err->num_warnings = 0;
	cinfo->err->msg_code = 0;

	strError = EMPTY_STRING;
}

METHODDEF(void)
extended_output_message(j_common_ptr cinfo)
{
  char buffer[JMSG_LENGTH_MAX];

  (*cinfo->err->format_message)(cinfo, buffer);

  strError = buffer;
}

CString GetJpegWriterError() 
{
	return strError;
}

BOOL WriteRGBBytesIntoJpegFile(const CString& strOutFileName, const int nWidth, const int nHeight, const int nQuality, BYTE* pBuffer) 
{
	struct jpeg_compress_struct cinfo;
	struct extended_error_mgr jerr;
	JSAMPROW row_pointer[1];
	int row_stride;

	FILE* outfile;
	if ((outfile = _tfopen(strOutFileName, TEXT("wb"))) == NULL) {
		strError.Format(CANT_OPEN_FILE, strOutFileName);
		return FALSE;
	}

	cinfo.err = jpeg_std_error(&jerr.pub);
	jerr.pub.error_exit = extended_error_exit;
	jerr.pub.output_message = extended_output_message;
	jerr.pub.reset_error_mgr = extended_reset_error_mgr;

	if (setjmp(jerr.setjmp_buffer)) {
		jpeg_destroy_compress(&cinfo);
		fclose(outfile);
		return FALSE;
	}

	jpeg_create_compress(&cinfo);
	jpeg_stdio_dest(&cinfo, outfile);

	cinfo.image_width = nWidth; 	
	cinfo.image_height = nHeight;
	cinfo.input_components = 3;	
	cinfo.in_color_space = JCS_RGB; 	

	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, nQuality, TRUE);

	jpeg_start_compress(&cinfo, TRUE);

	row_stride = nWidth * 3;

	while (cinfo.next_scanline < cinfo.image_height) {
		row_pointer[0] = & pBuffer[cinfo.next_scanline * row_stride];
		jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}

	jpeg_finish_compress(&cinfo);
	fclose(outfile);
	jpeg_destroy_compress(&cinfo);

	return TRUE;
}

BOOL WriteBitmapIntoJpegFile(const CString& strOutFileName, const int nQuality, HBITMAP hBitmap) 
{
	CSTScreenBuffer screenBuffer;
	screenBuffer.Create(hBitmap);

	BYTE* pBitmapBytes = new BYTE[screenBuffer.GetWidth() * screenBuffer.GetHeight() * 3];

	int i = 0;
	for (int nY = 0; nY < screenBuffer.GetHeight(); nY++) {
		for (int nX = 0; nX < screenBuffer.GetWidth(); nX++) {
			BGRColor color = screenBuffer.GetPoint(nX, nY);
			pBitmapBytes[i++] = color.m_R;
			pBitmapBytes[i++] = color.m_G;
			pBitmapBytes[i++] = color.m_B;
		}
	}
	BOOL res = WriteRGBBytesIntoJpegFile(strOutFileName, screenBuffer.GetWidth(), screenBuffer.GetHeight(), nQuality, pBitmapBytes);

	delete[] pBitmapBytes;

	return res;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -