UE4 C++ 动态读取图片
一、 原理介绍
UE4图片的加载过程先通过SetCompressed将二进制文件转化为BGRA的数据,然后通过GetRaw()函数提取到数组里面赋值给Texture2D,然后通过CreateTransient创建图片信息数据。最后填充PlatformData的Mip资源,加载图片。
二、代码如下
build.cs里添加模块"ImageWrapper"
.h文件
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include"Runtime/Core/Public/HAL/PlatformFilemanager.h"
#include"Runtime/Core/Public/Misc/FileHelper.h"
#include"Runtime/Core/Public/Misc/Paths.h"
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
#include"IImageWrapperModule.h"
#include"IImageWrapper.h"
#include"ImageUtils.h"
#include "MyBlueprintFunctionLibrary.generated.h"/*** */
UCLASS()
class TEST_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{GENERATED_BODY()public:UFUNCTION(BlueprintCallable, Category = "Picture|Tools")static class UTexture2D* LoadImageToTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight);UFUNCTION(BlueprintCallable, Category = "LoadLocalTexture")static TArray<FString> GetFolderFiles(FString path);};
.cpp文件
// Fill out your copyright notice in the Description page of Project Settings.#include "MyBlueprintFunctionLibrary.h"static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString InImagePath)
{IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));if (InImagePath.EndsWith(".png")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);}else if (InImagePath.EndsWith(".jpg") && InImagePath.EndsWith(".jpeg")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);}else if (InImagePath.EndsWith(".bmp")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);}else if (InImagePath.EndsWith(".ico")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);}else if (InImagePath.EndsWith("exr")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);}else if (InImagePath.EndsWith(".icns")){return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);}return nullptr;
}UTexture2D* UMyBlueprintFunctionLibrary::LoadImageToTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight)
{UTexture2D* Texture = nullptr;IsValid = false;//判断所查找的文件是否存在if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*ImagePath)){return nullptr;}//将文件的二进制格式存储到数组中TArray<uint8>RawFileData;if (!FFileHelper::LoadFileToArray(RawFileData, *ImagePath)){return nullptr;}//获取文件的格式IImageWrapperPtr ImageWrapper = GetImageWrapperByExtention(ImagePath);//把二进制数据转为纯粹的BGRA的数据格式if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num())){//跟格式无关的颜色数据TArray<uint8> UncompressedRGBA ;//把上一步的BGRA的数据格式通过GetRaw函数提取到一个数组里,然后再赋值给UTexture2Dif (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedRGBA)){//创建图片信息数据Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);if (Texture != nullptr){IsValid = true;//获取图片的大小OutWidth = ImageWrapper->GetWidth();OutHeight = ImageWrapper->GetHeight();//填充PlatformData的Mip资源void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);FMemory::Memcpy(TextureData, UncompressedRGBA.GetData(), UncompressedRGBA.Num());Texture->PlatformData->Mips[0].BulkData.Unlock();Texture->UpdateResource();}}}return Texture;
}TArray<FString> UMyBlueprintFunctionLibrary::GetFolderFiles( FString path)
{TArray<FString> files;FPaths::NormalizeDirectoryName(path);IFileManager& FileManager = IFileManager::Get();FString FinalPath = path / TEXT("*");FileManager.FindFiles(files, *FinalPath, true, true);return files;
}
三、创建UMG蓝图,拖入image
四、打开关卡蓝图
五、结果如下
UE4 C++ 动态读取图片相关推荐
- UE4之读取图片并纹理贴图显示
参考: 谢谢这位大哥,按照你的讲解,做了一个出来. https://www.bilibili.com/video/BV1UE411Z7uY?t=1496 步骤一: 添加模块引用 PrivateDepe ...
- 【小白学PyTorch】16.TF2读取图片的方法
<<小白学PyTorch>> 扩展之tensorflow2.0 | 15 TF2实现一个简单的服装分类任务 小白学PyTorch | 14 tensorboardX可视化教程 ...
- pytorch dataset_【小白学PyTorch】16.TF2读取图片的方法
<> 扩展之tensorflow2.0 | 15 TF2实现一个简单的服装分类任务 小白学PyTorch | 14 tensorboardX可视化教程 小白学PyTorch | 13 Ef ...
- 【专栏精选】实战:动态配置图片
本文节选自洪流学堂公众号技术专栏<大话Unity2019>,未经允许不可转载. 洪流学堂公众号回复专栏,查看更多专栏文章. 洪流学堂,让你快人几步.你好,我是郑洪智. 小新:"大 ...
- Qt 编程指南 8 显示静态小图片和动态大图片
显示控件概览 图片浏览示例 第一行是一个标签控件,objectName 为 labelShow,文本内容清空,因为是用来显示图片用的. 主界面第二行的控件是四个普通按钮 第一个按钮文本是 " ...
- ImageView中动态设置图片
关于ImageView中如何动态设置图片 我是从一个list中读取的字符串 动态的设置到ImageView中. mTreeList中存放的是R.drawable.icon_direct_bottom ...
- 【UE4 C++】读取灰度图/RGB图中的像素值生成自定义形状的模型分布
效果图 一.PS制作一张自定义形状的像素图 导出为PNG图片 二.UE4创建函数库读取图片像素颜色 1.新建C++类 2.头文件中添加函数 public:UFUNCTION(BlueprintCall ...
- OpenCv中批量读取图片数据并显示出来的C++程序
/*************************************************************************************************** ...
- gif演示python循环_用Python将动态GIF图片倒放播放的方法
这次让我们一个用 Python 做一个小工具:将动态 GIF 图片倒序播放! GIF(Graphics Interchange Format) 是一种可以用来呈现动画效果的图片格式,原理就是保存很多帧 ...
最新文章
- php,http_build_query,乱码
- python多线程内存越要越大_Python 面试:这9个问题你一定要掌握!
- 结构体重定义冲突_有意减脂、调整饮食,体重却增加了?
- 机器学习笔记(十)降维和度量学习
- Jquery中获取表单提交时不确认个数元素的个数与值的方法
- 前端学习(11):标题和段落
- 求二进制数中1的个数(转)
- html5鼠标讯听,HTML5 随鼠标移动而变化的低频信号发生器
- 一本程序员职场的百科全书
- phpexcel 导入导出
- 技术人 | 浅谈如何成为技术一号位?
- VM16 安装win 11
- 什么是DTC?为什么国内外如此火爆
- 欧美html游戏安卓,HTML5 Games - Rated M or for 18+ only
- 网易杭研易盾实习心得
- Freetype的基本应用
- pixhawk使用WiFi模块
- Debian系统下安装Aria2
- 中国棋手芈昱廷战胜日本围棋机器人DeepZenGo
- 一文看懂yolov7;yolov7详解