123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using System.Text.RegularExpressions;
- using System.Data.SqlClient;
- using TMPro;
- using System;
- using System.IO;
- using ZXing;
- using ZXing.QrCode;
- using System.Net;
- using SimpleJSON;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Globalization;
- public class 電簽 : MonoBehaviour
- {
- public RawImage rawImage;
- public CanvasScaler canvasScaler;
- private string loadingText = "";
- private int dotCount = 0;
- [SerializeField] GameObject 讀取面板;
- [SerializeField] TextMeshProUGUI 讀取;
- [SerializeField] GameObject 彈跳面板;
- [SerializeField] TextMeshProUGUI 彈跳文字;
- private Texture2D canvasTexture;
- public Color drawColor = Color.black;
- public float brushSize = 10f;
-
- private bool isDrawing = false;
- private Vector2 lastPos;
-
- string 圖片資料庫;
- // Start is called before the first frame update
- void Start()
- {
- Screen.autorotateToPortrait = false;
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- //Main.Global.固高 = 1900f;
- //Main.Global.固寬 = 900f;
- //Main.Global.固比1 = 1f;
- //Main.Global.螢幕格式="瘦";
- //canvasScaler.referenceResolution = new Vector2(Main.Global.固高, Main.Global.固寬);
- //canvasScaler.matchWidthOrHeight = Main.Global.固比1;
- //Main.Global.阿帕契路徑 = "http://106.1.48.106:8080/event-star/hskin_logint.php?";
- //Main.Global.預設伺服器路徑 = "test";
- //Main.Global.控制表流水 = "113072301";
- //Main.Global.PA = "即時清";
- StartCoroutine(AnimateText());
- StartCoroutine(圖片資料庫選擇());
- }
- IEnumerator AnimateText()
- {
- string qqq = 讀取.text.ToString();
- while (true)
- {
- yield return new WaitForSeconds(0.5f);
-
- if (dotCount < 3)
- {
- loadingText += ".";
- dotCount++;
- }
- else
- {
- loadingText = "";
- dotCount = 0;
- }
-
- 讀取.text = qqq + loadingText;
- }
- }
- IEnumerator 圖片資料庫選擇()
- {
- 讀取面板.SetActive(true);
- string strcon = "圖片資料庫選擇";
- string se = Main.Global.預設伺服器路徑;
- string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}&se={1}", strcon, se);
- UnityWebRequest request = UnityWebRequest.Get(strUrl);
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ConnectionError)
- {
- //Debug.Log(request.error);
- yield break;
- }
- string str = request.downloadHandler.text.ToString();
-
- if (str == "n")
- {
- 讀取面板.SetActive(false);
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "圖片資料庫已滿,請找系統管理員!!";
- yield return new WaitForSeconds(2);
- //change_Secen(2);
- }
- else
- {
- 圖片資料庫 = str;
- Debug.Log(圖片資料庫);
- 讀取面板.SetActive(false);
- ddd();
- }
-
- }
- void ddd()
- {
- canvasTexture = new Texture2D((int)rawImage.rectTransform.rect.width, (int)rawImage.rectTransform.rect.height);
-
- Color[] fillColorArray = canvasTexture.GetPixels();
-
- for (int i = 0; i < fillColorArray.Length; ++i)
- {
- fillColorArray[i] = Color.white;
- }
-
- canvasTexture.SetPixels(fillColorArray);
- canvasTexture.Apply();
- rawImage.texture = canvasTexture;
- }
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- isDrawing = true;
- lastPos = GetMousePosition();
- }
- else if (Input.GetMouseButtonUp(0))
- {
- isDrawing = false;
- }
-
- if (isDrawing)
- {
- Vector2 currentPos = GetMousePosition();
- DrawLine(lastPos, currentPos);
- lastPos = currentPos;
- }
- }
- void DrawLine(Vector2 from, Vector2 to)
- {
- Vector2 diff = to - from;
- float distance = diff.magnitude;
- int steps = Mathf.RoundToInt(distance);
- for (int i = 0; i < steps; i++)
- {
- float t = i / distance;
- Vector2 point = Vector2.Lerp(from, to, t);
- DrawPoint((int)point.x, (int)point.y);
- }
- canvasTexture.Apply();
- }
-
- void DrawPoint(int x, int y)
- {
- for (int i = -Mathf.RoundToInt(brushSize / 2); i < Mathf.RoundToInt(brushSize / 2); i++)
- {
- for (int j = -Mathf.RoundToInt(brushSize / 2); j < Mathf.RoundToInt(brushSize / 2); j++)
- {
- if (x + i >= 0 && x + i < canvasTexture.width && y + j >= 0 && y + j < canvasTexture.height)
- {
- canvasTexture.SetPixel(x + i, y + j, drawColor);
- }
- }
- }
- }
-
- Vector2 GetMousePosition()
- {
- Vector2 localMousePos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, Input.mousePosition, null, out localMousePos);
- return localMousePos + rawImage.rectTransform.rect.size / 2;
- }
- public void 存檔()
- {
- StartCoroutine(查詢最後一筆簽名圖片編號());
- }
-
- public void ClearCanvas()
- {
- Color[] clearColorArray = new Color[canvasTexture.width * canvasTexture.height];
- for (int i = 0; i < clearColorArray.Length; i++)
- {
- clearColorArray[i] = Color.clear;
- }
- canvasTexture.SetPixels(clearColorArray);
- canvasTexture.Apply();
- ddd();
- }
-
- IEnumerator 查詢最後一筆簽名圖片編號()
- {
- string strcon = "查詢最後一筆簽名圖片編號";
- string se = Main.Global.預設伺服器路徑;
- string pa1 = 圖片資料庫;
- string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}&se={1}&PA1={2}", strcon, se, pa1);
- Debug.Log(strUrl);
- UnityWebRequest request = UnityWebRequest.Get(strUrl);
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ConnectionError)
- {
- //Debug.Log(request.error);
- yield break;
- }
- Debug.Log(request.downloadHandler.text.ToString());
- string str = request.downloadHandler.text.ToString();
- if (str == "n")
- {
- str = "IR00000000";
- }
- string cnum = str.Substring(2);
- int inum = int.Parse(cnum);
- inum += 1;
- string 新流水 = "IR";
- if (inum < 10)
- {
- 新流水 += "0000000" + inum.ToString();
- }
- if (inum > 9 && inum < 100)
- {
- 新流水 += "000000" + inum.ToString();
- }
- if (inum > 99 && inum < 1000)
- {
- 新流水 += "00000" + inum.ToString();
- }
- if (inum > 999 && inum < 10000)
- {
- 新流水 += "0000" + inum.ToString();
- }
- if (inum > 9999 && inum < 100000)
- {
- 新流水 += "000" + inum.ToString();
- }
- if (inum > 99999 && inum < 10000000)
- {
- 新流水 += "00" + inum.ToString();
- }
- if (inum > 999999 && inum < 10000000)
- {
- 新流水 += "0" + inum.ToString();
- }
- if (inum > 9999999 && inum < 100000000)
- {
- 新流水 += inum.ToString();
- }
- yield return StartCoroutine(文件明細表修改(新流水));
- }
-
- IEnumerator 文件明細表修改(string 新流水)
- {
- 圖片上傳(新流水);
- string strcon = "文件明細表修改";
- string se = Main.Global.預設伺服器路徑;
- string pa = Main.Global.控制表流水;
- string pa1 = 圖片資料庫;
- string pa2 = 新流水;
- string pa3 = Main.Global.PA;
- DateTime currentDate = DateTime.Now;
- string pa4 = currentDate.ToString("yyyy/MM/dd");
- 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);
- UnityWebRequest request = UnityWebRequest.Get(strUrl);
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ConnectionError)
- {
- //Debug.Log(request.error);
- yield break;
- }
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "新增成功";
- yield return new WaitForSeconds(2f);
- 返回();
- }
- public void 圖片上傳(string 流水)
- {
- string ip = "";
- if (Main.Global.預設伺服器路徑 == "test")
- {
- ip = "106.1.48.106,4567";
- }
- else
- {
- ip = "mssql-147629-0.cloudclusters.net,12711";
- }
- string connectionString = "Data Source=" + ip + ";Initial Catalog=" + 圖片資料庫 + ";User ID=B70340;Password=Lee0911274990";
- Debug.Log(connectionString);
-
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- try
- {
- connection.Open();
- Texture2D texture2D = new Texture2D((rawImage.texture as Texture2D).width, (rawImage.texture as Texture2D).height);
- Graphics.CopyTexture(rawImage.texture, texture2D);
-
- // 將 Texture2D 轉換為 PNG 格式的二進制數據
- byte[] imageBytes = texture2D.EncodeToPNG();
-
- string query = "INSERT INTO 簽名圖片庫 (圖片流水號, 圖片) VALUES (@流水號, @binary_file)";
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- // 添加流水號參數
- command.Parameters.AddWithValue("@流水號", 流水);
-
- // 添加二進制圖片數據參數
- command.Parameters.AddWithValue("@binary_file", imageBytes);
- int rowsAffected = command.ExecuteNonQuery();
- Debug.Log("成功插入 " + rowsAffected + " 行數據");
- }
- }
- catch (Exception ex)
- {
- Debug.LogError("Error connecting to the database: " + ex.Message);
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
- }
- finally
- {
- connection.Close();
- }
- }
- }
- public void 返回()
- {
- if (Main.Global.螢幕格式 == "瘦")
- {
- change_Secen(20);
- }
- else
- {
- change_Secen(21);
- }
- }
- public void 彈跳面板關閉()
- {
- 彈跳面板.SetActive(false);
- 彈跳文字.text = "";
- }
- public void change_Secen(int _screenNum)
- {
-
- SceneManager.LoadScene(_screenNum);
- }
- }
|