TBScanViewController.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // TBScanViewController.h
  3. // TBScanSDK
  4. //
  5. // Created by Tom on 15/7/24.
  6. // Copyright (c) 2015 Taobao.com. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import <Foundation/Foundation.h>
  10. #import <TBDecodeSDK/tbItf.h>
  11. #import <TBScanSDK/TBScanMacros.h>
  12. extern NSString * const TBScanVCInitConfigMultipleQueueStartCamera;
  13. extern NSString * const TBScanVCInitConfigSubClassDelegate;
  14. @class TBScanResult;
  15. @class TBScanPlugin;
  16. @class AVCaptureVideoPreviewLayer;
  17. enum{
  18. TorchState_On = 1,//闪光灯开
  19. TorchState_Off,//闪光灯关闭
  20. TorchState_Disable,//没有闪光灯(ipad,ipod)
  21. };
  22. typedef int TorchState;// 闪光灯状态
  23. // 扫码SDK会弹出的UIAlertView的tag
  24. enum{
  25. AlertType_LocalPhoto = 1688,
  26. AlertType_CameraDisable,
  27. };
  28. typedef int AlertType;
  29. enum{
  30. ImageType_DidFind = 1000,
  31. ImageType_AIDetectedQR
  32. };
  33. typedef int ImageType;
  34. @protocol TBScanViewControllerTorchDelegate <NSObject>
  35. /**
  36. 闪光灯提示
  37. 黑暗环境:如果闪光灯暗,显示闪光灯按钮,提示打开闪光灯; 如果闪光灯亮,显示闪光灯按钮,提示关闭
  38. 光线充足:如果闪光灯亮,显示闪光灯按钮,提示关闭; 如果闪光灯暗, 不显示
  39. @param yesOrNo 是否要展示手电筒
  40. @param yesOrNo 打开或关闭闪光灯
  41. */
  42. - (void)showTorchView:(BOOL)yesOrNo shouldTurnOn:(BOOL)shouldTurnOn;
  43. /**
  44. *
  45. * @discussion 闪光灯初始化、状态变更都会调用这个方法,可以在这里实现闪光灯的UI
  46. *
  47. * @param bState 闪光灯当前的状态,
  48. *
  49. */
  50. -(void)setTorchState:(TorchState)bState;
  51. @end
  52. @protocol TBScanViewControllerDelegate <NSObject>
  53. @required
  54. /**
  55. *
  56. * @discussion 条码,快递单,二维码等扫描成功会回调这个函数
  57. *
  58. * @param result 扫描成功的结果,具体参见TBScanResult类
  59. *
  60. */
  61. -(void)didFind:(NSArray<TBScanResult*>*)resultArray;
  62. @optional
  63. /**
  64. 摄像头没有访问权限时会调用该方法
  65. */
  66. -(void)cameraPermissionDenied;
  67. /**
  68. *
  69. * @discussion 摄像头启动后的回调
  70. *
  71. */
  72. -(void)cameraDidStart;
  73. /**
  74. 收到摄像头第一帧的回调
  75. */
  76. -(void)frameDidReceived;
  77. /**
  78. *
  79. * @discussion 摄像头启动失败
  80. *
  81. */
  82. -(void)cameraStartFail;
  83. /**
  84. 因为收到内存警告,而释放摄像头
  85. */
  86. -(void)releaseByMemoryWarning;
  87. /**
  88. *
  89. * @discussion 识别本地相册图片失败时会调用该方法
  90. *
  91. */
  92. -(void)scanPhotoFailed;
  93. /////////////////////////// 新特性 ////////////////////////////
  94. /**
  95. * bStableStatusMonitorEnable 为 YES 时会回调此方法
  96. * @discussion 机身稳定状态改变时的回调
  97. *
  98. * @param isStable YES 为变稳,NO 为变不稳,
  99. * @param scanFrameCount 第几帧
  100. *
  101. */
  102. - (void)stableStatusChanged:(BOOL)isStable AtScanFrameCount:(NSInteger)scanFrameCount;
  103. /**
  104. 获取 AI 模型的路径,如果设置了 bAIQRImageDetectEnable 或 bAIQRCVDetectEnable 为 true, 会立即调用此方法
  105. */
  106. - (NSURL *)mlmodelcURLForAIDetect;
  107. /**
  108. AI 模型在第几帧探测到了二维码
  109. */
  110. -(void)detectedQRByAIAtScanFrameCount:(NSInteger)frmaeCount;
  111. ///////////////////////////////埋点接口//////////////////////////////
  112. /**
  113. *
  114. * @discussion 用接入方自己的埋点接口实现
  115. *
  116. */
  117. -(void)userTrack:(NSString*)name;
  118. /**
  119. *
  120. * @discussion 用接入方的埋点接口实现,可以上传数据
  121. *
  122. */
  123. -(void)userTrack:(NSString*)name args:(NSDictionary*)data;
  124. /**
  125. Alipay用于关键路径记录,目前主要用于研究主线程切换耗时
  126. */
  127. -(void)alipayLog:(NSString*)content;
  128. /**
  129. 业务方处理扫描结果的机会
  130. @param > 真实识别结果
  131. @return 处理结果继续走流程
  132. */
  133. -(NSArray<TBScanResult*>*)handleResultArray:(NSArray<TBScanResult*>*)resultArray;
  134. /**
  135. 如果 needSaveFrameASImageWhenDidFind 或 needSaveFrameASImageWhenAIDetectedQR 为ture ,这可以获取帧转化的图像
  136. */
  137. -(void)handleImage:(UIImage *)image WithImageType:(ImageType)imageType;
  138. ///////////////////////////////iPad相关回调/////////////////////////
  139. /**
  140. *
  141. * @discussion iPad识别本地相册图片失败时会调用该方法
  142. *
  143. */
  144. -(void)scanPadPhotoFailed;
  145. /**
  146. * @discussion popover 消失通知
  147. */
  148. -(void)dismissPopover;
  149. ////////////////////////////////定制
  150. /**
  151. 自定义界面
  152. */
  153. -(void)buildContainerView:(UIView*)containerView;
  154. /**
  155. 自定义相册
  156. */
  157. -(void)buildCustomAlbum;
  158. /**
  159. 自定义相机权限框
  160. */
  161. -(void)buildCustomCameraPermissionAlert;
  162. @end
  163. @interface TBScanViewController : UIViewController <UIAlertViewDelegate>
  164. - (instancetype)initWithConfig:(NSDictionary *)config;
  165. @property (nonatomic,weak) id<TBScanViewControllerDelegate> delegate;
  166. @property (nonatomic,weak) id<TBScanViewControllerTorchDelegate> torchDelegate;
  167. /**
  168. *
  169. * @discussion scanType 是一个整型,可以用该值来确定要支持的扫码类型,可以用按位或的方式来支持多个类型,详细见ScanType定义。
  170. * 推荐使用最小类型,扫码类型越少识别速度越快,比如只支持条形码则使用ScanType_Barcode,只支持QR码则使用ScanType_QRCode,只支持
  171. * 条形码和QR码则使用ScanType_Barcode|ScanType_QRCode。请谨慎使用ScanType_All_Code类型,该类型中包含一些不常用的扫码类型。
  172. *
  173. */
  174. @property (nonatomic,assign) ScanType scanType;
  175. /**
  176. *
  177. * @discussion rectOfInterest 设置底层解码时的裁剪区域,如果不设置,SDK会默认一个裁剪区域
  178. *
  179. */
  180. @property (nonatomic,assign) CGRect rectOfInterest;
  181. /**
  182. *
  183. * @discussion cameraWidthPercent 设置屏幕中默认显示摄像头内容的百分比,设值有效范围大约为0.4~1.0,默认为0.74
  184. *
  185. */
  186. @property (nonatomic,assign) CGFloat cameraWidthPercent;
  187. /**
  188. *
  189. * @discussion bPlaySound 设置识别成功后是否播放声音,YES播放,NO不播放,默认是YES
  190. *
  191. */
  192. @property (nonatomic,assign) BOOL bPlaySound;
  193. /**
  194. *
  195. * @discussion bPreciseScan 设置扫描模式,YES能扫描很难识别的条码二维码,但是会影响扫描速度,NO对很复杂的条码二维码识别不了,但是识别速度会提升,默认是NO
  196. *
  197. */
  198. @property (nonatomic,assign) BOOL bPreciseScan;
  199. /**
  200. *
  201. * @discussion bResumeScanAtViewAppear 设置viewAppear的时候是否自动恢复扫描,为NO时不恢复需要手动调用resumeScan来恢复,默认为YES
  202. *
  203. */
  204. @property (nonatomic,assign) BOOL bResumeScanAtViewAppear;
  205. /**
  206. 是否支持自动根据屏幕内疑似二维码size做放大缩小,默认是YES
  207. */
  208. @property (nonatomic,assign,setter=setBAutoZoomEnable:) BOOL bAutoZoomEnable; ///< 逐步淘汰掉这种以下面渐进式的为主
  209. /**
  210. 是否支持自动根据屏幕内疑似二维码 size 做渐进试的放大缩小,默认是YES
  211. */
  212. @property (nonatomic,assign) BOOL bGraduallyAutoZoomEnable;
  213. /**
  214. 对焦 near 优先模式是否启用
  215. */
  216. @property (nonatomic,assign) BOOL bAutoFocusRangeNearEnable;
  217. /**
  218. 为防止过早自动放大,默认前 35 帧不做自动放大,可调整
  219. */
  220. @property (nonatomic,assign) int autoZoomDelayFrameCount;
  221. /**
  222. 是否支持手势,默认YES
  223. */
  224. @property (nonatomic,assign) BOOL bGestureEnable;
  225. /**
  226. 是否监控扫码过程中机身稳定性, 默认 NO,为 YES 时会在状态变动时回调方法 - (void)stableStatusUpdate: AtScanCount:
  227. */
  228. @property (nonatomic,assign) BOOL bStableStatusMonitorEnable;
  229. /**
  230. 是否要在收到内存警告时释放相机,默认 NO
  231. */
  232. @property (nonatomic,assign) BOOL shouldReleaseByMemoryWarning;
  233. //////////////////////////////// 新特性 AI 相关 /////////////////////////////////////
  234. /**
  235. 动画框大小,用于做放大参考
  236. */
  237. @property (nonatomic,assign) CGRect animationRect;
  238. /**
  239. 提示打开闪光灯的阈值
  240. */
  241. @property (nonatomic,assign) CGFloat flashOnBrightnessThreshold;
  242. /**
  243. 提示关闭闪光灯的阈值
  244. */
  245. @property (nonatomic,assign) CGFloat flashOffBrightnessThreshold;
  246. /**
  247. 拍照时设置该值为true,不主动改变闪光灯的状态
  248. */
  249. @property (nonatomic,assign) BOOL bKeepTorchModeWhenPauseScan;
  250. /////////////////////// 保存帧图像 ////////////////
  251. /**
  252. 是否要保存 did find 那一帧图像, 图像会通过 delegate 异步线程回传 默认 NO
  253. */
  254. @property (nonatomic,assign) BOOL needSaveFrameASImageWhenDidFind;
  255. /**
  256. *
  257. * @discussion bImpactFeedBack 设置识别成功后是否短促震动,YES播放,NO不播放,默认是YES
  258. *
  259. */
  260. @property (nonatomic,assign) BOOL bImpactFeedBack;
  261. ////////////////////////////自定义UIAlertView的文案/////////////////////////////////
  262. // 无摄像头权限时的AlertView的title
  263. @property (nonatomic,strong) NSString *cameraPermissionDeniedTitle;
  264. // 无摄像头权限时的AlertView的提示信息
  265. @property (nonatomic,strong) NSString *cameraPermissionDeniedMsg;
  266. // 无摄像头权限时的AlertView取消按钮的title
  267. @property (nonatomic,strong) NSString *cameraPermissionDeniedCancelTitle;
  268. // 相册识别失败时的AlertView的提示信息
  269. @property (nonatomic,strong) NSString *scanPhotoFailedMsg;
  270. // 相册识别失败时AlertView取消按钮的title
  271. @property (nonatomic,strong) NSString *scanPhotoFailedCancelTitle;
  272. @property (nonatomic, assign) BOOL supportSaveColorfullImage; ///配置是否支持保存彩色图片
  273. /**
  274. *
  275. * @discussion 停止扫描并关闭摄像头,一般情况下不要直接调用该方法
  276. *
  277. */
  278. -(void)exitScan;
  279. /**
  280. *
  281. * @discussion 扫描本地图库,会调起本地相册
  282. *
  283. */
  284. -(void)scanPhotoLibrary;
  285. /**
  286. 从相册选择的流程
  287. */
  288. -(void)scanPhotoImage:(UIImage*)image;
  289. /**
  290. *
  291. * @discussion 点击闪光灯,会调用代理接口-(void)setTorchState:(TorchState)bState,在该代理接口中根据参数bState来判断当前闪光灯的状态从而实现对应的UI
  292. *
  293. */
  294. -(void)onTorch;
  295. /**
  296. * 返回闪光灯的状态
  297. */
  298. -(TorchState)torchMode;
  299. /**
  300. *
  301. * @discussion 暂停扫码
  302. *
  303. */
  304. -(void)pauseScan;
  305. /**
  306. *
  307. * @discussion 继续扫码
  308. *
  309. */
  310. -(void)resumeScan;
  311. /**
  312. *
  313. * @discussion 暂停扫码,并停止相机(预览停止)
  314. *
  315. */
  316. -(void)pauseCaptureSession;
  317. /**
  318. *
  319. * @discussion 继续扫码,并恢复相机
  320. *
  321. */
  322. -(void)resumeCaptureSession;
  323. /**
  324. *
  325. * @discussion 暂停扫,并停止预览,相机 Session 没断(便于 resume 快速开始,不必重启相机)
  326. *
  327. */
  328. -(void)pauseScanAndPreView;
  329. /**
  330. *
  331. * @discussion 继续扫,并同步预览
  332. *
  333. */
  334. -(void)resumeScanAndPreView;
  335. /**
  336. *
  337. * @discussion 针对预留ScanType可以注册自定义的Plugin
  338. *
  339. */
  340. -(void)registerPlugin:(TBScanPlugin*)inPlugin withType:(ScanType)type;
  341. /**
  342. *
  343. * @discussion 对底层算法做自定义配置
  344. *
  345. */
  346. - (void)startCameraWithConfig:(NSDictionary *)config;
  347. /**
  348. *
  349. * @discussion 获取底层算法解码过程详细信息, 如果没有则为 nil
  350. */
  351. - (NSDictionary *)getDecodeDetailInfo;
  352. ///////////////////////////////下面两个接口只在iPad下有效/////////////////////////
  353. /**
  354. *
  355. * @discussion 对iPad相册取图弹出框有定制要求的可以使用这个接口来替换通用的相册取图scanPhotoLibrary接口
  356. *
  357. * @param rect 指定相册取图弹出框的区域
  358. *
  359. * @param arrowDirections 指定相册取图弹出框的箭头方向
  360. *
  361. * @param arrowColor 指定相册取图弹出框的背景色,nil为白色
  362. *
  363. */
  364. -(void)scanPhotoLibrary:(CGRect)rect permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections with:(UIColor*)arrowColor;
  365. @end
  366. @interface TBScanViewController(CaptureManager)
  367. /**
  368. *
  369. * @discussion 判断摄像头是否支持该SessionPreset
  370. *
  371. */
  372. -(BOOL)canSetSession:(NSString *)sessionPreset;
  373. // 判断当前是否在使用前置摄像头
  374. -(BOOL)isUsingFrontCamera;
  375. -(void)setCameraCaptureFocusPoint:(CGPoint)point;
  376. -(void)updateCodeDetectStatus;
  377. @end