一、使用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;//B
Data[((y + i) * texturex + (x + j)) * 4 + 1] = color;//G
Data[((y + i) * texturex + (x + j)) * 4 + 2] = color;//R
Data[((y + i) * texturex + (x + j)) * 4 + 3] = 255;//A 0:全透明;255:全不透明
}
}
//后移
QRData++;
}
}
mipmap.BulkData.Unlock();
tex->UpdateResource();
//释放
QRcode_free(pQRC);
return tex;
}