BarcodeDetectorExample.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #if !UNITY_WSA_10_0
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.ImgprocModule;
  4. using OpenCVForUnity.ObjdetectModule;
  5. using OpenCVForUnity.UnityUtils;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. namespace OpenCVForUnityExample
  10. {
  11. /// <summary>
  12. /// BarcodeDetector Example
  13. /// An example of Barcode detection using the BarcodeDetector class.
  14. /// </summary>
  15. public class BarcodeDetectorExample : MonoBehaviour
  16. {
  17. // Use this for initialization
  18. void Start()
  19. {
  20. Run();
  21. }
  22. private void Run()
  23. {
  24. Texture2D imgTexture = Resources.Load("book") 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. BarcodeDetector detector = new BarcodeDetector();
  29. // When using super resolution.
  30. // Please, download 'sr.*' from https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode and put them into the StreamingAssets/OpenCVForUnity/barcode directory.
  31. //BarcodeDetector detector = new BarcodeDetector(Utils.getFilePath("OpenCVForUnity/barcode/sr.prototxt"), Utils.getFilePath("OpenCVForUnity/barcode/sr.caffemodel"));
  32. List<string> decoded_info = new List<string>();
  33. List<string> decoded_type = new List<string>();
  34. Mat points = new Mat();
  35. bool result = detector.detectAndDecodeWithType(imgMat, decoded_info, decoded_type, points);
  36. if (result)
  37. {
  38. for (int i = 0; i < decoded_info.Count; i++)
  39. {
  40. Debug.Log("decoded_info[" + i + "] " + decoded_info[i]);
  41. }
  42. for (int i = 0; i < decoded_type.Count; i++)
  43. {
  44. Debug.Log("decoded_type[" + i + "] " + decoded_type[i]);
  45. }
  46. Debug.Log("points.dump() " + points.dump());
  47. // draw Barcode contour.
  48. float[] points_arr = new float[8];
  49. points.get(0, 0, points_arr);
  50. 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);
  51. 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);
  52. 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);
  53. 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);
  54. Imgproc.putText(imgMat, "DECODED INFO[0]: " + decoded_info[0], new Point(5, imgMat.rows() - 35), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  55. Imgproc.putText(imgMat, "DECODED TYPE[0]: " + decoded_type[0], new Point(5, imgMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  56. }
  57. Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
  58. Utils.matToTexture2D(imgMat, texture);
  59. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  60. }
  61. // Update is called once per frame
  62. void Update()
  63. {
  64. }
  65. /// <summary>
  66. /// Raises the destroy event.
  67. /// </summary>
  68. void OnDestroy()
  69. {
  70. }
  71. /// <summary>
  72. /// Raises the back button click event.
  73. /// </summary>
  74. public void OnBackButtonClick()
  75. {
  76. SceneManager.LoadScene("OpenCVForUnityExample");
  77. }
  78. }
  79. }
  80. #endif