UE4 读取本地图片

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


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


1.通过本地图片路径,获取图片,并将其数据转为uint类型的数组

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma region 通过本地图片转换成UTexture2D  
  2. UTexture2D* AMyProjectGameMode::GetLocalTexture(const FString &_TexPath)  
  3. {  
  4.     UTexture2D* OutTex=NULL;  
  5.     IImageWrapperModule& imageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));  
  6.     IImageWrapperPtr imageWrapper = imageWrapperModule.CreateImageWrapper(EImageFormat::PNG);  
  7.   
  8.     TArray<uint8> OutArray;  
  9.     if(FFileHelper::LoadFileToArray(OutArray, *_TexPath))  
  10.     {  
  11.         if (imageWrapper.IsValid()&&  
  12.             imageWrapper->SetCompressed(OutArray.GetData(), OutArray.Num()))  
  13.         {  
  14.             const TArray<uint8>* uncompressedRGBA = NULL;  
  15.             if (imageWrapper->GetRaw(ERGBFormat::RGBA, 8, uncompressedRGBA))  
  16.             {  
  17.                 const TArray<FColor> uncompressedFColor=uint8ToFColor(*uncompressedRGBA);  
  18.                 OutTex=TextureFromImage(  
  19.                     imageWrapper->GetWidth(),  
  20.                     imageWrapper->GetHeight(),  
  21.                     uncompressedFColor,  
  22.                     true);  
  23.             }  
  24.         }  
  25.     }  
  26.     return OutTex;  
  27. }  
  28. #pragma endregion  
2.将uint8数组转为颜色数组

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma region 将uint8数组转为颜色数组  
  2. TArray<FColor> AMyProjectGameMode::uint8ToFColor(const TArray<uint8> origin)  
  3. {  
  4.     TArray<FColor> uncompressedFColor;  
  5.     uint8 auxOrigin;  
  6.     FColor auxDst;  
  7.   
  8.     for (int i = 0; i < origin.Num(); i++) {  
  9.         auxOrigin = origin[i];  
  10.         auxDst.R = auxOrigin;  
  11.         i++;  
  12.         auxOrigin = origin[i];  
  13.         auxDst.G = auxOrigin;  
  14.         i++;  
  15.         auxOrigin = origin[i];  
  16.         auxDst.B = auxOrigin;  
  17.         i++;  
  18.         auxOrigin = origin[i];  
  19.         auxDst.A = auxOrigin;  
  20.         uncompressedFColor.Add(auxDst);  
  21.     }  
  22.   
  23.     return  uncompressedFColor;  
  24.   
  25. }  
  26. #pragma endregion  
3.将颜色数组赋值给Texture
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma region 将颜色数组赋值给Texture  
  2. UTexture2D* AMyProjectGameMode::TextureFromImage(const int32 SrcWidth, const int32 SrcHeight, const TArray<FColor> &SrcData, const bool UseAlpha)   
  3. {  
  4.   
  5.     // 创建Texture2D纹理  
  6.     UTexture2D* MyScreenshot = UTexture2D::CreateTransient(SrcWidth, SrcHeight, PF_B8G8R8A8);  
  7.   
  8.     // 锁住他的数据,以便修改  
  9.     uint8* MipData = static_cast<uint8*>(MyScreenshot->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));  
  10.   
  11.     // 创建纹理数据  
  12.     uint8* DestPtr = NULL;  
  13.     const FColor* SrcPtr = NULL;  
  14.     for (int32 y = 0; y<SrcHeight; y++)  
  15.     {  
  16.         DestPtr = &MipData[(SrcHeight - 1 - y) * SrcWidth * sizeof(FColor)];  
  17.         SrcPtr = const_cast<FColor*>(&SrcData[(SrcHeight - 1 - y) * SrcWidth]);  
  18.         for (int32 x = 0; x<SrcWidth; x++)  
  19.         {  
  20.             *DestPtr++ = SrcPtr->B;  
  21.             *DestPtr++ = SrcPtr->G;  
  22.             *DestPtr++ = SrcPtr->R;  
  23.             if (UseAlpha)  
  24.             {  
  25.                 *DestPtr++ = SrcPtr->A;  
  26.             }  
  27.             else  
  28.             {  
  29.                 *DestPtr++ = 0xFF;  
  30.             }  
  31.             SrcPtr++;  
  32.         }  
  33.     }  
  34.   
  35.     // 解锁纹理  
  36.     MyScreenshot->PlatformData->Mips[0].BulkData.Unlock();  
  37.     MyScreenshot->UpdateResource();  
  38.   
  39.     return MyScreenshot;  
  40. }  
  41. #pragma endregion  
4.我这里建图片路径放在工程的相对路径下,调用GetLocalTexture函数,获取Texture2D

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void AMyProjectGameMode::BeginPlay()  
  2. {  
  3.     const FString _FilePath = FPaths::GameDir() + "video_logo.png";  
  4.     _UITex = GetLocalTexture(_FilePath);  
  5. }  
5.注意别忘了,需要添加两个头文件

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapper.h"  
  2. #include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapperModule.h"  
6.运行截图,我这里将获取的图片放在了UI界面上

7.我在读取jpg格式的图片的时候,颜色明显不对,读png的格式的时候,就完全正常,还未去寻找原因


本文转自:http://blog.csdn.net/qq992817263/article/details/52704475

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值