QRCodeDetectorWebCamTextureExample.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using OpenCVForUnity.CoreModule;
  7. using OpenCVForUnity.ObjdetectModule;
  8. using OpenCVForUnity.ImgprocModule;
  9. using OpenCVForUnity.UnityUtils;
  10. using OpenCVForUnity.UnityUtils.Helper;
  11. namespace OpenCVForUnityExample
  12. {
  13. /// <summary>
  14. /// QRCodeDetector WebCamTexture Example
  15. /// An example of detecting QRCode in a image of WebCamTexture using the QRCodeDetector class.
  16. /// https://github.com/opencv/opencv/blob/master/samples/cpp/qrcode.cpp
  17. /// </summary>
  18. [RequireComponent (typeof(WebCamTextureToMatHelper))]
  19. public class QRCodeDetectorWebCamTextureExample : MonoBehaviour
  20. {
  21. /// <summary>
  22. /// The gray mat.
  23. /// </summary>
  24. Mat grayMat;
  25. /// <summary>
  26. /// The texture.
  27. /// </summary>
  28. Texture2D texture;
  29. /// <summary>
  30. /// The QRCode detector.
  31. /// </summary>
  32. QRCodeDetector detector;
  33. /// <summary>
  34. /// The points.
  35. /// </summary>
  36. Mat points;
  37. /// <summary>
  38. /// The webcam texture to mat helper.
  39. /// </summary>
  40. WebCamTextureToMatHelper webCamTextureToMatHelper;
  41. /// <summary>
  42. /// The FPS monitor.
  43. /// </summary>
  44. FpsMonitor fpsMonitor;
  45. // Use this for initialization
  46. void Start ()
  47. {
  48. fpsMonitor = GetComponent<FpsMonitor> ();
  49. webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
  50. detector = new QRCodeDetector ();
  51. #if UNITY_ANDROID && !UNITY_EDITOR
  52. // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
  53. webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
  54. #endif
  55. webCamTextureToMatHelper.Initialize ();
  56. }
  57. /// <summary>
  58. /// Raises the web cam texture to mat helper initialized event.
  59. /// </summary>
  60. public void OnWebCamTextureToMatHelperInitialized ()
  61. {
  62. Debug.Log ("OnWebCamTextureToMatHelperInitialized");
  63. Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
  64. texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false);
  65. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  66. gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
  67. Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  68. if (fpsMonitor != null) {
  69. fpsMonitor.Add ("width", webCamTextureMat.width ().ToString ());
  70. fpsMonitor.Add ("height", webCamTextureMat.height ().ToString ());
  71. fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
  72. }
  73. float width = webCamTextureMat.width ();
  74. float height = webCamTextureMat.height ();
  75. float widthScale = (float)Screen.width / width;
  76. float heightScale = (float)Screen.height / height;
  77. if (widthScale < heightScale) {
  78. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  79. } else {
  80. Camera.main.orthographicSize = height / 2;
  81. }
  82. grayMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1);
  83. points = new Mat ();
  84. // if WebCamera is frontFaceing, flip Mat.
  85. if (webCamTextureToMatHelper.GetWebCamDevice ().isFrontFacing) {
  86. webCamTextureToMatHelper.flipHorizontal = true;
  87. }
  88. }
  89. /// <summary>
  90. /// Raises the web cam texture to mat helper disposed event.
  91. /// </summary>
  92. public void OnWebCamTextureToMatHelperDisposed ()
  93. {
  94. Debug.Log ("OnWebCamTextureToMatHelperDisposed");
  95. if (grayMat != null)
  96. grayMat.Dispose ();
  97. if (texture != null) {
  98. Texture2D.Destroy (texture);
  99. texture = null;
  100. }
  101. if (points != null)
  102. points.Dispose ();
  103. }
  104. /// <summary>
  105. /// Raises the web cam texture to mat helper error occurred event.
  106. /// </summary>
  107. /// <param name="errorCode">Error code.</param>
  108. public void OnWebCamTextureToMatHelperErrorOccurred (WebCamTextureToMatHelper.ErrorCode errorCode)
  109. {
  110. Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
  111. }
  112. // Update is called once per frame
  113. void Update ()
  114. {
  115. if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) {
  116. Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
  117. Imgproc.cvtColor (rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
  118. bool result = detector.detect (grayMat, points);
  119. if (result) {
  120. string decode_info = detector.decode (grayMat, points);
  121. // Debug.Log (decode_info);
  122. // Debug.Log (points.dump ());
  123. // draw QRCode contour.
  124. float[] points_arr = new float[8];
  125. points.get (0, 0, points_arr);
  126. Imgproc.line (rgbaMat, new Point (points_arr [0], points_arr [1]), new Point (points_arr [2], points_arr [3]), new Scalar (255, 0, 0, 255), 2);
  127. Imgproc.line (rgbaMat, new Point (points_arr [2], points_arr [3]), new Point (points_arr [4], points_arr [5]), new Scalar (255, 0, 0, 255), 2);
  128. Imgproc.line (rgbaMat, new Point (points_arr [4], points_arr [5]), new Point (points_arr [6], points_arr [7]), new Scalar (255, 0, 0, 255), 2);
  129. Imgproc.line (rgbaMat, new Point (points_arr [6], points_arr [7]), new Point (points_arr [0], points_arr [1]), new Scalar (255, 0, 0, 255), 2);
  130. Imgproc.putText (rgbaMat, "DECODE INFO: " + decode_info, new Point (5, grayMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  131. }
  132. Utils.fastMatToTexture2D (rgbaMat, texture);
  133. }
  134. }
  135. /// <summary>
  136. /// Raises the destroy event.
  137. /// </summary>
  138. void OnDestroy ()
  139. {
  140. webCamTextureToMatHelper.Dispose ();
  141. if (detector != null)
  142. detector.Dispose ();
  143. }
  144. /// <summary>
  145. /// Raises the back button click event.
  146. /// </summary>
  147. public void OnBackButtonClick ()
  148. {
  149. SceneManager.LoadScene ("OpenCVForUnityExample");
  150. }
  151. /// <summary>
  152. /// Raises the play button click event.
  153. /// </summary>
  154. public void OnPlayButtonClick ()
  155. {
  156. webCamTextureToMatHelper.Play ();
  157. }
  158. /// <summary>
  159. /// Raises the pause button click event.
  160. /// </summary>
  161. public void OnPauseButtonClick ()
  162. {
  163. webCamTextureToMatHelper.Pause ();
  164. }
  165. /// <summary>
  166. /// Raises the stop button click event.
  167. /// </summary>
  168. public void OnStopButtonClick ()
  169. {
  170. webCamTextureToMatHelper.Stop ();
  171. }
  172. /// <summary>
  173. /// Raises the change camera button click event.
  174. /// </summary>
  175. public void OnChangeCameraButtonClick ()
  176. {
  177. webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing ();
  178. }
  179. }
  180. }