Unity3D使用C#脚本调用外置摄像头拍摄照片,并转化成byte数组

  TakePhoto.cs

  IEnumerator Start() 
  {
        
        //获取摄像头
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if(Application.HasUserAuthorization(UserAuthorization.WebCam))
        {

            WebCamDevice[] devices = WebCamTexture.devices;
            if(devices !=null)
            {
          //获取设备名称 deviceName
= devices[0].name; tex = new WebCamTexture(deviceName,400,300,12); tex.Play();//建议在需要拍摄照片的时候开启摄像头,不拍摄时将其关闭掉,这里可以使用tex.Pause()暂停摄像头,tex.Stop()关闭摄像头 } } } //获取像素 private byte[] GetPhotoPixel(WebCamTexture ca) { Texture2D texture = new Texture2D (ca.width,ca.height); int y = 0; while (y < texture.height) { int x = 0; while (x < texture.width) { UnityEngine.Color color = ca.GetPixel(x,y); texture.SetPixel(x,y,color); ++x; } ++y; } texture.Apply (); // texture.name = name ; byte[] pngData = GetJpgData (texture); return pngData ; } //控制照片大小 private byte[] GetJpgData(Texture2D te) { byte[] data= null ; int quelity = 75 ; while(quelity > 20) { data = te.EncodeToJPG(quelity); int size = data.Length/1024; if( size > 30 ) { quelity -=5; } else { break ; } } return data ; }

 

转载于:https://www.cnblogs.com/Bright-King/p/6865700.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值