程序要怎么读取到指定路径下的图片文件
如题,哪位大佬能给一个思路么这个是普通文本文件的。我估计你用 LoadFileToArray 可以读二进制的图片文件
读出来以后,再想办法转换成纹理来显示吧
void PicReader::SaveConf()
{
TArray<FString> item;
item.Add(TEXT("Resolution=1920x1080"));
item.Add(TEXT("RenderAPI=VULKAN"));
item.Add(TEXT("Server=192.168.1.100"));
#if PLATFORM_WINDOWS
// Windows 下测试用
const TCHAR * confPath = TEXT("../conf/test.conf");
#else
// Linux 下保存到不同的目录
const TCHAR * confPath = TEXT("../../../../conf/test.conf");
#endif
// 会直接覆盖原有的文件
// WindowsNoEditor\FileRW\Binaries\Win64\FileRW.exe
FFileHelper::SaveStringArrayToFile(item, confPath);
TArray<FString> conf;
FFileHelper::LoadFileToStringArray(conf, confPath);
for (FString line : conf) {
UE_LOG(LogTemp, Warning, TEXT("Read line : %s"), *line);
}
}
UTexture2D * AMyQRCode::GetTexture2DFromDiskFile()
{
TArray<uint8> RawFileData;
UTexture2D* MyTexture = NULL;
FString FilePath = FPaths::ProjectDir() + "TargetQRCode.bmp";
if (FFileHelper::LoadFileToArray(RawFileData, *FilePath /*"<path to file>"*/))
{
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<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->PlatformData->Mips.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
MyTexture->PlatformData->Mips.BulkData.Unlock();
// Update the rendering resource from data.
MyTexture->UpdateResource();
}
}
}
return MyTexture;
}
页:
[1]