Skip to content

Instantly share code, notes, and snippets.

@Bunkerbewohner
Created September 30, 2020 10:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bunkerbewohner/84bd704e1cb1da17cb086aadb32cd1cd to your computer and use it in GitHub Desktop.
Save Bunkerbewohner/84bd704e1cb1da17cb086aadb32cd1cd to your computer and use it in GitHub Desktop.
#include "QrCode.hpp" // from https://github.com/nayuki/QR-Code-generator
#include "ImageUtils.h" // from Unreal Engine (4.24)
/// <summary>Generates a QR code texture from a string.</summary>
/// <param name="parent">UE parent (required)</param>
/// <param name="string">String to encode</param>
UTexture2D* UWebBuzzers::GenerateQrCode(UObject* parent, FString string)
{
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(TCHAR_TO_UTF8(*string), qrcodegen::QrCode::Ecc::LOW);
uint8 size = qr.getSize();
TArray<FColor> pixels;
pixels.SetNumZeroed(size * size);
FColor black = FColor::Black;
FColor white = FColor::White;
for (uint8 x = 0; x < size; x++)
{
for (uint8 y = 0; y < size; y++)
{
FColor color = qr.getModule(x, y) ? white : black;
pixels[x + y * size] = color;
}
}
UTexture2D* texture = UTexture2D::CreateTransient(size, size, EPixelFormat::PF_B8G8R8A8, "QRCode");
void* data = texture->PlatformData->Mips[0].BulkData.Lock(LOCK_WRITE);
FMemory::Memcpy(data, pixels.GetData(), size * size * 4);
texture->PlatformData->Mips[0].BulkData.Unlock();
texture->UpdateResource();
texture->Filter = TextureFilter::TF_Nearest;
return texture;
}
@pasotee
Copy link

pasotee commented Mar 28, 2022

For everyone using a more updated version of Unreal engine, getting compilation errors on:
void* data = texture->PlatformData->Mips[0].BulkData.Lock(LOCK_WRITE);
Try using:
void* data = texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE); instead.

@BustGame
Copy link

BustGame commented Dec 1, 2022

How can i implement this? I have no idea what to do with it.

@pasotee
Copy link

pasotee commented Dec 1, 2022

@BustGame feel free to reach out here: alexandru AT outofthebox-plugins.com and I can take a look if this still works in your version of Unreal.

@BustGame
Copy link

BustGame commented Dec 3, 2022

@BustGame feel free to reach out here: alexandru AT outofthebox-plugins.com and I can take a look if this still works in your version of Unreal.

I have no idea how can i contact you through this page but i cant implement library from nayuki into unreal engine. Tried many methods and still can't found solution. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment