版本: Unity 6 (6000.0)
语言英语
  • C#

PhotoCaptureFrame.CopyRawImageDataIntoBuffer

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每项建议,并在必要时进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请 <a>稍后再试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public void CopyRawImageDataIntoBuffer(List<byte> byteBuffer);

参数

byteBuffer 将捕获的原始图像数据复制到的目标字节列表。

描述

将原始 IMFMediaBuffer 图像数据复制到字节列表中。

如果您希望在外部插件或其他线程上对字节数据进行自己的图像处理,您可能希望将原始 IMFMediaBuffer 数据复制到自己的字节列表中。

有关 WinRT IMFMediaBuffer 对象的更多信息,请访问 https://msdn.microsoft.com/en-us/library/windows/desktop/ms696261(v=vs.85).aspx

此示例将从网络摄像头捕获图像,并将图像数据从原始 IMFMediaBuffer 对象手动复制到纹理中,并在游戏对象上显示。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Windows.WebCam;

public class PhotoCaptureRawImageExample : MonoBehaviour { PhotoCapture photoCaptureObject = null; Texture2D targetTexture = null; Renderer quadRenderer = null;

// Use this for initialization void Start() { Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();

targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height, TextureFormat.RGBA32, false);

PhotoCapture.CreateAsync(false, delegate(PhotoCapture captureObject) { photoCaptureObject = captureObject;

CameraParameters c = new CameraParameters(); c.cameraResolutionWidth = targetTexture.width; c.cameraResolutionHeight = targetTexture.height; c.pixelFormat = CapturePixelFormat.BGRA32;

captureObject.StartPhotoModeAsync(c, delegate(PhotoCapture.PhotoCaptureResult result) { photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); }); }); }

void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) { List<byte> imageBufferList = new List<byte>(); // Copy the raw IMFMediaBuffer data into our empty byte list. photoCaptureFrame.CopyRawImageDataIntoBuffer(imageBufferList);

// In this example, we captured the image using the BGRA32 format. // So our stride will be 4 since we have a byte for each rgba channel. // The raw image data will also be flipped so we access our pixel data // in the reverse order. int stride = 4; float denominator = 1.0f / 255.0f; List<Color> colorArray = new List<Color>(); for (int i = imageBufferList.Count - 1; i >= 0; i -= stride) { float a = (int)(imageBufferList[i - 0]) * denominator; float r = (int)(imageBufferList[i - 1]) * denominator; float g = (int)(imageBufferList[i - 2]) * denominator; float b = (int)(imageBufferList[i - 3]) * denominator;

colorArray.Add(new Color(r, g, b, a)); }

targetTexture.SetPixels(colorArray.ToArray()); targetTexture.Apply();

if (quadRenderer == null) { GameObject p = GameObject.CreatePrimitive(PrimitiveType.Quad); quadRenderer = p.GetComponent<Renderer>() as Renderer; quadRenderer.material = new Material(Shader.Find("Custom/Unlit/UnlitTexture"));

p.transform.parent = this.transform; p.transform.localPosition = new Vector3(0.0f, 0.0f, 1.0f); }

quadRenderer.material.SetTexture("_MainTex", targetTexture);

// Take another photo photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); } }