码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-05-17 10:10:29      阅读:738      评论:0      收藏:0      [点我收藏+]

标签:turn   dev   size   art   调用   style   大小   length   apply   

  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(); } } } //获取像素 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 ; }

 

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

标签:turn   dev   size   art   调用   style   大小   length   apply   

原文地址:http://www.cnblogs.com/Bright-King/p/6865700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!