Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2617937
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Android平台

2016-01-28 15:46:03

     之前也是找了一些网上的资料,不是很满意,而且有都比较死,不能根据控件大小自动缩放,这样的话就很有局限性;如果控件自适应屏幕了,但是你的摄像头区域不自适应控件,那么就会出现水印模糊的情况,这里我顺便创建摄像头的时候就根据控件大小创建的,这样就可以自适应控件了。

    后续还可以加其他的功能,什么区域裁剪之类的。。。有更好的方式记得给我留言,谢谢。
    直接脚本(打包apk运行):
        

点击(此处)折叠或打开

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization;
  5. using System.Runtime .Serialization.Formatters.Binary;
  6. using System.Threading;

  7. /******
  8.  * des: 摄像头工具类
  9.  * author: hl
  10.  * date: 2016-01-28
  11.  * function:
  12.  * 1. 根据plane的大小设置摄像头的区域;
  13.  * 2. 截屏、录屏区域和上诉相同,后续考虑加入区域裁剪处理
  14.  * 3. 内部容错挺多,当然根据使用情况可以精简
  15.  */
  16. public class takePhoto : MonoBehaviour
  17. {
  18.     ///
  19.     /// 设备名称 - 暂时无用
  20.     ///
  21.     public string deviceName;
  22.     ///
  23.     /// 接收返回的图片数据
  24.     ///
  25.     private WebCamTexture tex;
  26.     ///
  27.     /// 预览区域宽高 - 不用Rect的原因:来回强转容易丢失精度
  28.     ///
  29.     private int wtHeight, wtWeight;
  30.     private int wtStartx, wtStarty;
  31.     ///
  32.     /// 是否正在录屏
  33.     ///
  34.     private bool bIsSeriousCapture = false;
  35.     ///
  36.     /// plane对象,承载显示摄像头图片,注意旋转角度
  37.     ///
  38.     public Transform planeT; ///< Rotation: x 90 y 180 z 0
  39.     ///
  40.     /// 存档路径 - test
  41.     ///
  42.     private string filePath = "/sdcard/";

  43.     void Start()
  44.     {
  45.         if (Application.platform == RuntimePlatform.Android)
  46.         {
  47.             filePath = "/sdcard/";
  48.         }
  49.         else
  50.         {
  51.             filePath = "C:/Users/Administrator/Desktop/";
  52.         }
  53.     }

  54.     void OnGUI()
  55.     {
  56.         if (GUI.Button(new Rect(10, 20, 100, 40), "开启摄像头"))
  57.         {
  58.             ///< 调用摄像头
  59.             startCamara();
  60.         }

  61.         if (GUI.Button(new Rect(10, 170, 100, 40), "停止/拍照"))
  62.         {
  63.             ///< 停止捕获镜头
  64.             stopCamara();
  65.         }

  66.         if(GUI.Button(new Rect(10,70,100,40),"捕获照片"))
  67.         {
  68.             ///< 捕获照片
  69.             capturePicJpg(filePath + "test.jpg");
  70.         }

  71.         if (GUI.Button(new Rect(10, 120, 100, 40), "录像"))
  72.         {
  73.             ///< 录像
  74.             seriousPicPng(filePath);
  75.         }
  76.     }

  77.     ///
  78.     /// 开启摄像头
  79.     ///
  80.     public void startCamara()
  81.     {
  82.         if (null == planeT)
  83.         {
  84.             return;
  85.         }
  86.         if (null != tex)
  87.         {
  88.             if (!tex.isPlaying)
  89.             {
  90.                 tex.Play();
  91.             }
  92.             return;
  93.         }
  94.         StartCoroutine(start());
  95.     }

  96.     ///
  97.     /// 关闭摄像头
  98.     ///
  99.     public void stopCamara()
  100.     {
  101.         ///< 如果在录屏,停止录屏操作
  102.         bIsSeriousCapture = false;
  103.         if (null == tex)
  104.         {
  105.             StopAllCoroutines();
  106.             return;
  107.         }
  108.         tex.Stop();
  109.         StopAllCoroutines();
  110.         tex = null;
  111.     }

  112.     ///
  113.     /// 摄像头停下了就相当于拍照结束 - 此时可以直接获取Texture...
  114.     ///
  115.     ///
  116.     public Texture getFinalTexture()
  117.     {
  118.         return planeT.renderer.material.mainTexture;
  119.     }

  120.     ///
  121.     /// 拍照JPG
  122.     ///
  123.     ///
  124.     public void capturePicJpg(string _filePath)
  125.     {
  126.         ///< 正在录屏,直接返回
  127.         if (bIsSeriousCapture || null == tex)
  128.         {
  129.             return;
  130.         }
  131.         if (null != tex && !tex.isPlaying)
  132.         {
  133.             return;
  134.         }
  135.         tex.Pause();
  136.         StartCoroutine(getTexture(_filePath, 0));
  137.     }

  138.     ///
  139.     /// 拍照PNG
  140.     ///
  141.     ///
  142.     public void capturePicPng(string _filePath)
  143.     {
  144.         ///< 正在录屏,直接返回
  145.         if (bIsSeriousCapture || null == tex)
  146.         {
  147.             return;
  148.         }
  149.         if (null != tex && !tex.isPlaying)
  150.         {
  151.             return;
  152.         }
  153.         tex.Pause();
  154.         StartCoroutine(getTexture(_filePath, 1));
  155.     }

  156.     ///
  157.     /// 连续拍照Jpg
  158.     ///
  159.     public void seriousPicJpg(string _folder)
  160.     {
  161.         ///< 正在录屏,直接返回
  162.         if (null == tex)
  163.         {
  164.             return;
  165.         }
  166.         if (null != tex && !tex.isPlaying)
  167.         {
  168.             return;
  169.         }
  170.         bIsSeriousCapture = true;
  171.         StartCoroutine(SeriousPhotoes(_folder, 0));
  172.     }

  173.     ///
  174.     /// 连续拍照Png
  175.     ///
  176.     public void seriousPicPng(string _folder)
  177.     {
  178.         if (null == tex)
  179.         {
  180.             return;
  181.         }
  182.         if (null != tex && !tex.isPlaying)
  183.         {
  184.             return;
  185.         }
  186.         bIsSeriousCapture = true;
  187.         StartCoroutine(SeriousPhotoes(_folder, 1));
  188.     }

  189.     ///*******************辅助协程接口***********************************///

  190.     ///
  191.     /// 启动摄像头
  192.     ///
  193.     private IEnumerator start()
  194.     {
  195.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  196.         
  197.         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  198.         {
  199.             WebCamDevice[] devices = WebCamTexture.devices;
  200.             if (devices.Length > 0)
  201.             {
  202.                 deviceName = devices[0].name;
  203.                 ///< 根据Plane设置摄像头
  204.                 Bounds bds = new Bounds();
  205.                 bds.min = Camera.main.WorldToScreenPoint(planeT.gameObject.renderer.bounds.min);
  206.                 bds.max = Camera.main.WorldToScreenPoint(planeT.gameObject.renderer.bounds.max);
  207.                 wtWeight = (int)Mathf.Abs(bds.max.x - bds.min.x);
  208.                 wtHeight = (int)Mathf.Abs(bds.max.y - bds.min.y);
  209.                 wtStartx = (int)bds.min.x;
  210.                 wtStarty = (int)bds.min.y;

  211.                 tex = new WebCamTexture(deviceName,
  212.                                         wtWeight, wtHeight, 12);
  213.                 planeT.renderer.material.mainTexture = tex;
  214.                 tex.Play();
  215.             }
  216.         }
  217.     }

  218.     ///
  219.     /// 获取截图
  220.     ///
  221.     /// The texture.
  222.     private IEnumerator getTexture(string _filePath, byte _type)
  223.     {
  224.         yield return new WaitForEndOfFrame();

  225.         Texture2D t = new Texture2D(wtWeight, wtHeight);
  226.         t.ReadPixels(new Rect(wtStartx, wtStarty, wtWeight, wtHeight), 0, 0, false);
  227.         t.Apply();
  228.         if (0 == _type)
  229.         {
  230.             byte[] byt = t.EncodeToJPG();
  231.             File.WriteAllBytes(_filePath, byt);
  232.         }
  233.         if (1 == _type)
  234.         {
  235.             byte[] byt = t.EncodeToPNG();
  236.             File.WriteAllBytes(_filePath, byt);
  237.         }
  238.         tex.Play();
  239.     }

  240.     ///
  241.     /// 连续捕获照片
  242.     ///
  243.     /// The photoes.
  244.     private IEnumerator SeriousPhotoes(string _folder, byte _type)
  245.     {
  246.         while (bIsSeriousCapture)
  247.         {
  248.             yield return new WaitForEndOfFrame();
  249.             
  250.             Texture2D t = new Texture2D(wtWeight, wtHeight, TextureFormat.RGB24, true);
  251.             t.ReadPixels(new Rect(wtStartx, wtStarty, Screen.width - 200, Screen.height), 0, 0, false);
  252.             t.Apply();
  253.             if (0 == _type)
  254.             {
  255.                 byte[] byt = t.EncodeToJPG();
  256.                 File.WriteAllBytes(_folder + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".jpg", byt);
  257.             }
  258.             if (1 == _type)
  259.             {
  260.                 byte[] byt = t.EncodeToPNG();
  261.                 File.WriteAllBytes(_folder + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".png", byt);
  262.             }
  263.             Thread.Sleep(300);
  264.         }
  265.     }
  266. }

阅读(10726) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~