VC++生成二维码
2017-08-18 本文已影响75人
晓函
用到的是日本人写的一个二维码算法,QR_Encode,
由于原始二维码非常迷你,才10多个像素宽,如果直接使用DrawImage方式拉伸,将会严重失真,
里面几行代码中实现了像素点按照倍数填充放大。
无论放大多少倍都是很清晰的。
//GDI+
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
//二维码算法
#include "../LIBS/QR_Encode.h"
//初始化
CCompositePic::CCompositePic():
init_succ_(true)
{
if (Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL) != Gdiplus::Ok)
{
g_log_wnd->Print(L"Gdiplus初始化失败");
init_succ_ = false;
return;
}
init_succ_ = true;
}
CCompositePic::~CCompositePic(){
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
//data字符串数据,size要生成的二维码大小,pic_path生成的图片路径
bool CCompositePic::DrawQrcode( wstring data, int size, wstring pic_path){
CQR_Encode* pQR_Encode = new CQR_Encode;
int nLevel = 1;
int nVersion = 0;
BOOL bAutoExtent = TRUE;
int nMaskingNo = -1;
string strEncodeData = StringParse::WStringToString(data);
BOOL bret = pQR_Encode->EncodeData(nLevel, nVersion, bAutoExtent, nMaskingNo, strEncodeData.c_str());
if (!bret)
return bret;
//计算出需要放大的比例
int scale_rate = size / pQR_Encode->m_nSymbleSize;
Gdiplus::Bitmap bitmap_qrcode(pQR_Encode->m_nSymbleSize*scale_rate, pQR_Encode->m_nSymbleSize*scale_rate);
//填上白底
Gdiplus::Graphics graphics_qrcode(&bitmap_qrcode);
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 255, 255, 255));
graphics_qrcode.FillRectangle(&brush, 0, 0, bitmap_qrcode.GetWidth(), bitmap_qrcode.GetHeight());
//根据二维码像素点数据 开始绘制
for (int i = 0; i < pQR_Encode->m_nSymbleSize; ++i)
{
for (int j = 0; j < pQR_Encode->m_nSymbleSize; ++j)
{
//如果有数据,则画上黑色
if (pQR_Encode->m_byModuleData[i][j])
{
//原始像素
//bitmap_qrcode.SetPixel(i, j, RGB(0, 0, 0));
//放大的倍数,每个像素点放大
for (int x = 0; x < scale_rate; x++)
for (int y = 0; y < scale_rate; y++)
bitmap_qrcode.SetPixel(i*scale_rate + x, j * scale_rate + y, RGB(1, 1, 1));
}
}
}
//保存图片
CLSID pngclsid = { 0 };
GetCodecClsid(TEXT("image/jpeg"), pngclsid);
DeleteFile(pic_path.c_str());
bitmap_qrcode.Save(pic_path.c_str(), &pngclsid);
if (!OS::IsExists(pic_path))
return false;
return true;
}
bool CCompositePic::GetCodecClsid(LPCTSTR lpstrFormat, CLSID &clsid)
{
UINT nNum = 0, nSize = 0;
Gdiplus::GetImageEncodersSize(&nNum, &nSize) == Gdiplus::Ok;
Gdiplus::ImageCodecInfo *pInfo = (Gdiplus::ImageCodecInfo *)malloc(nSize);
if (Gdiplus::GetImageEncoders(nNum, nSize, pInfo) != Gdiplus::Ok)
{
free(pInfo);
}
LPCWSTR lpstr = lpstrFormat;
for (int nIndex = 0; nIndex < nNum; ++nIndex)
{
if (wcscmp(pInfo[nIndex].MimeType, lpstr) == 0)
{
clsid = pInfo[nIndex].Clsid;
return true;
}
}
return false;
};