WeChatQRCode.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #if !UNITY_WSA_10_0
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.Wechat_qrcodeModule
  8. {
  9. // C++: class WeChatQRCode
  10. /**
  11. * WeChat QRCode includes two CNN-based models:
  12. * A object detection model and a super resolution model.
  13. * Object detection model is applied to detect QRCode with the bounding box.
  14. * super resolution model is applied to zoom in QRCode when it is small.
  15. *
  16. */
  17. public class WeChatQRCode : DisposableOpenCVObject
  18. {
  19. protected override void Dispose(bool disposing)
  20. {
  21. try
  22. {
  23. if (disposing)
  24. {
  25. }
  26. if (IsEnabledDispose)
  27. {
  28. if (nativeObj != IntPtr.Zero)
  29. wechat_1qrcode_WeChatQRCode_delete(nativeObj);
  30. nativeObj = IntPtr.Zero;
  31. }
  32. }
  33. finally
  34. {
  35. base.Dispose(disposing);
  36. }
  37. }
  38. protected internal WeChatQRCode(IntPtr addr) : base(addr) { }
  39. public IntPtr getNativeObjAddr() { return nativeObj; }
  40. // internal usage only
  41. public static WeChatQRCode __fromPtr__(IntPtr addr) { return new WeChatQRCode(addr); }
  42. //
  43. // C++: cv::wechat_qrcode::WeChatQRCode::WeChatQRCode(string detector_prototxt_path = "", string detector_caffe_model_path = "", string super_resolution_prototxt_path = "", string super_resolution_caffe_model_path = "")
  44. //
  45. /**
  46. * Initialize the WeChatQRCode.
  47. * It includes two models, which are packaged with caffe format.
  48. * Therefore, there are prototxt and caffe models (In total, four paramenters).
  49. *
  50. * param detector_prototxt_path prototxt file path for the detector
  51. * param detector_caffe_model_path caffe model file path for the detector
  52. * param super_resolution_prototxt_path prototxt file path for the super resolution model
  53. * param super_resolution_caffe_model_path caffe file path for the super resolution model
  54. */
  55. public WeChatQRCode(string detector_prototxt_path, string detector_caffe_model_path, string super_resolution_prototxt_path, string super_resolution_caffe_model_path)
  56. {
  57. nativeObj = DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_WeChatQRCode_10(detector_prototxt_path, detector_caffe_model_path, super_resolution_prototxt_path, super_resolution_caffe_model_path));
  58. }
  59. /**
  60. * Initialize the WeChatQRCode.
  61. * It includes two models, which are packaged with caffe format.
  62. * Therefore, there are prototxt and caffe models (In total, four paramenters).
  63. *
  64. * param detector_prototxt_path prototxt file path for the detector
  65. * param detector_caffe_model_path caffe model file path for the detector
  66. * param super_resolution_prototxt_path prototxt file path for the super resolution model
  67. */
  68. public WeChatQRCode(string detector_prototxt_path, string detector_caffe_model_path, string super_resolution_prototxt_path)
  69. {
  70. nativeObj = DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_WeChatQRCode_11(detector_prototxt_path, detector_caffe_model_path, super_resolution_prototxt_path));
  71. }
  72. /**
  73. * Initialize the WeChatQRCode.
  74. * It includes two models, which are packaged with caffe format.
  75. * Therefore, there are prototxt and caffe models (In total, four paramenters).
  76. *
  77. * param detector_prototxt_path prototxt file path for the detector
  78. * param detector_caffe_model_path caffe model file path for the detector
  79. */
  80. public WeChatQRCode(string detector_prototxt_path, string detector_caffe_model_path)
  81. {
  82. nativeObj = DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_WeChatQRCode_12(detector_prototxt_path, detector_caffe_model_path));
  83. }
  84. /**
  85. * Initialize the WeChatQRCode.
  86. * It includes two models, which are packaged with caffe format.
  87. * Therefore, there are prototxt and caffe models (In total, four paramenters).
  88. *
  89. * param detector_prototxt_path prototxt file path for the detector
  90. */
  91. public WeChatQRCode(string detector_prototxt_path)
  92. {
  93. nativeObj = DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_WeChatQRCode_13(detector_prototxt_path));
  94. }
  95. /**
  96. * Initialize the WeChatQRCode.
  97. * It includes two models, which are packaged with caffe format.
  98. * Therefore, there are prototxt and caffe models (In total, four paramenters).
  99. *
  100. */
  101. public WeChatQRCode()
  102. {
  103. nativeObj = DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_WeChatQRCode_14());
  104. }
  105. //
  106. // C++: vector_string cv::wechat_qrcode::WeChatQRCode::detectAndDecode(Mat img, vector_Mat& points = vector_Mat())
  107. //
  108. /**
  109. * Both detects and decodes QR code.
  110. * To simplify the usage, there is a only API: detectAndDecode
  111. *
  112. * param img supports grayscale or color (BGR) image.
  113. * param points optional output array of vertices of the found QR code quadrangle. Will be
  114. * empty if not found.
  115. * return list of decoded string.
  116. */
  117. public List<string> detectAndDecode(Mat img, List<Mat> points)
  118. {
  119. ThrowIfDisposed();
  120. if (img != null) img.ThrowIfDisposed();
  121. Mat points_mat = new Mat();
  122. List<string> retVal = new List<string>();
  123. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_detectAndDecode_10(nativeObj, img.nativeObj, points_mat.nativeObj)));
  124. Converters.Mat_to_vector_Mat(points_mat, points);
  125. points_mat.release();
  126. Converters.Mat_to_vector_string(retValMat, retVal);
  127. return retVal;
  128. }
  129. /**
  130. * Both detects and decodes QR code.
  131. * To simplify the usage, there is a only API: detectAndDecode
  132. *
  133. * param img supports grayscale or color (BGR) image.
  134. * empty if not found.
  135. * return list of decoded string.
  136. */
  137. public List<string> detectAndDecode(Mat img)
  138. {
  139. ThrowIfDisposed();
  140. if (img != null) img.ThrowIfDisposed();
  141. List<string> retVal = new List<string>();
  142. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(wechat_1qrcode_WeChatQRCode_detectAndDecode_11(nativeObj, img.nativeObj)));
  143. Converters.Mat_to_vector_string(retValMat, retVal);
  144. return retVal;
  145. }
  146. //
  147. // C++: void cv::wechat_qrcode::WeChatQRCode::setScaleFactor(float _scalingFactor)
  148. //
  149. /**
  150. * set scale factor
  151. * QR code detector use neural network to detect QR.
  152. * Before running the neural network, the input image is pre-processed by scaling.
  153. * By default, the input image is scaled to an image with an area of 160000 pixels.
  154. * The scale factor allows to use custom scale the input image:
  155. * width = scaleFactor*width
  156. * height = scaleFactor*width
  157. *
  158. * scaleFactor valuse must be &gt; 0 and &lt;= 1, otherwise the scaleFactor value is set to -1
  159. * and use default scaled to an image with an area of 160000 pixels.
  160. * param _scalingFactor automatically generated
  161. */
  162. public void setScaleFactor(float _scalingFactor)
  163. {
  164. ThrowIfDisposed();
  165. wechat_1qrcode_WeChatQRCode_setScaleFactor_10(nativeObj, _scalingFactor);
  166. }
  167. //
  168. // C++: float cv::wechat_qrcode::WeChatQRCode::getScaleFactor()
  169. //
  170. public float getScaleFactor()
  171. {
  172. ThrowIfDisposed();
  173. return wechat_1qrcode_WeChatQRCode_getScaleFactor_10(nativeObj);
  174. }
  175. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  176. const string LIBNAME = "__Internal";
  177. #else
  178. const string LIBNAME = "opencvforunity";
  179. #endif
  180. // C++: cv::wechat_qrcode::WeChatQRCode::WeChatQRCode(string detector_prototxt_path = "", string detector_caffe_model_path = "", string super_resolution_prototxt_path = "", string super_resolution_caffe_model_path = "")
  181. [DllImport(LIBNAME)]
  182. private static extern IntPtr wechat_1qrcode_WeChatQRCode_WeChatQRCode_10(string detector_prototxt_path, string detector_caffe_model_path, string super_resolution_prototxt_path, string super_resolution_caffe_model_path);
  183. [DllImport(LIBNAME)]
  184. private static extern IntPtr wechat_1qrcode_WeChatQRCode_WeChatQRCode_11(string detector_prototxt_path, string detector_caffe_model_path, string super_resolution_prototxt_path);
  185. [DllImport(LIBNAME)]
  186. private static extern IntPtr wechat_1qrcode_WeChatQRCode_WeChatQRCode_12(string detector_prototxt_path, string detector_caffe_model_path);
  187. [DllImport(LIBNAME)]
  188. private static extern IntPtr wechat_1qrcode_WeChatQRCode_WeChatQRCode_13(string detector_prototxt_path);
  189. [DllImport(LIBNAME)]
  190. private static extern IntPtr wechat_1qrcode_WeChatQRCode_WeChatQRCode_14();
  191. // C++: vector_string cv::wechat_qrcode::WeChatQRCode::detectAndDecode(Mat img, vector_Mat& points = vector_Mat())
  192. [DllImport(LIBNAME)]
  193. private static extern IntPtr wechat_1qrcode_WeChatQRCode_detectAndDecode_10(IntPtr nativeObj, IntPtr img_nativeObj, IntPtr points_mat_nativeObj);
  194. [DllImport(LIBNAME)]
  195. private static extern IntPtr wechat_1qrcode_WeChatQRCode_detectAndDecode_11(IntPtr nativeObj, IntPtr img_nativeObj);
  196. // C++: void cv::wechat_qrcode::WeChatQRCode::setScaleFactor(float _scalingFactor)
  197. [DllImport(LIBNAME)]
  198. private static extern void wechat_1qrcode_WeChatQRCode_setScaleFactor_10(IntPtr nativeObj, float _scalingFactor);
  199. // C++: float cv::wechat_qrcode::WeChatQRCode::getScaleFactor()
  200. [DllImport(LIBNAME)]
  201. private static extern float wechat_1qrcode_WeChatQRCode_getScaleFactor_10(IntPtr nativeObj);
  202. // native support for java finalize()
  203. [DllImport(LIBNAME)]
  204. private static extern void wechat_1qrcode_WeChatQRCode_delete(IntPtr nativeObj);
  205. }
  206. }
  207. #endif