1.创建并显示
QRcode* pQR_Encode;
pQR_Encode = QRcode_encodeString("12345678901234567890", 0, QR_ECLEVEL_H, QR_MODE_8, 1);
if (pQR_Encode)
{
int nBmpWidth = pQR_Encode->width;
//获取控件的边界大小
CRect rect;
GetDlgItem(IDC_PIC)->GetClientRect(&rect);
CStatic* pic =(CStatic * )GetDlgItem(IDC_PIC);
//定义并创建一个内存设备环境DC
CDC* pCurCDC = GetDlgItem(IDC_PIC)->GetDC();
//CDC *pCurCDC = GetDC();
//创建兼容性的DC
CDC* pWorkDC = new CDC;
pWorkDC->CreateCompatibleDC(pCurCDC);
//pWorkDC->CreateCompatibleDC(NULL);
CBitmap workBitmap;
CBitmap backBitmap;
CDC* pBackDC = new CDC;
pBackDC->CreateCompatibleDC(pCurCDC);
//workBitmap.CreateCompatibleBitmap(pWorkDC, nBmpWidth, nBmpWidth);
workBitmap.CreateCompatibleBitmap(pWorkDC, rect.Width(), rect.Height());
backBitmap.CreateCompatibleBitmap(pBackDC, rect.Width(), rect.Height());
CBitmap* pOldBitmap = pWorkDC->SelectObject(&workBitmap);//将位图选入临时内存设备环境
CBitmap* pbackBitmap = pBackDC->SelectObject(&backBitmap);
// 背景全部画成白色
pWorkDC->PatBlt(0, 0, nBmpWidth, nBmpWidth, WHITENESS);
// pBackDC->PatBlt(0, 0, nBmpWidth, nBmpWidth, WHITENESS);
// 根据二维码内容绘制位图
unsigned char* pSourceData = pQR_Encode->data;
for (int i = 0; i < pQR_Encode->width; ++i)
{
for (int j = 0; j < pQR_Encode->width; ++j)
{
if (*pSourceData & 1)
{
pWorkDC->SetPixel(i, j, RGB(0, 0, 0));
//pCurCDC->SetPixel(i, j, RGB(0, 0, 0));
}
pSourceData++;
}
}
pic->ModifyStyle(SS_ENHMETAFILE, SS_BITMAP | SS_CENTERIMAGE);
//pic->ModifyStyle(SS_ENHMETAFILE, SS_BITMAP | SS_REALSIZEIMAGE);
//pic->SetBitmap(HBITMAP(pWorkDC->GetCurrentBitmap()->GetSafeHandle()));
//pic->SetBitmap((HBITMAP )workBitmap.GetSafeHandle());
// 图片显示调用函数stretchBlt
//::StretchBlt(pCurCDC->GetSafeHdc(),335, 33, 150, 150, pWorkDC->GetSafeHdc(), 0, 0,nBmpWidth, nBmpWidth, SRCCOPY);
pBackDC->StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), pWorkDC, 0, 0, nBmpWidth, nBmpWidth, SRCCOPY);
// pic->SetBitmap(HBITMAP(pCurCDC->GetCurrentBitmap()->Detach()));
pWorkDC->SelectObject(pOldBitmap);
pBackDC->SelectObject(pbackBitmap);
pic->SetBitmap((HBITMAP)backBitmap.Detach());
DeleteObject(pOldBitmap); //删除内存中的位图
DeleteObject(pbackBitmap); //删除内存中的位图
pWorkDC->DeleteDC(); //删除CreateCompatibleDC得到的图片DC
delete pWorkDC;
pBackDC->DeleteDC();
delete pBackDC;
ReleaseDC(pCurCDC); //释放获取的DC句柄
QRcode_free(pQR_Encode);
}
2.创建并保存为图像文件
void GenerateQRcode(char* msg) {
unsigned int unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
unsigned char* pRGBData, * pSourceData, * pDestData;
QRcode* pQRC;
FILE* f;
if (pQRC = QRcode_encodeString(msg, 0, QR_ECLEVEL_H, QR_MODE_8, 1))
{
unWidth = pQRC->width;
unWidthAdjusted = unWidth * 8 * 3;
if (unWidthAdjusted % 4)
unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
unDataBytes = unWidthAdjusted * unWidth * 8;
// Allocate pixels buffer
if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
{
exit(-1);
}
// Preset to white
memset(pRGBData, 0xff, unDataBytes);
// Prepare bmp headers
// 位图文件头
BITMAPFILEHEADER kFileHeader;
kFileHeader.bfType = 0x4d42; // 位图文件的类型,必须为"BM"
kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) + unDataBytes;// 位图文件的大小
kFileHeader.bfReserved1 = 0;// 位图文件保留字,必须为0
kFileHeader.bfReserved2 = 0;// 位图文件保留字,必须为0
kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);// 位图数据的起始位,位图文件头+位图信息头+调色板的大小
// 位图信息头
BITMAPINFOHEADER kInfoHeader;
kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);// 本结构所占用字节数
kInfoHeader.biWidth = unWidth * 8; // 位图的宽度,以像素为单位
kInfoHeader.biHeight = ((int)unWidth * 8);// 位图的高度,以像素为单位
kInfoHeader.biPlanes = 1;// 目标设备的级别,必须为1
kInfoHeader.biBitCount = 24; // 每个像素所需的位数,必须是1(双色)、
//4(16色)、8(256色)或24(真彩色)之一
kInfoHeader.biCompression = BI_RGB;// 位图压缩格式,必须是0,不压缩
kInfoHeader.biSizeImage = 0; // 位图的大小,以字节为单位
kInfoHeader.biXPelsPerMeter = 0; // 位图水平分辨率
kInfoHeader.biYPelsPerMeter = 0; // 位图垂直分辨率
kInfoHeader.biClrUsed = 0; // 位图实际使用的颜色表中的颜色数
kInfoHeader.biClrImportant = 0; // 位图显示过程中重要的颜色数,0表示所有的颜色都重要
// Convert QrCode bits to bmp pixels
pSourceData = pQRC->data;
for (int y = unWidth - 1; y >= 0; y--)
{
pDestData = pRGBData + unWidthAdjusted * y * 8;
// y
for (x = 0; x < unWidth; x++)
{
if (*pSourceData & 1)
{
for (int l = 0; l < 8; l++)
{
for (int n = 0; n < 8; n++)
{
*(pDestData + n * 3 + unWidthAdjusted * l) = 0;
*(pDestData + 1 + n * 3 + unWidthAdjusted * l) = 0;
*(pDestData + 2 + n * 3 + unWidthAdjusted * l) = 0;
}
}
}
pDestData += 3 * 8;
pSourceData++;
}
}
// Output the bmp file
if (!(fopen_s(&f, "temp.bmp", "wb")))
{
fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);
fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
fclose(f);
}
else
{
printf("Unable to open file");
exit(-1);
}
// Free data
free(pRGBData);
QRcode_free(pQRC);
}
else
{
printf("NULL returned");
exit(-1);
}
}