Skip to content

Instantly share code, notes, and snippets.

@icywind
Last active January 21, 2021 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icywind/c72b4236c1a27877d81ae1016b3f8ae3 to your computer and use it in GitHub Desktop.
Save icywind/c72b4236c1a27877d81ae1016b3f8ae3 to your computer and use it in GitHub Desktop.
Interface for Agora Video Chat clients
// Get Image from the AR Camera, extract the raw data from the image
private unsafe void CaptureARBuffer()
{
// Get the image in the ARSubsystemManager.cameraFrameReceived callback
XRCameraImage image;
if (!cameraManager.TryGetLatestImage(out image))
{
Debug.LogWarning("Capture AR Buffer returns nothing!!!!!!");
return;
}
var conversionParams = new XRCameraImageConversionParams
{
// Get the full image
inputRect = new RectInt(0, 0, image.width, image.height),
// Downsample by 2
outputDimensions = new Vector2Int(image.width, image.height),
// Color image format
outputFormat = ConvertFormat,
// Flip across the x axis
transformation = CameraImageTransformation.MirrorX
// Call ProcessImage when the async operation completes
};
// See how many bytes we need to store the final image.
int size = image.GetConvertedDataSize(conversionParams);
Debug.Log("OnCameraFrameReceived, size == " + size + "w:" + image.width + " h:" + image.height + " planes=" + image.planeCount);
// Allocate a buffer to store the image
var buffer = new NativeArray<byte>(size, Allocator.Temp);
// Extract the image data
image.Convert(conversionParams, new System.IntPtr(buffer.GetUnsafePtr()), buffer.Length);
// The image was converted to RGBA32 format and written into the provided buffer
// so we can dispose of the CameraImage. We must do this or it will leak resources.
byte[] bytes = buffer.ToArray();
monoProxy.StartCoroutine(PushFrame(bytes, image.width, image.height,
() => { image.Dispose(); buffer.Dispose(); }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment