C語言產生二維碼

來源:互聯網
上載者:User

標籤: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語言產生二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.