暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using TMPro;
  10. using System;
  11. using System.IO;
  12. using ZXing;
  13. using ZXing.QrCode;
  14. using System.Net;
  15. using SimpleJSON;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Linq;
  19. using System.Runtime.InteropServices;
  20. using System.Globalization;
  21. public class 電簽 : MonoBehaviour
  22. {
  23. public RawImage rawImage;
  24. public CanvasScaler canvasScaler;
  25. private string loadingText = "";
  26. private int dotCount = 0;
  27. [SerializeField] GameObject 讀取面板;
  28. [SerializeField] TextMeshProUGUI 讀取;
  29. [SerializeField] GameObject 彈跳面板;
  30. [SerializeField] TextMeshProUGUI 彈跳文字;
  31. private Texture2D canvasTexture;
  32. public Color drawColor = Color.black;
  33. public float brushSize = 10f;
  34. private bool isDrawing = false;
  35. private Vector2 lastPos;
  36. string 圖片資料庫;
  37. // Start is called before the first frame update
  38. void Start()
  39. {
  40. Screen.autorotateToPortrait = false;
  41. Screen.orientation = ScreenOrientation.LandscapeLeft;
  42. //Main.Global.固高 = 1900f;
  43. //Main.Global.固寬 = 900f;
  44. //Main.Global.固比1 = 1f;
  45. //Main.Global.螢幕格式="瘦";
  46. //canvasScaler.referenceResolution = new Vector2(Main.Global.固高, Main.Global.固寬);
  47. //canvasScaler.matchWidthOrHeight = Main.Global.固比1;
  48. //Main.Global.阿帕契路徑 = "http://106.1.48.106:8080/event-star/hskin_logint.php?";
  49. //Main.Global.預設伺服器路徑 = "test";
  50. //Main.Global.控制表流水 = "113072301";
  51. //Main.Global.PA = "即時清";
  52. StartCoroutine(AnimateText());
  53. StartCoroutine(圖片資料庫選擇());
  54. }
  55. IEnumerator AnimateText()
  56. {
  57. string qqq = 讀取.text.ToString();
  58. while (true)
  59. {
  60. yield return new WaitForSeconds(0.5f);
  61. if (dotCount < 3)
  62. {
  63. loadingText += ".";
  64. dotCount++;
  65. }
  66. else
  67. {
  68. loadingText = "";
  69. dotCount = 0;
  70. }
  71. 讀取.text = qqq + loadingText;
  72. }
  73. }
  74. IEnumerator 圖片資料庫選擇()
  75. {
  76. 讀取面板.SetActive(true);
  77. string strcon = "圖片資料庫選擇";
  78. string se = Main.Global.預設伺服器路徑;
  79. string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}&se={1}", strcon, se);
  80. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  81. yield return request.SendWebRequest();
  82. if (request.result == UnityWebRequest.Result.ConnectionError)
  83. {
  84. //Debug.Log(request.error);
  85. yield break;
  86. }
  87. string str = request.downloadHandler.text.ToString();
  88. if (str == "n")
  89. {
  90. 讀取面板.SetActive(false);
  91. 彈跳面板.SetActive(true);
  92. 彈跳文字.text = "圖片資料庫已滿,請找系統管理員!!";
  93. yield return new WaitForSeconds(2);
  94. //change_Secen(2);
  95. }
  96. else
  97. {
  98. 圖片資料庫 = str;
  99. Debug.Log(圖片資料庫);
  100. 讀取面板.SetActive(false);
  101. ddd();
  102. }
  103. }
  104. void ddd()
  105. {
  106. canvasTexture = new Texture2D((int)rawImage.rectTransform.rect.width, (int)rawImage.rectTransform.rect.height);
  107. Color[] fillColorArray = canvasTexture.GetPixels();
  108. for (int i = 0; i < fillColorArray.Length; ++i)
  109. {
  110. fillColorArray[i] = Color.white;
  111. }
  112. canvasTexture.SetPixels(fillColorArray);
  113. canvasTexture.Apply();
  114. rawImage.texture = canvasTexture;
  115. }
  116. void Update()
  117. {
  118. if (Input.GetMouseButtonDown(0))
  119. {
  120. isDrawing = true;
  121. lastPos = GetMousePosition();
  122. }
  123. else if (Input.GetMouseButtonUp(0))
  124. {
  125. isDrawing = false;
  126. }
  127. if (isDrawing)
  128. {
  129. Vector2 currentPos = GetMousePosition();
  130. DrawLine(lastPos, currentPos);
  131. lastPos = currentPos;
  132. }
  133. }
  134. void DrawLine(Vector2 from, Vector2 to)
  135. {
  136. Vector2 diff = to - from;
  137. float distance = diff.magnitude;
  138. int steps = Mathf.RoundToInt(distance);
  139. for (int i = 0; i < steps; i++)
  140. {
  141. float t = i / distance;
  142. Vector2 point = Vector2.Lerp(from, to, t);
  143. DrawPoint((int)point.x, (int)point.y);
  144. }
  145. canvasTexture.Apply();
  146. }
  147. void DrawPoint(int x, int y)
  148. {
  149. for (int i = -Mathf.RoundToInt(brushSize / 2); i < Mathf.RoundToInt(brushSize / 2); i++)
  150. {
  151. for (int j = -Mathf.RoundToInt(brushSize / 2); j < Mathf.RoundToInt(brushSize / 2); j++)
  152. {
  153. if (x + i >= 0 && x + i < canvasTexture.width && y + j >= 0 && y + j < canvasTexture.height)
  154. {
  155. canvasTexture.SetPixel(x + i, y + j, drawColor);
  156. }
  157. }
  158. }
  159. }
  160. Vector2 GetMousePosition()
  161. {
  162. Vector2 localMousePos;
  163. RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, Input.mousePosition, null, out localMousePos);
  164. return localMousePos + rawImage.rectTransform.rect.size / 2;
  165. }
  166. public void 存檔()
  167. {
  168. StartCoroutine(查詢最後一筆簽名圖片編號());
  169. }
  170. public void ClearCanvas()
  171. {
  172. Color[] clearColorArray = new Color[canvasTexture.width * canvasTexture.height];
  173. for (int i = 0; i < clearColorArray.Length; i++)
  174. {
  175. clearColorArray[i] = Color.clear;
  176. }
  177. canvasTexture.SetPixels(clearColorArray);
  178. canvasTexture.Apply();
  179. ddd();
  180. }
  181. IEnumerator 查詢最後一筆簽名圖片編號()
  182. {
  183. string strcon = "查詢最後一筆簽名圖片編號";
  184. string se = Main.Global.預設伺服器路徑;
  185. string pa1 = 圖片資料庫;
  186. string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}&se={1}&PA1={2}", strcon, se, pa1);
  187. Debug.Log(strUrl);
  188. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  189. yield return request.SendWebRequest();
  190. if (request.result == UnityWebRequest.Result.ConnectionError)
  191. {
  192. //Debug.Log(request.error);
  193. yield break;
  194. }
  195. Debug.Log(request.downloadHandler.text.ToString());
  196. string str = request.downloadHandler.text.ToString();
  197. if (str == "n")
  198. {
  199. str = "IR00000000";
  200. }
  201. string cnum = str.Substring(2);
  202. int inum = int.Parse(cnum);
  203. inum += 1;
  204. string 新流水 = "IR";
  205. if (inum < 10)
  206. {
  207. 新流水 += "0000000" + inum.ToString();
  208. }
  209. if (inum > 9 && inum < 100)
  210. {
  211. 新流水 += "000000" + inum.ToString();
  212. }
  213. if (inum > 99 && inum < 1000)
  214. {
  215. 新流水 += "00000" + inum.ToString();
  216. }
  217. if (inum > 999 && inum < 10000)
  218. {
  219. 新流水 += "0000" + inum.ToString();
  220. }
  221. if (inum > 9999 && inum < 100000)
  222. {
  223. 新流水 += "000" + inum.ToString();
  224. }
  225. if (inum > 99999 && inum < 10000000)
  226. {
  227. 新流水 += "00" + inum.ToString();
  228. }
  229. if (inum > 999999 && inum < 10000000)
  230. {
  231. 新流水 += "0" + inum.ToString();
  232. }
  233. if (inum > 9999999 && inum < 100000000)
  234. {
  235. 新流水 += inum.ToString();
  236. }
  237. yield return StartCoroutine(文件明細表修改(新流水));
  238. }
  239. IEnumerator 文件明細表修改(string 新流水)
  240. {
  241. 圖片上傳(新流水);
  242. string strcon = "文件明細表修改";
  243. string se = Main.Global.預設伺服器路徑;
  244. string pa = Main.Global.控制表流水;
  245. string pa1 = 圖片資料庫;
  246. string pa2 = 新流水;
  247. string pa3 = Main.Global.PA;
  248. DateTime currentDate = DateTime.Now;
  249. string pa4 = currentDate.ToString("yyyy/MM/dd");
  250. string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}&se={1}&PA={2}&PA1={3}&PA2={4}&PA3={5}&PA4={6}", strcon, se, pa, pa1, pa2, pa3, pa4);
  251. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  252. yield return request.SendWebRequest();
  253. if (request.result == UnityWebRequest.Result.ConnectionError)
  254. {
  255. //Debug.Log(request.error);
  256. yield break;
  257. }
  258. 彈跳面板.SetActive(true);
  259. 彈跳文字.text = "新增成功";
  260. yield return new WaitForSeconds(2f);
  261. 返回();
  262. }
  263. public void 圖片上傳(string 流水)
  264. {
  265. string ip = "";
  266. if (Main.Global.預設伺服器路徑 == "test")
  267. {
  268. ip = "106.1.48.106,4567";
  269. }
  270. else
  271. {
  272. ip = "mssql-147629-0.cloudclusters.net,12711";
  273. }
  274. string connectionString = "Data Source=" + ip + ";Initial Catalog=" + 圖片資料庫 + ";User ID=B70340;Password=Lee0911274990";
  275. Debug.Log(connectionString);
  276. using (SqlConnection connection = new SqlConnection(connectionString))
  277. {
  278. try
  279. {
  280. connection.Open();
  281. Texture2D texture2D = new Texture2D((rawImage.texture as Texture2D).width, (rawImage.texture as Texture2D).height);
  282. Graphics.CopyTexture(rawImage.texture, texture2D);
  283. // 將 Texture2D 轉換為 PNG 格式的二進制數據
  284. byte[] imageBytes = texture2D.EncodeToPNG();
  285. string query = "INSERT INTO 簽名圖片庫 (圖片流水號, 圖片) VALUES (@流水號, @binary_file)";
  286. using (SqlCommand command = new SqlCommand(query, connection))
  287. {
  288. // 添加流水號參數
  289. command.Parameters.AddWithValue("@流水號", 流水);
  290. // 添加二進制圖片數據參數
  291. command.Parameters.AddWithValue("@binary_file", imageBytes);
  292. int rowsAffected = command.ExecuteNonQuery();
  293. Debug.Log("成功插入 " + rowsAffected + " 行數據");
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. Debug.LogError("Error connecting to the database: " + ex.Message);
  299. 彈跳面板.SetActive(true);
  300. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  301. }
  302. finally
  303. {
  304. connection.Close();
  305. }
  306. }
  307. }
  308. public void 返回()
  309. {
  310. if (Main.Global.螢幕格式 == "瘦")
  311. {
  312. change_Secen(20);
  313. }
  314. else
  315. {
  316. change_Secen(21);
  317. }
  318. }
  319. public void 彈跳面板關閉()
  320. {
  321. 彈跳面板.SetActive(false);
  322. 彈跳文字.text = "";
  323. }
  324. public void change_Secen(int _screenNum)
  325. {
  326. SceneManager.LoadScene(_screenNum);
  327. }
  328. }