You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity开发Hololens 2时获取原始摄像头画面的技术问询

解决方案:Hololens 2 获取无全息图的原始摄像头流

核心方案对比与推荐

1. 优先推荐:Unity + Windows.Media.Capture API 直接访问摄像头

这是比Research Mode更轻量化的方案,无需开启特殊权限,适配普通部署场景:

  • 实现逻辑:在Unity中通过C#调用UWP原生的Windows.Media.Capture API,直接对接Hololens 2的硬件摄像头流,跳过Mixed Reality Capture的全息图合成环节。
  • 匹配需求点:
    • 可直接配置获取灰度流,无需额外颜色转换
    • 硬件支持稳定输出30fps以上帧率
    • 输出完全无全息图的原始传感器数据
  • 关键代码片段:
    private MediaCapture _mediaCapture;
    
    private async Task InitializeCameraAsync()
    {
        var captureSettings = new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview
        };
    
        _mediaCapture = new MediaCapture();
        await _mediaCapture.InitializeAsync(captureSettings);
    
        // 配置灰度格式输出
        var previewProps = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
        previewProps.Subtype = MediaEncodingSubtypes.Gray8; // 8位灰度格式
        await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewProps);
    
        // 注册帧回调
        var frameReader = await _mediaCapture.CreateFrameReaderAsync(previewProps);
        frameReader.FrameArrived += OnFrameArrived;
        await frameReader.StartAsync();
    }
    
    private void OnFrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
    {
        using (var frame = sender.TryAcquireLatestFrame())
        {
            if (frame == null) return;
    
            // 直接读取原始灰度帧数据
            var bitmap = frame.VideoMediaFrame.SoftwareBitmap;
            var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read);
            using (var reference = buffer.CreateReference())
            {
                byte[] frameData;
                ((IMemoryBufferByteAccess)reference).GetBuffer(out frameData, out _);
                // 调用OpenCV DLL处理帧
                YourCVLibrary.ProcessGrayFrame(frameData, bitmap.PixelWidth, bitmap.PixelHeight);
            }
        }
    }
    
    // 需实现IMemoryBufferByteAccess接口以直接访问内存
    [ComImport]
    [Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private unsafe interface IMemoryBufferByteAccess
    {
        void GetBuffer(out byte* buffer, out uint capacity);
    }
    
  • 权限配置:在Unity的UWP打包设置中,勾选Webcam权限,确保应用能访问硬件摄像头。

2. Research Mode 作为备选方案

如果需要鱼眼摄像头等特殊传感器数据,Research Mode依然可行,但需注意:

  • 需在Hololens设备开发者选项中开启Research Mode
  • 应用需声明researchMode权限
  • 可直接获取左右鱼眼摄像头的灰度流,帧率稳定在30fps以上

排除方案说明

  • Mixed Reality Capture:输出为叠加全息图后的合成画面,无法剥离全息层,完全不符合需求,直接排除。

性能优化提示

  • 直接配置灰度格式输出,避免在DLL中额外做颜色转换
  • 使用SoftwareBitmap的内存直接访问,减少帧数据拷贝开销
  • 在Unity中开启子线程处理帧数据,避免阻塞AR渲染主线程

内容的提问来源于stack exchange,提问作者CocoaMilka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 02:12:50