一、使用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就行了。
文章来源地址https://www.toymoban.com/news/detail-654992.html
二、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;
}
文章来源:https://www.toymoban.com/news/detail-654992.html
到了这里,关于[UE4][C++]使用qrencode动态生成二维码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!