Getting raw pixel data from UARTexture

We retrieve an UARTexture from the blueprint function “GetARTexture()” of the ARBlueprintLibrary (having “Camera Image” selected).

From this UARTexture, we want to retrieve the raw pixel data, but until now we don’t have any success.

We found a couple solutions, that were based on UTexture2D - like for example: UE4 - Reading the pixels from a UTexture2D - Isara Tech. , but UARTexture only derives from UTexture and not from UTexture2D.

The only one that we found which should work for UTexture and therefor for UARTexture as well, uses the editor-only property “Source” and therefor also doesn’t work:

Best Regards,

Michael

you can see what we done in different platform.i have search for a month.
it will return the unit8* raw data.

It seems the code below works for me.


UsingGetTexturePixels

1 Like

It works, Thanks.
But some minor changed should be applied in the code.
In my case, Texture2D->GetSizeXY() return 1*1, so I change to ARTexture->Size.XY and it works.
Here’s my code:

void UMyARLib::GetARTextureData(UARTexture* ARTexture)
{
	FTexture2DRHIRef Texture2D = ARTexture->GetResource()->TextureRHI->GetTexture2D();
	if(!Texture2D)
	{
		UE_LOG(LogTemp, Warning, TEXT("Texture2D failed"));
		return;
	}

	TArray<FColor> OutPixels;
	struct FReadSurfaceContext
	{
		FTexture2DRHIRef Texture;
		TArray<FColor>* OutData;
		FIntRect Rect;
		FReadSurfaceDataFlags Flags;
	};

	FReadSurfaceContext ReadSurfaceContext =
	{
		Texture2D,
		&OutPixels,
		FIntRect(0, 0, ARTexture->Size.X, ARTexture->Size.Y),
		FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)
	};

	FReadSurfaceContext Context = ReadSurfaceContext;
	ENQUEUE_RENDER_COMMAND(ReadSurfaceCommand)(
		[Context](FRHICommandListImmediate& RHICmdList)
		{
			RHICmdList.ReadSurfaceData(
				Context.Texture,
				Context.Rect,
				*Context.OutData,
				Context.Flags);
		});
	FlushRenderingCommands();

	//Save to file
	TArray64<uint8> ImgData;
	FImageUtils::PNGCompressImageArray(Context.Rect.Width(), Context.Rect.Height(), OutPixels, ImgData);
	FString FileName = FPaths::ProjectSavedDir() + "test.png";
	FFileHelper::SaveArrayToFile(ImgData, *FileName);
	
}