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

相关文章

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#中图片如何自适应pictureBox大小

《C#中图片如何自适应pictureBox大小》文章描述了如何在C#中实现图片自适应pictureBox大小,并展示修改前后的效果,修改步骤包括两步,作者分享了个人经验,希望对大家有所帮助... 目录C#图片自适应pictureBox大小编程修改步骤总结C#图片自适应pictureBox大小上图中“z轴

使用Python将长图片分割为若干张小图片

《使用Python将长图片分割为若干张小图片》这篇文章主要为大家详细介绍了如何使用Python将长图片分割为若干张小图片,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果1. Python需求

使用 Python 和 LabelMe 实现图片验证码的自动标注功能

《使用Python和LabelMe实现图片验证码的自动标注功能》文章介绍了如何使用Python和LabelMe自动标注图片验证码,主要步骤包括图像预处理、OCR识别和生成标注文件,通过结合Pa... 目录使用 python 和 LabelMe 实现图片验证码的自动标注环境准备必备工具安装依赖实现自动标注核心

Java操作xls替换文本或图片的功能实现

《Java操作xls替换文本或图片的功能实现》这篇文章主要给大家介绍了关于Java操作xls替换文本或图片功能实现的相关资料,文中通过示例代码讲解了文件上传、文件处理和Excel文件生成,需要的朋友可... 目录准备xls模板文件:template.xls准备需要替换的图片和数据功能实现包声明与导入类声明与

基于C#实现将图片转换为PDF文档

《基于C#实现将图片转换为PDF文档》将图片(JPG、PNG)转换为PDF文件可以帮助我们更好地保存和分享图片,所以本文将介绍如何使用C#将JPG/PNG图片转换为PDF文档,需要的可以参考下... 目录介绍C# 将单张图片转换为PDF文档C# 将多张图片转换到一个PDF文档介绍将图片(JPG、PNG)转

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

Spring MVC 图片上传

引入需要的包 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-