QRCodeDetectorExample.cs 3.2 KB

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