一、使用CMake編譯x64版本qrencode
? ? ?下載地址?
GitHub - fukuchi/libqrencode: A fast and compact QR Code encoding libraryA fast and compact QR Code encoding library. Contribute to fukuchi/libqrencode development by creating an account on GitHub.https://github.com/fukuchi/libqrencode
?Configure之后會報錯,不勾選WITH_TOOLS就行了。
?
?
?
?
二、UE4引入
還是采用第三方庫的形式,引入頭文件和靜態lib文件 ,注意只能在cpp里調用
#include "Runtime\Core\Public\Windows\AllowWindowsPlatformTypes.h"
#include <Windows.h>
#include "qrencode.h"
#include "Runtime\Core\Public\Windows\HideWindowsPlatformTypes.h"
?三、動態生成
以下代碼是在游戲自己創建的GameInstace里調用的,函數定義自己寫了
UTexture2D* UWGameInstance::CreateQRCode(FString Path,int Scale)
{QRcode* pQRC = QRcode_encodeString(TCHAR_TO_ANSI(*Path),1,QR_ECLEVEL_L,QR_MODE_8,1);if (!pQRC)return nullptr;unsigned char* QRData = pQRC->data;UTexture2D* tex = UTexture2D::CreateTransient(pQRC->width* Scale, pQRC->width* Scale);//FTexture2DMipMap& mipmap = tex->PlatformData->Mips[0];unsigned char* Data = (unsigned char*)mipmap.BulkData.Lock(LOCK_READ_WRITE);int texturex = tex->PlatformData->SizeX;int texturey = tex->PlatformData->SizeY;for (int32 y = 0; y < texturey; y+= Scale){for (int32 x = 0; x < texturex; x+= Scale){int color = 0;if (*QRData & 1){color = 255;}for (int32 i = 0; i < Scale; i++){for (int32 j = 0; j < Scale; j++){Data[((y + i) * texturex + (x + j)) * 4 + 0] = color;//BData[((y + i) * texturex + (x + j)) * 4 + 1] = color;//GData[((y + i) * texturex + (x + j)) * 4 + 2] = color;//RData[((y + i) * texturex + (x + j)) * 4 + 3] = 255;//A 0:全透明;255:全不透明}}//后移QRData++;}}mipmap.BulkData.Unlock();tex->UpdateResource();//釋放QRcode_free(pQRC);return tex;
}
?
?