UE4C++读取图片
来源:互联网 发布:微信里面的淘宝链接 编辑:程序博客网 时间:2024/05/21 10:23
很久以前的东西都找不到作者了,如果看到的话请告诉我
.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "Kismet/BlueprintFunctionLibrary.h"#include "LoadTexture.generated.h"/** * */UCLASS()class AA_API ULoadTexture : public UBlueprintFunctionLibrary{GENERATED_BODY()public:UFUNCTION(BlueprintCallable, Category = "TextureFromDisk")static class UTexture2D* GetTexture2DFromDiskFile(const FString& FilePath);//载入图像UFUNCTION(Category = "ExtendedContent|File", BlueprintCallable)static UTexture2D* LoadTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight);};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "AA.h"#include "LoadTexture.h"#include "FileManagerGeneric.h"#include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapper.h" #include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapperModule.h" //#include "Kismet/KismetSystemLibrary.h"//UKismetSystemLibrary::QuitGame(this, nullptr, EQuitPreference::Quit);class UTexture2D* ULoadTexture::GetTexture2DFromDiskFile(const FString& FilePath){TArray<uint8> RawFileData;UTexture2D* MyTexture = NULL;if (FFileHelper::LoadFileToArray(RawFileData, *FilePath /*"<path to file>"*/)){IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));// Note: PNG format. Other formats are supportedIImageWrapperPtr 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 renderingMyTexture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);// Fill in the source data from the filevoid* TextureData = MyTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());MyTexture->PlatformData->Mips[0].BulkData.Unlock();// Update the rendering resource from data.MyTexture->UpdateResource();}}}return MyTexture;}static IImageWrapperPtr 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* ULoadTexture::LoadTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight){UTexture2D* Texture = nullptr;IsValid = false;if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*ImagePath)){return nullptr;}TArray<uint8> CompressedData;if (!FFileHelper::LoadFileToArray(CompressedData, *ImagePath)){return nullptr;}IImageWrapperPtr ImageWrapper = GetImageWrapperByExtention(ImagePath);if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num())){const TArray<uint8>* UncompressedRGBA = nullptr;if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA)){Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);if (Texture != nullptr){IsValid = true;OutWidth = ImageWrapper->GetWidth();OutHeight = ImageWrapper->GetHeight();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;}PS: .h出错的话要在Build.cs 里加上 "ImageWrapper"
2 0
- UE4C++读取图片
- UE4C++获得UMG控件
- ue4c++项目移植问题
- 图片读取
- 图片读取
- 读取图片
- 图片读取
- 文件读取(读取图片)
- UE4C++实现保存文件扩展
- 使用Snippets加快UE4C++开发
- opencv读取图片读取不成功
- UE4C++ 查找文件 移动文件 删除文件
- UE4C++之Interface的声明与使用
- UE4C++声明结构体和枚举体
- UE4C++ 设置UMG控件的Slot
- 读取图片路径,显示图片
- 读取图片并打印图片
- 读取数据库图片
- C++ 指针
- 用localStorage来存储数据
- 007-dockerbook中的命令总结(1)
- Android如何获取手机的屏幕宽高
- 创建一个空项目
- UE4C++读取图片
- 欢迎使用CSDN-markdown编辑器
- 关于火狐浏览器下<input type="password" />记住密码的那些事
- selenium之获取alert
- TextView设置为可复制
- SQL删除重复数据的方法
- BZOJ3511 土地划分-最小割
- Linux中常用操作命令
- “collect2: error: ld returned 1 exit status”问题解决!