• UE4 读取本地图片


    参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-html5-p.html


    我这里,不能将图片全放工程之中,需要在外部在加载图片资源,再来使用


    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的格式的时候,就完全正常,还未去寻找原因

  • 相关阅读:
    [ Algorithm ] N次方算法 N Square 动态规划解决
    [ Algorithm ] LCS 算法 动态规划解决
    sql server全文索引使用中的小坑
    关于join时显示no join predicate的那点事
    使用scvmm 2012的动态优化管理群集资源
    附加数据库后无法创建发布,error 2812 解决
    浅谈Virtual Machine Manager(SCVMM 2012) cluster 过载状态检测算法
    windows 2012 r2下安装sharepoint 2013错误解决
    sql server 2012 数据引擎任务调度算法解析(下)
    sql server 2012 数据引擎任务调度算法解析(上)
  • 原文地址:https://www.cnblogs.com/liang123/p/6325849.html
Copyright © 2020-2023  润新知