1.開源二維碼庫libqrencode的下載地址
libqrencode是一個日本人寫的生成二維碼的能夠跨平臺的c庫,官網是這裏,Github在這裏。html
tar zxvf qrencode-3.4.2.tar.gz cd qrencode-3.4.2 ./configure make make install
安裝完成後咱們能夠發現共享庫下會多出 ios
1. /usr/local/include qrencode.h 頭文件git
2. /usr/local/lib .so 共享庫文件github
這就是咱們開發須要的東西bash
1.編寫主程序代碼--main.cppide
//main.cpp #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <cstdlib> #include "qrencode.h" using namespace std; typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned int DWORD; //位圖文件頭定義; typedef struct tagBITMAPFILEHEADER { // WORD bfType;//單獨讀取,結構體中就不定義了 DWORD bfSize; //文件大小 WORD bfReserved1; //保留字 WORD bfReserved2; //保留字 DWORD bfOffBits; //從文件頭到實際位圖數據的偏移字節數 } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { DWORD biSize; //信息頭大小 DWORD biWidth; //圖像寬度 DWORD biHeight; //圖像高度 WORD biPlanes; //位平面數,必須爲1 WORD biBitCount; //每像素位數 DWORD biCompression; //壓縮類型 DWORD biSizeImage; //壓縮圖像大小字節數 DWORD biXPelsPerMeter; //水平分辨率 DWORD biYPelsPerMeter; //垂直分辨率 DWORD biClrUsed; //位圖實際用到的色彩數 DWORD biClrImportant; //本位圖中重要的色彩數 } BITMAPINFOHEADER; //位圖信息頭定義 //像素信息 typedef struct tagIMAGEDATA { BYTE blue; BYTE green; BYTE red; } DATA; int main() { const char *QRTEXT = "歡迎來到個人的博客"; QRcode *qrCode; int version = 5; //設置版本號,這裏設爲5,對應尺寸:37 * 37 QRecLevel level = QR_ECLEVEL_H; QRencodeMode hint = QR_MODE_8; int casesensitive = 1; qrCode = QRcode_encodeString(QRTEXT, version, level, hint, casesensitive); if (NULL == qrCode) { printf("QRcode create fail\n"); return -1; } //將要生成的二維碼保存爲BMP真彩色圖片文件 FILE *pf = fopen("qrcode.bmp", "wb"); if (NULL == pf) { printf("file open fail.\n"); fclose(pf); return -1; } int width = qrCode->width; int height = qrCode->width; int biCount = 24; //真彩色 int lineByte = (width * biCount / 8 + 3) / 4 * 4; //每line字節數必須爲4的倍數 //位圖文件頭 BITMAPFILEHEADER bitMapFileHeader; //bitMapFileHeader.bfType = 0x4D42; bitMapFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lineByte * height; bitMapFileHeader.bfReserved1 = 0; bitMapFileHeader.bfReserved2 = 0; bitMapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //位圖信息頭 BITMAPINFOHEADER bitMapInfoHeader; bitMapInfoHeader.biBitCount = biCount; bitMapInfoHeader.biClrImportant = 0; bitMapInfoHeader.biClrUsed = 0; bitMapInfoHeader.biCompression = 0; bitMapInfoHeader.biHeight = height; bitMapInfoHeader.biPlanes = 1; bitMapInfoHeader.biSize = 40; bitMapInfoHeader.biSizeImage = lineByte * height; bitMapInfoHeader.biWidth = width; bitMapInfoHeader.biXPelsPerMeter = 0; bitMapInfoHeader.biYPelsPerMeter = 0; WORD bfType = 0x4D42; //BM類型 fwrite(&bfType, sizeof(WORD), 1, pf); //寫文件頭進文件 fwrite(&bitMapFileHeader, sizeof(BITMAPFILEHEADER), 1, pf); //寫位圖信息頭進文件 fwrite(&bitMapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pf); unsigned char *pBMPData = new unsigned char[lineByte * height]; memset(pBMPData, 255, lineByte * height); unsigned char *qrData = qrCode->data; for (int i = 0; i < height; i++) { for (int j = 0; j < lineByte / 3; j++) { if (*(qrData)&1) { //設置rgb顏色,可自定義設置,這裏設爲黑色。 *(pBMPData + lineByte * i + 3 * j) = 0; *(pBMPData + lineByte * i + 3 * j + 1) = 0; *(pBMPData + lineByte * i + 3 * j + 2) = 0; } qrData++; } } //寫數據進文件 fwrite(pBMPData, sizeof(unsigned char), lineByte * height, pf); fclose(pf); delete[] pBMPData; pBMPData = NULL; QRcode_free(qrCode); return 0; }
2.makefilespa
# A commonMakefile for c programs, version 1.0 # Copyright (C)2014 shallnew \at 163 \dot com CC = g++ #CFLAGS += -g -Wall -Werror -O2 CPPFLAGS += -I/usr/local/include LDFLAGS += -L/usr/local/lib -lqrencode # SRC_OBJ =$(patsubst %.c, %.o, $(wildcard *.c)) SRC_FILES =$(wildcard *.cpp) SRC_OBJ =$(SRC_FILES:.cpp=.o) SRC_BIN =Qrcode $(SRC_BIN) :$(SRC_OBJ) $(CC) -o $@ -g $^ $(CPPFLAGS) $(LDFLAGS) $(RM) $(SRC_OBJ) *~ clean: $(RM) $(SRC_OBJ) $(SRC_BIN) *~
3.編譯運行code
問題點: 你可能在編譯的時候遇到htm
這是由於你尚未將Qrencode庫文件編寫進共享庫中blog
你能夠執行一下命令進行解決
echo "/usr/local/lib" >> /etc/ld.so.conf /sbin/ldconfig
若是過程順利的話你將看到一個小型的二維碼圖片
GG思密達