Ingen beskrivning
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.

qr.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ZXing;
  12. using ZXing.QrCode;
  13. using System.Net;
  14. public class qr : MonoBehaviour
  15. {
  16. [SerializeField] RawImage rowImage;
  17. [SerializeField] TextMeshProUGUI 測試;
  18. private WebCamTexture myCam;//接收攝影機返回的圖片數據
  19. private BarcodeReader barcodeReader;
  20. public CanvasScaler canvasScaler;
  21. // Start is called before the first frame update
  22. void Awake()
  23. {
  24. Main.Global.鏡頭啟動=false;
  25. barcodeReader = new BarcodeReader();
  26. int num = Main.Global.camfps;
  27. StartCoroutine(open_Camera(num));
  28. }
  29. void Start()
  30. {
  31. float screenWidth = Screen.width;
  32. float screenHeight = Screen.height;
  33. float rate = screenWidth/screenHeight;
  34. if (rate>1.6)
  35. {
  36. canvasScaler.matchWidthOrHeight = 1f;
  37. }
  38. else
  39. {
  40. canvasScaler.matchWidthOrHeight = 0f;
  41. }
  42. }
  43. void Update()
  44. {
  45. if (Main.Global.鏡頭啟動=true)
  46. {
  47. DecodeQR();
  48. }
  49. }
  50. public void exit_click()
  51. {
  52. change_Secen(0);
  53. }
  54. private void DecodeQR()
  55. {
  56. Result res = barcodeReader.Decode(myCam.GetPixels32(), myCam.width, myCam.height);
  57. if (res != null)
  58. {
  59. Main.Global.PI=res.ToString();
  60. 測試.text="readQR="+Main.Global.PI;
  61. StartCoroutine(QR登入());
  62. }
  63. else
  64. {
  65. 測試.text="readfalse";
  66. }
  67. }
  68. IEnumerator QR登入()
  69. {
  70. string strcon = "QR";
  71. string name = Main.Global.PI;
  72. string se = Main.Global.預設伺服器路徑;
  73. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&ID={1}&se={2}", strcon, name,se);
  74. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  75. yield return request.SendWebRequest();
  76. if (request.result == UnityWebRequest.Result.ConnectionError)
  77. {
  78. //Debug.Log(request.error);
  79. yield break;
  80. }
  81. else
  82. {
  83. 測試.text+="connect_sucess"+" ";
  84. }
  85. string htm2 = request.downloadHandler.text;
  86. //Debug.Log(htm2);
  87. Main.Global.鏡頭啟動=false;
  88. if (htm2=="登入失敗")
  89. {
  90. // 測試.text+=htm2;
  91. change_Secen(0);
  92. }
  93. else
  94. {
  95. Main.Global.PA=htm2;
  96. 測試.text+=htm2+" ";
  97. StartCoroutine(判斷密碼());
  98. }
  99. }
  100. IEnumerator 判斷密碼()
  101. {
  102. string strcon = "登入1";
  103. string name = Main.Global.PA;
  104. string se = Main.Global.預設伺服器路徑;
  105. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&帳號={1}&se={2}", strcon, name, se);
  106. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  107. yield return request.SendWebRequest();
  108. if (request.result == UnityWebRequest.Result.ConnectionError)
  109. {
  110. //Debug.Log(request.error);
  111. yield break;
  112. }
  113. string str = request.downloadHandler.text.ToString();
  114. Main.Global.PB=str;
  115. 測試.text+=str+" ";
  116. change_Secen(1);
  117. }
  118. private IEnumerator open_Camera(int fps)
  119. {
  120. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); //授權開啟鏡頭
  121. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  122. {
  123. //設置攝影機要攝影的區域
  124. myCam = new WebCamTexture(WebCamTexture.devices[0].name, Screen.width, Screen.height, fps);/* (攝影機名稱, 攝影機要拍到的寬度, 攝影機要拍到的高度, 攝影機的FPS) */
  125. rowImage.texture = myCam;
  126. myCam.Play();//開啟攝影機
  127. Main.Global.鏡頭啟動=true;
  128. }
  129. }
  130. public void change_Secen(int _screenNum)
  131. {
  132. myCam.Stop();
  133. SceneManager.LoadScene(_screenNum);
  134. }
  135. }