OpenCamera.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class OpenCamera : MonoBehaviour
  7. {
  8. // UI 相关参数
  9. public RawImage rawImage;
  10. public Button button_TakePhoto;
  11. // 摄像机图片参数
  12. private WebCamTexture webCamTexture;
  13. // Use this for initialization
  14. void Start()
  15. {
  16. // 打开相机
  17. StartCoroutine("OpenCameras");
  18. // 按钮绑定点击事件
  19. // button_TakePhoto.onClick.AddListener(TakePhotoAndSaveImage_Button);
  20. }
  21. /// <summary>
  22. /// 使用协程打开相机函数
  23. /// </summary>
  24. /// <returns></returns>
  25. IEnumerator OpenCameras()
  26. {
  27. // 申请相机权限
  28. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  29. // 判断是否有相机权限
  30. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  31. {
  32. // 获取相机设备
  33. WebCamDevice[] webCamDevices = WebCamTexture.devices;
  34. // 判断是否有相机设别
  35. if (webCamDevices != null && webCamDevices.Length > 0)
  36. {
  37. // 把 0 号设备(移动端后置摄像头)名称赋值
  38. string webCamName = webCamDevices[0].name;
  39. // 设置相机渲染宽高,并运行相机
  40. webCamTexture = new WebCamTexture(webCamName, Screen.width, Screen.height);
  41. // webCamTexture.requestedFPS = 30;
  42. webCamTexture.Play();
  43. // 把获取的图像渲染到画布上
  44. rawImage.texture = webCamTexture;
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// 拍照保存函数的包装接口
  50. /// </summary>
  51. void TakePhotoAndSaveImage_Button()
  52. {
  53. // 调用拍照保存函数
  54. TakePhotoAndSaveImage(webCamTexture);
  55. }
  56. /// <summary>
  57. /// 保存图片的接口函数
  58. /// </summary>
  59. /// <param name="tex"></param>
  60. void TakePhotoAndSaveImage(WebCamTexture tex)
  61. {
  62. // 新建一个 Texture2D 来获取相机图片
  63. // 然后 把图片转成 JPG 格式的 bytes
  64. Texture2D texture2D = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, true);
  65. texture2D.SetPixels32(tex.GetPixels32());
  66. texture2D.Apply();
  67. byte[] imageBytes = texture2D.EncodeToJPG();
  68. // 判断图片 bytes 是否为空
  69. if (imageBytes != null && imageBytes.Length > 0)
  70. {
  71. // 判断Android 平台,进行对应路径设置
  72. string savePath;
  73. string platformPath = Application.streamingAssetsPath + "/MyTempPhotos";
  74. #if UNITY_ANDROID && !UNITY_EDITOR
  75. platformPath = "/sdcard/DCIM/MyTempPhotos";
  76. #endif
  77. // 如果文件夹不存在,就创建文件夹
  78. if (!Directory.Exists(platformPath))
  79. {
  80. Directory.CreateDirectory(platformPath);
  81. }
  82. // 保存图片
  83. savePath = platformPath + "/" + Time.deltaTime + ".jpg";
  84. File.WriteAllBytes(savePath, imageBytes);
  85. }
  86. }
  87. }