Test.cs 9.5 KB

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