Test.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ZIM;
  6. using ZIM.Unity;
  7. public class Test : MonoBehaviour
  8. {
  9. public RawImage rawImage;
  10. public RawImage rawImage1;
  11. public RawImage rawImage2;
  12. public RawImage rawImage3;
  13. private WebCamTexture _webCamTexture;
  14. private Texture2D mUVCTexture2DTemp;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. CreateTextureIfNeeded(rawImage.texture);
  19. rawImage1.texture = m_SourceTexture.zimAutoLight(10) ;
  20. if (m_SourceTexture != null)
  21. {
  22. var pixelData = m_TargetTexture.GetPixelData<Color32>(0);
  23. var pixelDataSource = m_SourceTexture.GetPixelData<Color32>(0);
  24. //var pixelDataSource = m_SourceTexture.GetPixels();
  25. var offset = 0;
  26. for (var y = 0; y < m_SourceTexture.width; ++y)
  27. {
  28. for (var x = 0; x < m_SourceTexture.height; ++x, ++offset)
  29. {
  30. // Debug.Log(pixelDataSource[offset]);
  31. pixelData[offset] = pixelDataSource[offset];
  32. }
  33. }
  34. m_TargetTexture.Apply();
  35. }
  36. }
  37. public void OnClick_Open()
  38. {
  39. if (_webCamTexture != null)
  40. {
  41. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  42. return;
  43. }
  44. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  45. {
  46. WebCamDevice[] devices = WebCamTexture.devices;
  47. for (int i = 0; i < devices.Length; i++)
  48. {
  49. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  50. // this.logText.text += "devices[" + i + "].name:" + devices[i].name + "\n";
  51. }
  52. if (devices.Length == 0)
  53. {
  54. Debug.LogError("开启失败,没找到可用的摄像头!");
  55. return;
  56. }
  57. string deviceName = devices[0].name;
  58. _webCamTexture = new WebCamTexture(deviceName, 1280, 720, 30);
  59. _webCamTexture.Play();
  60. if (rawImage) rawImage.texture = _webCamTexture;
  61. Debug.Log("成功开启摄像头 " + deviceName);
  62. }
  63. else
  64. {
  65. Debug.LogError("开启失败,用户未授予摄像头权限!");
  66. }
  67. }
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. if (_webCamTexture && _webCamTexture.didUpdateThisFrame)
  72. {
  73. // CreateExternalTextureIfNeeded(rawImage.texture);
  74. //SaveScreenShot(rawImage.texture);
  75. if (texture2D != null)
  76. Destroy(texture2D);
  77. texture2D = TextureToTexture2D2(rawImage.texture);
  78. rawImage1.texture = texture2D;
  79. //var pixels = texture2D.GetPixels();
  80. //Debug.Log(pixels.Length);
  81. }
  82. if (Input.GetKeyDown(KeyCode.Space) && texture2D !=null)
  83. {
  84. Debug.Log("Input.GetKeyDown(KeyCode.Space)");
  85. //SaveScreenShot(rawImage.texture);
  86. if (rawImage3.texture != null)
  87. DestroyImmediate(rawImage3.texture);
  88. rawImage3.texture = texture2D.zimAutoLight(10);
  89. }
  90. }
  91. private Texture2D externalTexture;
  92. public void CreateExternalTextureIfNeeded(Texture texture)
  93. {
  94. if (externalTexture != null)
  95. DestroyImmediate(externalTexture);
  96. externalTexture = Texture2D.CreateExternalTexture(rawImage.texture.width, rawImage.texture.height, TextureFormat.ARGB32, false, true, rawImage.texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
  97. rawImage1.texture = externalTexture;
  98. }
  99. private Texture2D texture2D;
  100. private Texture2D TextureToTexture2D(Texture texture)
  101. {
  102. if(texture2D == null) texture2D = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  103. RenderTexture currentRT = RenderTexture.active;
  104. RenderTexture renderTexture = RenderTexture.GetTemporary(
  105. texture.width,
  106. texture.height,
  107. 0,
  108. RenderTextureFormat.ARGB32,
  109. RenderTextureReadWrite.Linear);
  110. Graphics.Blit(texture, renderTexture);
  111. RenderTexture.active = renderTexture;
  112. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  113. texture2D.Apply();
  114. RenderTexture.active = currentRT;
  115. RenderTexture.ReleaseTemporary(renderTexture);
  116. return texture2D;
  117. }
  118. private Texture2D TextureToTexture2D2(Texture texture)
  119. {
  120. Texture2D _texture2D = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  121. RenderTexture currentRT = RenderTexture.active;
  122. RenderTexture renderTexture = RenderTexture.GetTemporary(
  123. texture.width,
  124. texture.height,
  125. 0,
  126. RenderTextureFormat.ARGB32,
  127. RenderTextureReadWrite.Linear);
  128. Graphics.Blit(texture, renderTexture);
  129. RenderTexture.active = renderTexture;
  130. _texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  131. _texture2D.Apply();
  132. RenderTexture.active = currentRT;
  133. RenderTexture.ReleaseTemporary(renderTexture);
  134. return _texture2D;
  135. }
  136. Texture2D m_SourceTexture;
  137. Texture2D m_TargetTexture;
  138. private void CreateTextureIfNeeded(Texture texture)
  139. {
  140. //&& (m_SourceTexture.width != texture.width || m_SourceTexture.height != texture.height)
  141. //if (m_SourceTexture != null)
  142. //{
  143. // DestroyImmediate(m_SourceTexture);
  144. // m_SourceTexture = null;
  145. //}
  146. if(m_SourceTexture == null) m_SourceTexture = new Texture2D(texture.width, texture.height,TextureFormat.ARGB32,false,true);
  147. RenderTexture renderTexture = RenderTexture.GetTemporary(
  148. texture.width,
  149. texture.height,
  150. 0,
  151. RenderTextureFormat.ARGB32,
  152. RenderTextureReadWrite.Linear);
  153. Graphics.Blit(texture, renderTexture);
  154. RenderTexture currentRT = RenderTexture.active;
  155. RenderTexture.active = renderTexture;
  156. m_SourceTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  157. m_SourceTexture.Apply();
  158. RenderTexture.active = currentRT;
  159. RenderTexture.ReleaseTemporary(renderTexture);
  160. if (m_TargetTexture != null)
  161. {
  162. DestroyImmediate(m_TargetTexture);
  163. m_TargetTexture = null;
  164. }
  165. if (m_TargetTexture == null)
  166. {
  167. m_TargetTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  168. m_TargetTexture.wrapMode = TextureWrapMode.Clamp;
  169. rawImage2.texture = m_TargetTexture;
  170. }
  171. }
  172. private static Texture2D ResizeTexture(Texture source)
  173. {
  174. if (source != null)
  175. {
  176. int width = source.width;
  177. int height = source.height;
  178. // 创建临时的RenderTexture
  179. RenderTexture renderTex = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  180. // 复制source的纹理到RenderTexture里
  181. Graphics.Blit(source, renderTex);
  182. // 开启当前RenderTexture激活状态
  183. RenderTexture previous = RenderTexture.active;
  184. RenderTexture.active = renderTex;
  185. // 创建修改后的纹理,保持与源纹理相同压缩格式
  186. Texture2D resizedTexture = new Texture2D(width, height, TextureFormat.ARGB32, false,true);
  187. // 读取像素到创建的纹理中
  188. resizedTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  189. // 应用修改到GPU上
  190. resizedTexture.Apply();
  191. // 停止当前RenderTexture工作
  192. RenderTexture.active = previous;
  193. // 释放内存
  194. RenderTexture.ReleaseTemporary(renderTex);
  195. return resizedTexture;
  196. }
  197. else
  198. {
  199. return null;
  200. }
  201. }
  202. private Texture2D CreateTextureBySetGetPixels(Texture2D sourceTexture)
  203. {
  204. if (sourceTexture != null)
  205. {
  206. int sourceMipLevel = 0;
  207. Color[] srcPixels = sourceTexture.GetPixels(sourceMipLevel);
  208. //newTextureByPixels = new Texture2D(sourceTexture.width, sourceTexture.height);
  209. Texture2D newTextureByPixels = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false);
  210. newTextureByPixels.SetPixels(srcPixels);
  211. newTextureByPixels.Apply();
  212. return newTextureByPixels;
  213. }
  214. else
  215. {
  216. Debug.LogWarning("Texture is null");
  217. return null;
  218. }
  219. }
  220. private Texture2D CreateTextureByRawData(Texture2D sourceTexture)
  221. {
  222. if (sourceTexture != null)
  223. {
  224. Texture2D newTextureByRawData = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false);
  225. // 对于运行时纹理生成,也可以通过GetRawTextureData直接写入纹理数据,返回一个Unity.Collections.NativeArray
  226. // 这可以更快,因为它避免了 LoadRawTextureData 会执行的内存复制。
  227. newTextureByRawData.LoadRawTextureData(sourceTexture.GetRawTextureData());
  228. newTextureByRawData.Apply();
  229. return newTextureByRawData;
  230. }
  231. else
  232. {
  233. Debug.LogWarning("Texture is null");
  234. return null;
  235. }
  236. }
  237. }