1. 程式人生 > >UE4 讀取本地圖片

UE4 讀取本地圖片

我這裡,不能將圖片全放工程之中,需要在外部在載入圖片資源,再來使用

1.通過本地圖片路徑,獲取圖片,並將其資料轉為uint型別的陣列

#pragma region 通過本地圖片轉換成UTexture2D
UTexture2D* AMyProjectGameMode::GetLocalTexture(const FString &_TexPath)
{
	UTexture2D* OutTex=NULL;
	IImageWrapperModule& imageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	IImageWrapperPtr imageWrapper = imageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

	TArray<uint8> OutArray;
	if(FFileHelper::LoadFileToArray(OutArray, *_TexPath))
	{
		if (imageWrapper.IsValid()&&
			imageWrapper->SetCompressed(OutArray.GetData(), OutArray.Num()))
		{
			const TArray<uint8>* uncompressedRGBA = NULL;
			if (imageWrapper->GetRaw(ERGBFormat::RGBA, 8, uncompressedRGBA))
			{
				const TArray<FColor> uncompressedFColor=uint8ToFColor(*uncompressedRGBA);
				OutTex=TextureFromImage(
					imageWrapper->GetWidth(),
					imageWrapper->GetHeight(),
					uncompressedFColor,
					true);
			}
		}
	}
	return OutTex;
}
#pragma endregion
2.將uint8陣列轉為顏色陣列
#pragma region 將uint8陣列轉為顏色陣列
TArray<FColor> AMyProjectGameMode::uint8ToFColor(const TArray<uint8> origin)
{
	TArray<FColor> uncompressedFColor;
	uint8 auxOrigin;
	FColor auxDst;

	for (int i = 0; i < origin.Num(); i++) {
		auxOrigin = origin[i];
		auxDst.R = auxOrigin;
		i++;
		auxOrigin = origin[i];
		auxDst.G = auxOrigin;
		i++;
		auxOrigin = origin[i];
		auxDst.B = auxOrigin;
		i++;
		auxOrigin = origin[i];
		auxDst.A = auxOrigin;
		uncompressedFColor.Add(auxDst);
	}

	return  uncompressedFColor;

}
#pragma endregion
3.將顏色陣列賦值給Texture
#pragma region 將顏色陣列賦值給Texture
UTexture2D* AMyProjectGameMode::TextureFromImage(const int32 SrcWidth, const int32 SrcHeight, const TArray<FColor> &SrcData, const bool UseAlpha) 
{

	// 建立Texture2D紋理
	UTexture2D* MyScreenshot = UTexture2D::CreateTransient(SrcWidth, SrcHeight, PF_B8G8R8A8);

	// 鎖住他的資料,以便修改
	uint8* MipData = static_cast<uint8*>(MyScreenshot->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));

	// 建立紋理資料
	uint8* DestPtr = NULL;
	const FColor* SrcPtr = NULL;
	for (int32 y = 0; y<SrcHeight; y++)
	{
		DestPtr = &MipData[(SrcHeight - 1 - y) * SrcWidth * sizeof(FColor)];
		SrcPtr = const_cast<FColor*>(&SrcData[(SrcHeight - 1 - y) * SrcWidth]);
		for (int32 x = 0; x<SrcWidth; x++)
		{
			*DestPtr++ = SrcPtr->B;
			*DestPtr++ = SrcPtr->G;
			*DestPtr++ = SrcPtr->R;
			if (UseAlpha)
			{
				*DestPtr++ = SrcPtr->A;
			}
			else
			{
				*DestPtr++ = 0xFF;
			}
			SrcPtr++;
		}
	}

	// 解鎖紋理
	MyScreenshot->PlatformData->Mips[0].BulkData.Unlock();
	MyScreenshot->UpdateResource();

	return MyScreenshot;
}
#pragma endregion
4.我這裡建圖片路徑放在工程的相對路徑下,呼叫GetLocalTexture函式,獲取Texture2D
void AMyProjectGameMode::BeginPlay()
{
	const FString _FilePath = FPaths::GameDir() + "video_logo.png";
	_UITex = GetLocalTexture(_FilePath);
}
5.注意別忘了,需要新增兩個標頭檔案
#include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapper.h"
#include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapperModule.h"
6.執行截圖,我這裡將獲取的圖片放在了UI介面上

7.我在讀取jpg格式的圖片的時候,顏色明顯不對,讀png的格式的時候,就完全正常,還未去尋找原因