QRCodeDetectorExample.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.ImgprocModule;
  3. using OpenCVForUnity.ObjdetectModule;
  4. using OpenCVForUnity.UnityUtils;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace OpenCVForUnityExample
  9. {
  10. /// <summary>
  11. /// QRCodeDetector Example
  12. /// An example of QRCode detection using the QRCodeDetector class.
  13. /// https://github.com/opencv/opencv/blob/master/samples/cpp/qrcode.cpp
  14. /// </summary>
  15. public class QRCodeDetectorExample : MonoBehaviour
  16. {
  17. // Use this for initialization
  18. void Start()
  19. {
  20. Run();
  21. }
  22. private void Run()
  23. {
  24. Texture2D imgTexture = Resources.Load("link_github_ocv") as Texture2D;
  25. Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  26. Utils.texture2DToMat(imgTexture, imgMat);
  27. Debug.Log("imgMat.ToString() " + imgMat.ToString());
  28. Mat grayMat = new Mat();
  29. Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
  30. Mat points = new Mat();
  31. List<string> decodedInfo = new List<string>();
  32. List<Mat> straightQrcode = new List<Mat>();
  33. QRCodeDetector detector = new QRCodeDetector();
  34. bool result = detector.detectAndDecodeMulti(grayMat, decodedInfo, points, straightQrcode);
  35. if (result)
  36. {
  37. //Debug.Log(points.dump());
  38. //Debug.Log(points.ToString());
  39. for (int i = 0; i < points.rows(); i++)
  40. {
  41. //Debug.Log(decoded_info[i]);
  42. //Debug.Log(straight_qrcode[i].dump());
  43. //// draw QRCode contour.
  44. float[] points_arr = new float[8];
  45. points.get(i, 0, points_arr);
  46. Imgproc.line(imgMat, new Point(points_arr[0], points_arr[1]), new Point(points_arr[2], points_arr[3]), new Scalar(255, 0, 0, 255), 2);
  47. Imgproc.line(imgMat, new Point(points_arr[2], points_arr[3]), new Point(points_arr[4], points_arr[5]), new Scalar(255, 0, 0, 255), 2);
  48. Imgproc.line(imgMat, new Point(points_arr[4], points_arr[5]), new Point(points_arr[6], points_arr[7]), new Scalar(255, 0, 0, 255), 2);
  49. Imgproc.line(imgMat, new Point(points_arr[6], points_arr[7]), new Point(points_arr[0], points_arr[1]), new Scalar(255, 0, 0, 255), 2);
  50. if (decodedInfo.Count > i && decodedInfo[i] != null)
  51. Imgproc.putText(imgMat, decodedInfo[i], new Point(points_arr[0], points_arr[1]), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  52. }
  53. }
  54. else
  55. {
  56. Imgproc.putText(imgMat, "Decoding failed.", new Point(5, imgMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  57. }
  58. Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
  59. Utils.matToTexture2D(imgMat, texture);
  60. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  61. }
  62. // Update is called once per frame
  63. void Update()
  64. {
  65. }
  66. /// <summary>
  67. /// Raises the destroy event.
  68. /// </summary>
  69. void OnDestroy()
  70. {
  71. }
  72. /// <summary>
  73. /// Raises the back button click event.
  74. /// </summary>
  75. public void OnBackButtonClick()
  76. {
  77. SceneManager.LoadScene("OpenCVForUnityExample");
  78. }
  79. }
  80. }