UE4生成二维码

来源:互联网 发布:水果店软件 编辑:程序博客网 时间:2024/05/22 17:28

1.首先到网上找个二维码的库,按照以前的文章导出第三方静态库

2. .h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "GameFramework/Actor.h"#include "QRcode.generated.h"UCLASS()class TTTTT_API AQRcode : public AActor{GENERATED_BODY()public:// Sets default values for this actor's propertiesAQRcode();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;UFUNCTION(BlueprintCallable, Category = "My|MyActor")FString SetQECode(FString name = "Hello");};
.cpp

// Fill out your copyright notice in the Description page of Project Settings.#include "TTTTT.h"#include "QRcode.h"#include "qrencode.h"#include <string>#include <fstream>using namespace std;#define QRCODE_TEXT"http://www.ultramundum.org/index.htm";// Text to encode into QRCode#define OUT_FILE"E:/test.bmp"// Output file name#define OUT_FILE_PIXEL_PRESCALER8// Prescaler (number of pixels in bmp file for each QRCode pixel, on each dimension)#define PIXEL_COLOR_R0// Color of bmp pixels#define PIXEL_COLOR_G0#define PIXEL_COLOR_B0// BMP definestypedef unsigned shortWORD;typedef unsigned longDWORD;typedef signed longLONG;#define BI_RGB0L#pragma pack(push, 2) //2字节对齐,不然会出问题typedef struct{WORD    bfType;DWORD   bfSize;WORD    bfReserved1;WORD    bfReserved2;DWORD   bfOffBits;} BITMAPFILEHEADER;typedef struct{DWORD      biSize;LONG       biWidth;LONG       biHeight;WORD       biPlanes;WORD       biBitCount;DWORD      biCompression;DWORD      biSizeImage;LONG       biXPelsPerMeter;LONG       biYPelsPerMeter;DWORD      biClrUsed;DWORD      biClrImportant;} BITMAPINFOHEADER;#pragma pack(pop)//-------------------------------------------------------// Sets default valuesAQRcode::AQRcode(){ // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;}// Called when the game starts or when spawnedvoid AQRcode::BeginPlay(){Super::BeginPlay();} //Called every framevoid AQRcode::Tick(float DeltaTime){Super::Tick(DeltaTime);}FString AQRcode::SetQECode(FString name){FString SavePath = FPaths::GameDir() + "Content/test.bmp";std::string MyStdString(TCHAR_TO_UTF8(*name));std::string MyStdPath(TCHAR_TO_UTF8(*SavePath));const char*szSourceSring = MyStdString.c_str();    //QRCODE_TEXTconst char*szSavePathSring = MyStdPath.c_str();unsigned intunWidth, x, y, l, n, unWidthAdjusted, unDataBytes;unsigned char*pRGBData, *pSourceData, *pDestData;QRcode*pQRC;FILE*f;// Compute QRCodepQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1);if (pQRC){unWidth = pQRC->width; // 矩阵的维数unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3; //每一个维度占的像素的个数(8),每个像素3个字节if (unWidthAdjusted % 4)unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER;// Allocate pixels bufferpRGBData = (unsigned char*)malloc(unDataBytes);if (!pRGBData){printf("Out of memory");exit(-1);}// Preset to whitememset(pRGBData, 0xff, unDataBytes);// Prepare bmp headersBITMAPFILEHEADER kFileHeader;kFileHeader.bfType = 0x4d42;  // "BM"kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER) +unDataBytes;kFileHeader.bfReserved1 = 0;kFileHeader.bfReserved2 = 0;kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER);BITMAPINFOHEADER kInfoHeader;kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER); //负数可以控制图像上下颠倒kInfoHeader.biPlanes = 1;kInfoHeader.biBitCount = 24;kInfoHeader.biCompression = BI_RGB;kInfoHeader.biSizeImage = 0;kInfoHeader.biXPelsPerMeter = 0;kInfoHeader.biYPelsPerMeter = 0;kInfoHeader.biClrUsed = 0;kInfoHeader.biClrImportant = 0;// Convert QrCode bits to bmp pixelspSourceData = pQRC->data;for (y = 0; y < unWidth; y++){pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER;for (x = 0; x < unWidth; x++){if (*pSourceData & 1){for (l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++){for (n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++){*(pDestData + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_B;*(pDestData + 1 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_G;*(pDestData + 2 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_R;}}}pDestData += 3 * OUT_FILE_PIXEL_PRESCALER;pSourceData++;}}// Output the bmp fileif (!(fopen_s(&f, szSavePathSring, "wb"))){fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);fclose(f);}else{GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("QR Unable to open file!"));}// Free datafree(pRGBData);QRcode_free(pQRC);return SavePath;}else{GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("QR NULL returned!"));return SavePath;}}
3.
参考:

http://blog.csdn.net/liyuanbhu/article/details/44647139
http://blog.csdn.net/sunnyloves/article/details/53053487
http://blog.csdn.net/jameskun77/article/details/53518113

原创粉丝点击