1
0

RemoveBGManagerOld.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. using BestHTTP;
  7. using BestHTTP.Forms;
  8. using UnityEngine.Events;
  9. using ToneTuneToolkit.Data;
  10. namespace LonginesYogaPhotoJoy
  11. {
  12. public class RemoveBGManagerOld : MonoBehaviour
  13. {
  14. public static RemoveBGManagerOld Instance;
  15. private UnityAction<Texture2D> onRemoveBGCompelete;
  16. private const string removebgAPI = "https://api.remove.bg/v1.0/removebg";
  17. private string key;
  18. // 测试key U1j4pJeg9zT63Kfa8zDmiRkG
  19. // 正式key 76YHaSA8WZYmbZXfqfBeYbqy // 20240606 剩余100
  20. // live X859F9v3g4YpoPBRQe2n7h8T
  21. [Header("DEBUG - Peek")]
  22. [SerializeField] private Texture2D preuploadTexture;
  23. [SerializeField] private Texture2D downloadTexture;
  24. // ==================================================
  25. private void Awake()
  26. {
  27. Instance = this;
  28. }
  29. private void Start()
  30. {
  31. Init();
  32. }
  33. // ==================================================
  34. public void AddEventListener(UnityAction<Texture2D> unityAction)
  35. {
  36. onRemoveBGCompelete += unityAction;
  37. return;
  38. }
  39. public void RemoveEventListener(UnityAction<Texture2D> unityAction)
  40. {
  41. onRemoveBGCompelete -= unityAction;
  42. return;
  43. }
  44. // ==================================================
  45. private void Init()
  46. {
  47. key = JsonManager.GetJson(Application.streamingAssetsPath + "/removebgkey.json", "Key");
  48. return;
  49. }
  50. // ==================================================
  51. /// <summary>
  52. /// 上传照片至RemoveBG
  53. /// </summary>
  54. /// <param name="value"></param>
  55. public void UploadPhoto2RemoveBG(Texture2D value)
  56. {
  57. preuploadTexture = value;
  58. byte[] bytes = value.EncodeToPNG();
  59. HTTPMultiPartForm form = new HTTPMultiPartForm();
  60. form.AddBinaryData("image_file", bytes);
  61. // form.AddField("size", "full");
  62. form.AddField("size", "auto");
  63. form.AddField("type", "person");
  64. HTTPRequest request = new HTTPRequest(
  65. new Uri(removebgAPI),
  66. HTTPMethods.Post,
  67. UploadPictureCallback);
  68. request.SetHeader("X-Api-Key", key);
  69. request.SetForm(form);
  70. request.Send();
  71. return;
  72. }
  73. private void UploadPictureCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
  74. {
  75. if (httpResponse == null)
  76. {
  77. Debug.Log("RemoveBG请求无响应...<color=red>[ER]</color>");
  78. return;
  79. }
  80. if (httpResponse.StatusCode == 200)
  81. {
  82. Debug.Log(httpResponse.DataAsTexture2D.width + " / " + httpResponse.DataAsTexture2D.height);
  83. string fullPath = $"{Application.streamingAssetsPath}/RemoveBG/{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png";
  84. byte[] bytes = httpResponse.DataAsTexture2D.EncodeToPNG();
  85. File.WriteAllBytes(fullPath, bytes);
  86. downloadTexture = httpResponse.DataAsTexture2D; // 无必要
  87. if (onRemoveBGCompelete != null)
  88. {
  89. onRemoveBGCompelete(httpResponse.DataAsTexture2D);
  90. }
  91. Debug.Log("RemoveBG返回成功...[OK]");
  92. }
  93. else
  94. {
  95. Debug.Log($"RemoveBG返回失败,{httpResponse.StatusCode}...<color=red>[ER]</color>");
  96. Debug.Log(httpResponse.DataAsText);
  97. // UploadPhoto2RemoveBG(preuoloadTexture);
  98. }
  99. return;
  100. }
  101. }
  102. }