TestManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.IO;
  7. namespace MartellController
  8. {
  9. public class TestManager : MonoBehaviour
  10. {
  11. public static TestManager Instance;
  12. // ==================================================
  13. private void Awake()
  14. {
  15. Instance = this;
  16. }
  17. private void Update()
  18. {
  19. OrginalStreamRawImage.texture = CameraManager.GetCamTexture();
  20. }
  21. // ==================================================
  22. // Step 00
  23. // 视频流预览
  24. public RawImage OrginalStreamRawImage;
  25. public RectTransform OrginalStreamShotArea;//用来取景的ui,设置为透明的
  26. public GameObject GOAyayi;
  27. public void TakeShotOrgin()
  28. {
  29. GOAyayi.SetActive(false);
  30. StartCoroutine(TakeScreenshotAction(OrginalStreamShotArea));
  31. return;
  32. }
  33. private IEnumerator TakeScreenshotAction(RectTransform screenshotArea)
  34. {
  35. yield return new WaitForEndOfFrame();
  36. int width = (int)screenshotArea.rect.width;
  37. int height = (int)screenshotArea.rect.height;
  38. Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
  39. float leftBottomX = screenshotArea.transform.position.x + screenshotArea.rect.xMin;
  40. float leftBottomY = screenshotArea.transform.position.y + screenshotArea.rect.yMin;
  41. texture2D.ReadPixels(new Rect(leftBottomX, leftBottomY, width, height), 0, 0);
  42. texture2D.Apply();
  43. string fullPath = $"{Application.streamingAssetsPath}/OrginalPictures/{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png";
  44. byte[] bytes = texture2D.EncodeToPNG();
  45. File.WriteAllBytes(fullPath, bytes);
  46. UpdateScreenshotPreview(texture2D);
  47. yield break;
  48. }
  49. // ==================================================
  50. // Step 01
  51. // 截图预览
  52. public RawImage ScreenshotPreviewRawImage;
  53. public Texture2D ScreenshotPreviewTexture2D;
  54. private void UpdateScreenshotPreview(Texture2D value)
  55. {
  56. // 贴图复制
  57. byte[] data = value.GetRawTextureData();
  58. ScreenshotPreviewTexture2D = new Texture2D(value.width, value.height, TextureFormat.RGBA32, false);
  59. ScreenshotPreviewTexture2D.LoadRawTextureData(data);
  60. ScreenshotPreviewTexture2D.Apply();
  61. ScreenshotPreviewRawImage.texture = ScreenshotPreviewTexture2D;
  62. GOAyayi.SetActive(true);
  63. NetManager.Instance.UploadPicture2RemoveBG(value); // 把图片发出去
  64. return;
  65. }
  66. // ==================================================
  67. // RemoveBG预览
  68. public RawImage RemoveBGPreviewRawImage;
  69. public Texture2D RemoveBGPreviewTexture2D;
  70. public RectTransform RemoveBGPreviewShotArea;//用来取景的ui,设置为透明的
  71. public void UpdateRemoveBGPreview(Texture2D value)
  72. {
  73. // 请勿复制
  74. // byte[] data = value.GetRawTextureData();
  75. // RemoveBGPreviewTexture2D = new Texture2D(value.width, value.height, TextureFormat.RGBA32, false);
  76. // RemoveBGPreviewTexture2D.LoadRawTextureData(data);
  77. // RemoveBGPreviewTexture2D.Apply();
  78. RemoveBGPreviewTexture2D = value;
  79. RemoveBGPreviewRawImage.texture = value;
  80. StartCoroutine(TakeMixshotAction(RemoveBGPreviewShotArea));
  81. return;
  82. }
  83. private IEnumerator TakeMixshotAction(RectTransform screenshotArea)
  84. {
  85. yield return new WaitForEndOfFrame();
  86. int width = (int)screenshotArea.rect.width;
  87. int height = (int)screenshotArea.rect.height;
  88. Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
  89. float leftBottomX = screenshotArea.transform.position.x + screenshotArea.rect.xMin;
  90. float leftBottomY = screenshotArea.transform.position.y + screenshotArea.rect.yMin;
  91. texture2D.ReadPixels(new Rect(leftBottomX, leftBottomY, width, height), 0, 0);
  92. texture2D.Apply();
  93. string fullPath = $"{Application.streamingAssetsPath}/MixPictures/{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png";
  94. byte[] bytes = texture2D.EncodeToPNG();
  95. File.WriteAllBytes(fullPath, bytes);
  96. yield break;
  97. }
  98. // ==================================================
  99. }
  100. }