开启外部摄像头

在unity中建立一个image作为摄像头显示画面,然后通过命令render到image上即可。

public WebCamTexture webTex;  
    public string deviceName;  
      
    void Start()  
    {  
          StartCoroutine(CallCamera())
    }  
    void Update()  
    {  
          
    }  ///  
///调用摄像头  
///  
    IEnumerator CallCamera()    
    {  
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);    
        if(Application.HasUserAuthorization(UserAuthorization.WebCam))    
        {    
            WebCamDevice[] devices = WebCamTexture.devices;        
            deviceName = devices[0].name;    
            //设置摄像机摄像的区域    
            webTex=new WebCamTexture(deviceName,10,10,10); 
            GetComponent<MeshRenderer>().material.mainTexture = webTex;     
            webTex.Play();//开始摄像    
        }    
    }

抓取当前画面

public void Capture()
    {
        //WebCamTexture wc = new WebCamTexture(160, 120);
        //wc.play();
        Texture2D t = new Texture2D(webTex.width, webTex.height);
        t.SetPixels(webTex.GetPixels());
        t.Apply();
        byte[] byt = t.EncodeToPNG();
        File.WriteAllBytes(@"D:/test.jpg", byt);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值