UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片

本文主要是介绍UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一,写带通道的RGBA图片(BMP)

1,写BMP图片的代码:

2,获取图片buffer,并写成代码

二,使用UE提供的接口写png图片

1, FTextureResource写成图片

2,UTextureRenderTarget2D写成图片

一,写带通道的RGBA图片(BMP)

1,写BMP图片的代码:

#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include <windows.h>
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"inline LONG BmpPitchCB(const BITMAPINFOHEADER* pFormat)
{return ((pFormat->biWidth * (pFormat->biBitCount >> 3) + 3) >> 2) << 2;
}inline HRESULT SaveBmp(LPCTSTR			pFileName,const void* pBuf,LONG			lBuf_W,LONG			lBuf_H,LONG			lBuf_Bit,BOOL			bAlign)
{if (pBuf == NULL){return E_INVALIDARG;}BITMAPFILEHEADER bfh;BITMAPINFOHEADER bih;memset(&bih, 0, sizeof(bih));bih.biSize = sizeof(bih);bih.biPlanes = 1;bih.biBitCount = (WORD)lBuf_Bit;bih.biWidth = lBuf_W;bih.biHeight = lBuf_H;bih.biSizeImage = BmpPitchCB(&bih) * abs(bih.biHeight);memset(&bfh, 0, sizeof(bfh));bfh.bfType = ((WORD)('M' << 8) | 'B');bfh.bfOffBits = 54;bfh.bfSize = 54 + bih.biSizeImage;// Correct the param// if (bAlign == false){if (bih.biSizeImage == (DWORD)bih.biWidth * bih.biBitCount * abs(bih.biHeight) / 8){bAlign = true;}}//HANDLE hFile = NULL;do{hFile = CreateFile(pFileName,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if (hFile == NULL || hFile == INVALID_HANDLE_VALUE){hFile = NULL;break;}DWORD dwWrited = 0;if (WriteFile(hFile, &bfh, sizeof(bfh), &dwWrited, NULL) == false){break;}if (WriteFile(hFile, &bih, sizeof(bih), &dwWrited, NULL) == false){break;}if (bAlign){if (WriteFile(hFile, pBuf, bih.biSizeImage, &dwWrited, NULL) == false){break;}}else{// bitmap format pitch// ...................................xxx for 32 bit aligned//const BYTE* pRow = static_cast<const BYTE*>(pBuf);const LONG   nRow = bih.biSizeImage / abs(bih.biHeight);LONG n = 0;for (n = abs(bih.biHeight); n > 1; n--){if (!WriteFile(hFile, pRow, nRow, &dwWrited, NULL)){break;}pRow += bih.biWidth * bih.biBitCount / 8;}if (n != 1){break;}if (!WriteFile(hFile, pRow, bih.biWidth * bih.biBitCount / 8, &dwWrited, NULL)){break;}LONG nPlus = nRow - bih.biWidth * bih.biBitCount / 8;if (nPlus > 0){if (!WriteFile(hFile, pRow, nPlus, &dwWrited, NULL)){break;}}}CloseHandle(hFile);return S_OK;} while (false);if (hFile){CloseHandle(hFile);}return E_FAIL;
}

2,获取图片buffer,并写成代码

UTextureRenderTarget2D* MediaRenderTarget; 				
FTextureResource* DstTextureRes = MediaRenderTarget->GetResource();
FTextureRHIRef DstTextureRef = DstTextureRes->TextureRHI;
FRHITexture* DstTexture = DstTextureRef->GetTexture2D();
//FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();
FIntRect RectTarget(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());
TArray<FColor> DataTarget;
ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImageTarget)([Texture = DstTexture, RectTarget, Data = &DataTarget](FRHICommandListImmediate& RHICmdList) mutable{RHICmdList.ReadSurfaceData(Texture, RectTarget, *Data, FReadSurfaceDataFlags(RCM_UNorm));});if (DataTarget.Num() == RectTarget.Area()){uint8* tempP = new uint8[RectTarget.Width() * RectTarget.Height() * 4];for (int32 i = 0; i < RectTarget.Width() * RectTarget.Height(); i++){tempP[i * 4] = DataTarget[i].B;tempP[i * 4 + 1] = DataTarget[i].G;tempP[i * 4 + 2] = DataTarget[i].R;tempP[i * 4 + 3] = DataTarget[i].A;}SaveBmp(TEXT("D://RHIUpdateTextureReference.bmp"), tempP, RectTarget.Width(), RectTarget.Height(), 32, true);}

二,使用UE提供的接口写png图片

1, FTextureResource写成图片

FTextureResource* rhiSource;	
FRHITexture* DstTexture = rhiSource->GetTexture2DRHI();
//FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();
FIntRect Rect(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());
FString FilePath = TEXT("D://rhiSource.png");
TArray<FColor> Data;
ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImage)([Texture = DstTexture, Rect, Data = &Data](FRHICommandListImmediate& RHICmdList) mutable
{RHICmdList.ReadSurfaceData(Texture, Rect, *Data, FReadSurfaceDataFlags(RCM_UNorm));});
if (Data.Num() == Rect.Area())
{TArray<uint8> Bitmap;FImageUtils::ThumbnailCompressImageArray(Rect.Width(), Rect.Height(), Data, Bitmap);FFileHelper::SaveArrayToFile(Bitmap, *FilePath);
}

2,UTextureRenderTarget2D写成图片

	UTextureRenderTarget2D* CapturingRenderTarget = MediaRenderTarget;if (CapturingRenderTarget){FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();FIntRect Rect(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());TArray<FColor> Data;ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImage)([Texture = DstTexture, Rect, Data = &Data](FRHICommandListImmediate& RHICmdList) mutable{RHICmdList.ReadSurfaceData(Texture, Rect, *Data, FReadSurfaceDataFlags(RCM_UNorm));});FlushRenderingCommands();if (Data.Num() == Rect.Area()){TArray<uint8> Bitmap;FImageUtils::ThumbnailCompressImageArray(Rect.Width(), Rect.Height(), Data, Bitmap);FFileHelper::SaveArrayToFile(Bitmap, *FilePath);}}

这篇关于UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979868

相关文章

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo