[UE4][C++]使用qrencode动态生成二维码

一、使用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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值