[mw_shl_code=cpp,true]UTexture2D * AMyQRCode::GetTexture2DFromDiskFile()
{
TArray<uint8> RawFileData;
UTexture2D* MyTexture = NULL;
FString FilePath = FPaths:rojectDir() + "TargetQRCode.bmp";
if (FFileHelper:oadFileToArray(RawFileData, *FilePath /*"<path to file>"*/))
{
IImageWrapperModule& ImageWrapperModule = FModuleManager:oadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
// Note: PNG format. Other formats are supported
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
const TArray<uint8>* UncompressedBGRA = NULL;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
// Create the UTexture for rendering
MyTexture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
// Fill in the source data from the file
void* TextureData = MyTexture->latformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
MyTexture->latformData->Mips[0].BulkData.Unlock();
// Update the rendering resource from data.
MyTexture->UpdateResource();
}
}
}
return MyTexture;
}[/mw_shl_code] |