LoginSignup
1
2

More than 3 years have passed since last update.

NatDevice (旧NatCam)でWebカメラの映像をカメラ番号を指定して表示する

Last updated at Posted at 2020-08-23

NatDevice

NatDeviceを使ってカメラ番号を指定する方法が見つからなかったのでメモ

NatCamTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using NatSuite.Devices;
public class NatCamTest : MonoBehaviour
{
    [Header(@"UI")]
    public RawImage rawImage;
    public AspectRatioFitter aspectFitter;
    [SerializeField] private int camera_num = 0;


    async void Start () {
        // Find webcams
        var criterion = MediaDeviceQuery.Criteria.GenericCameraDevice;
        var query = new MediaDeviceQuery(criterion);
        for (int i = 1; i <= camera_num; i++)
            {
                query.Advance();
            }

        var device = query.currentDevice as ICameraDevice;
        Debug.Log(device);

        // Start the camera preview
        var previewTexture = await device.StartRunning();
        // Display the preview in our UI
        rawImage.texture = previewTexture;
        aspectFitter.aspectRatio = (float)previewTexture.width / previewTexture.height;
    }
    // Update is called once per frame
    void Update()
    {

    }
}

備考

  • 別々の2つのWebカメラにアクセス可能
  • NatDeviceを使ったらUnityのwebcamtextureより遅延が少なくなるかな?と思ったら,あんまり変わんなさそう.
    • ただのwebcamtextureのwrapperぽい?
    • テクスチャへのアクセスはしやすそう
    • Python使ってアクセスしたほうが遅延が少なく安定した( Unityのwebcamtextureが遅すぎる問題の解決策 )

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2