码迷,mamicode.com
首页 > 编程语言 > 详细

C语言生成二维码

时间:2017-10-31 11:06:05      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:es2017   har   ram   class   call   des   name   cpp   demo   

技术分享

如图分享一个C语言写的生成二维码的库,可以保存为BMP文件也可以直接显示在GDI上。

Demo下载地址:https://pan.baidu.com/s/1c1UmRhY

 

#include <windows.h>
#include "qrencode.h"

QRcode* pQRC = NULL;

LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
	if (pQRC = QRcode_encodeString("测试二维码", 0, QR_ECLEVEL_H, QR_MODE_8, 1))
	{
		QRcode_NewBGR24(pQRC,0x00,0x00,0x00);
		QRcode_SaveBMP(pQRC,"1.bmp");

		char* clsName = "MyApp";
		WNDCLASS wc;
		ZeroMemory(&wc,sizeof(WNDCLASS));
		wc.hInstance = hInstance;
		wc.lpszClassName = clsName;
		wc.lpfnWndProc = MyWindowProc;
		wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
		wc.style = CS_HREDRAW | CS_VREDRAW;
		RegisterClass(&wc);
		HWND hMainwind = CreateWindow(clsName,"二维码",WS_OVERLAPPEDWINDOW,
			400,300,600,500,NULL,NULL,hInstance,NULL);
		if(hMainwind == NULL) return 0;
		ShowWindow(hMainwind,SW_NORMAL);
		MSG msg;
		while(GetMessage(&msg,NULL,0,0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		
		QRcode_free(pQRC);
		return 0;
	}
	return 1;
}

LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			BeginPaint(hwnd, &ps);

			RECT rect;
			GetClientRect(hwnd,&rect);

			BYTE *src_buff_ex = new BYTE[pQRC->rgbW*pQRC->rgbW*4];
			for(int i=0, j=0; j < pQRC->rgbW*pQRC->rgbW*4; i+=3, j+=4)
			{
				*(src_buff_ex+j) = *(pQRC->rgbData+i);
				*(src_buff_ex+j+1) = *(pQRC->rgbData+i+1);
				*(src_buff_ex+j+2) = *(pQRC->rgbData+i+2);
				*(src_buff_ex+j+3) = 0;
			}

			HDC hMemDC_ = CreateCompatibleDC(ps.hdc);
			HBITMAP hBitmapSrc_ = CreateCompatibleBitmap(ps.hdc, pQRC->rgbW, pQRC->rgbW);
			SelectObject(hMemDC_, hBitmapSrc_);
			
			long err = SetBitmapBits(hBitmapSrc_, pQRC->rgbW*pQRC->rgbW*4, src_buff_ex);
			if(err != 0)
			{
				BitBlt(ps.hdc, (rect.right-pQRC->rgbW)/2, (rect.bottom-pQRC->rgbW)/2,
					pQRC->rgbW, pQRC->rgbW, hMemDC_, 0, 0, SRCCOPY);
			}
			
			DeleteObject(hBitmapSrc_);
			DeleteDC(hMemDC_);

			free(src_buff_ex);

			EndPaint(hwnd, &ps);
		}
		break;
		case WM_DESTROY:
		{
			PostQuitMessage(0);
		}return 0;
		default:break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}

  

C语言生成二维码

标签:es2017   har   ram   class   call   des   name   cpp   demo   

原文地址:http://www.cnblogs.com/hatsusakana/p/7759985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!