示例#1
0
    public unsafe void GetScreenShot()
    {
        if (!cameraManager.TryGetLatestImage(out image))
        {
            return;
        }
        var format = TextureFormat.RGBA32;

        Texture2D texture = new Texture2D(image.width, image.height, format, false);

        var conversionParams = new XRCameraImageConversionParams {
            inputRect        = new RectInt(0, 0, image.width, image.height),
            outputDimensions = new Vector2Int(image.width, image.height),
            outputFormat     = TextureFormat.RGBA32,
            transformation   = CameraImageTransformation.MirrorY
        };

        var rawTextureData = texture.GetRawTextureData <byte>();

        try
        {
            IntPtr ptr = new IntPtr(rawTextureData.GetUnsafePtr());
            image.Convert(conversionParams, ptr, rawTextureData.Length);
        }
        finally
        {
            // We must dispose of the XRCameraImage after we're finished
            // with it to avoid leaking native resources.
            image.Dispose();
        }
        // Apply the updated texture data to our texture
        texture.Apply();

        // Set the RawImage's texture so we can visualize it
        float ratio = (float)texture.height / texture.width;

        if (Fit != null)
        {
            Fit.aspectRatio = 1f / ratio;
            Fit.aspectMode  = AspectRatioFitter.AspectMode.EnvelopeParent;
        }
        if (background != null)
        {
            background.texture = texture;
            background.rectTransform.localEulerAngles = new Vector3(0, 0, 90);
            background.rectTransform.localScale       = new Vector3(ratio, ratio, 1f);
            background.enabled = true;
        }
        StartCoroutine(SaveTexture(texture));
    }
        public static void GetPlaneDataRGB(out byte[] pixels, XRCameraImage image)
        {
            var conversionParams = new XRCameraImageConversionParams
            {
                inputRect        = new RectInt(0, 0, image.width, image.height),
                outputDimensions = new Vector2Int(image.width, image.height),
                outputFormat     = TextureFormat.RGB24,
                transformation   = CameraImageTransformation.None
            };

            int size = image.GetConvertedDataSize(conversionParams);

            pixels = new byte[size];
            GCHandle bufferHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);

            image.Convert(conversionParams, bufferHandle.AddrOfPinnedObject(), pixels.Length);
            bufferHandle.Free();
        }