BarcodeDetector.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.ObjdetectModule
  7. {
  8. // C++: class BarcodeDetector
  9. public class BarcodeDetector : GraphicalCodeDetector
  10. {
  11. protected override void Dispose(bool disposing)
  12. {
  13. try
  14. {
  15. if (disposing)
  16. {
  17. }
  18. if (IsEnabledDispose)
  19. {
  20. if (nativeObj != IntPtr.Zero)
  21. objdetect_BarcodeDetector_delete(nativeObj);
  22. nativeObj = IntPtr.Zero;
  23. }
  24. }
  25. finally
  26. {
  27. base.Dispose(disposing);
  28. }
  29. }
  30. protected internal BarcodeDetector(IntPtr addr) : base(addr) { }
  31. // internal usage only
  32. public static new BarcodeDetector __fromPtr__(IntPtr addr) { return new BarcodeDetector(addr); }
  33. //
  34. // C++: cv::barcode::BarcodeDetector::BarcodeDetector()
  35. //
  36. /**
  37. * Initialize the BarcodeDetector.
  38. */
  39. public BarcodeDetector() :
  40. base(DisposableObject.ThrowIfNullIntPtr(objdetect_BarcodeDetector_BarcodeDetector_10()))
  41. {
  42. }
  43. //
  44. // C++: cv::barcode::BarcodeDetector::BarcodeDetector(string prototxt_path, string model_path)
  45. //
  46. /**
  47. * Initialize the BarcodeDetector.
  48. *
  49. * Parameters allow to load _optional_ Super Resolution DNN model for better quality.
  50. * param prototxt_path prototxt file path for the super resolution model
  51. * param model_path model file path for the super resolution model
  52. */
  53. public BarcodeDetector(string prototxt_path, string model_path) :
  54. base(DisposableObject.ThrowIfNullIntPtr(objdetect_BarcodeDetector_BarcodeDetector_11(prototxt_path, model_path)))
  55. {
  56. }
  57. //
  58. // C++: bool cv::barcode::BarcodeDetector::decodeWithType(Mat img, Mat points, vector_string& decoded_info, vector_string& decoded_type)
  59. //
  60. /**
  61. * Decodes barcode in image once it's found by the detect() method.
  62. *
  63. * param img grayscale or color (BGR) image containing bar code.
  64. * param points vector of rotated rectangle vertices found by detect() method (or some other algorithm).
  65. * For N detected barcodes, the dimensions of this array should be [N][4].
  66. * Order of four points in vector<Point2f> is bottomLeft, topLeft, topRight, bottomRight.
  67. * param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
  68. * param decoded_type vector strings, specifies the type of these barcodes
  69. * return true if at least one valid barcode have been found
  70. */
  71. public bool decodeWithType(Mat img, Mat points, List<string> decoded_info, List<string> decoded_type)
  72. {
  73. ThrowIfDisposed();
  74. if (img != null) img.ThrowIfDisposed();
  75. if (points != null) points.ThrowIfDisposed();
  76. Mat decoded_info_mat = new Mat();
  77. Mat decoded_type_mat = new Mat();
  78. bool retVal = objdetect_BarcodeDetector_decodeWithType_10(nativeObj, img.nativeObj, points.nativeObj, decoded_info_mat.nativeObj, decoded_type_mat.nativeObj);
  79. Converters.Mat_to_vector_string(decoded_info_mat, decoded_info);
  80. decoded_info_mat.release();
  81. Converters.Mat_to_vector_string(decoded_type_mat, decoded_type);
  82. decoded_type_mat.release();
  83. return retVal;
  84. }
  85. //
  86. // C++: bool cv::barcode::BarcodeDetector::detectAndDecodeWithType(Mat img, vector_string& decoded_info, vector_string& decoded_type, Mat& points = Mat())
  87. //
  88. /**
  89. * Both detects and decodes barcode
  90. *
  91. * param img grayscale or color (BGR) image containing barcode.
  92. * param decoded_info UTF8-encoded output vector of string(s) or empty vector of string if the codes cannot be decoded.
  93. * param decoded_type vector of strings, specifies the type of these barcodes
  94. * param points optional output vector of vertices of the found barcode rectangle. Will be empty if not found.
  95. * return true if at least one valid barcode have been found
  96. */
  97. public bool detectAndDecodeWithType(Mat img, List<string> decoded_info, List<string> decoded_type, Mat points)
  98. {
  99. ThrowIfDisposed();
  100. if (img != null) img.ThrowIfDisposed();
  101. if (points != null) points.ThrowIfDisposed();
  102. Mat decoded_info_mat = new Mat();
  103. Mat decoded_type_mat = new Mat();
  104. bool retVal = objdetect_BarcodeDetector_detectAndDecodeWithType_10(nativeObj, img.nativeObj, decoded_info_mat.nativeObj, decoded_type_mat.nativeObj, points.nativeObj);
  105. Converters.Mat_to_vector_string(decoded_info_mat, decoded_info);
  106. decoded_info_mat.release();
  107. Converters.Mat_to_vector_string(decoded_type_mat, decoded_type);
  108. decoded_type_mat.release();
  109. return retVal;
  110. }
  111. /**
  112. * Both detects and decodes barcode
  113. *
  114. * param img grayscale or color (BGR) image containing barcode.
  115. * param decoded_info UTF8-encoded output vector of string(s) or empty vector of string if the codes cannot be decoded.
  116. * param decoded_type vector of strings, specifies the type of these barcodes
  117. * return true if at least one valid barcode have been found
  118. */
  119. public bool detectAndDecodeWithType(Mat img, List<string> decoded_info, List<string> decoded_type)
  120. {
  121. ThrowIfDisposed();
  122. if (img != null) img.ThrowIfDisposed();
  123. Mat decoded_info_mat = new Mat();
  124. Mat decoded_type_mat = new Mat();
  125. bool retVal = objdetect_BarcodeDetector_detectAndDecodeWithType_11(nativeObj, img.nativeObj, decoded_info_mat.nativeObj, decoded_type_mat.nativeObj);
  126. Converters.Mat_to_vector_string(decoded_info_mat, decoded_info);
  127. decoded_info_mat.release();
  128. Converters.Mat_to_vector_string(decoded_type_mat, decoded_type);
  129. decoded_type_mat.release();
  130. return retVal;
  131. }
  132. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  133. const string LIBNAME = "__Internal";
  134. #else
  135. const string LIBNAME = "opencvforunity";
  136. #endif
  137. // C++: cv::barcode::BarcodeDetector::BarcodeDetector()
  138. [DllImport(LIBNAME)]
  139. private static extern IntPtr objdetect_BarcodeDetector_BarcodeDetector_10();
  140. // C++: cv::barcode::BarcodeDetector::BarcodeDetector(string prototxt_path, string model_path)
  141. [DllImport(LIBNAME)]
  142. private static extern IntPtr objdetect_BarcodeDetector_BarcodeDetector_11(string prototxt_path, string model_path);
  143. // C++: bool cv::barcode::BarcodeDetector::decodeWithType(Mat img, Mat points, vector_string& decoded_info, vector_string& decoded_type)
  144. [DllImport(LIBNAME)]
  145. [return: MarshalAs(UnmanagedType.U1)]
  146. private static extern bool objdetect_BarcodeDetector_decodeWithType_10(IntPtr nativeObj, IntPtr img_nativeObj, IntPtr points_nativeObj, IntPtr decoded_info_mat_nativeObj, IntPtr decoded_type_mat_nativeObj);
  147. // C++: bool cv::barcode::BarcodeDetector::detectAndDecodeWithType(Mat img, vector_string& decoded_info, vector_string& decoded_type, Mat& points = Mat())
  148. [DllImport(LIBNAME)]
  149. [return: MarshalAs(UnmanagedType.U1)]
  150. private static extern bool objdetect_BarcodeDetector_detectAndDecodeWithType_10(IntPtr nativeObj, IntPtr img_nativeObj, IntPtr decoded_info_mat_nativeObj, IntPtr decoded_type_mat_nativeObj, IntPtr points_nativeObj);
  151. [DllImport(LIBNAME)]
  152. [return: MarshalAs(UnmanagedType.U1)]
  153. private static extern bool objdetect_BarcodeDetector_detectAndDecodeWithType_11(IntPtr nativeObj, IntPtr img_nativeObj, IntPtr decoded_info_mat_nativeObj, IntPtr decoded_type_mat_nativeObj);
  154. // native support for java finalize()
  155. [DllImport(LIBNAME)]
  156. private static extern void objdetect_BarcodeDetector_delete(IntPtr nativeObj);
  157. }
  158. }