這幾天在寫(xiě)RN插件的時(shí)候發(fā)現(xiàn)一很奇葩的問(wèn)題荆残,就是封裝原生掃描二維碼、條碼的插件橄妆,其中的問(wèn)題是,在二維碼還沒(méi)有進(jìn)入掃描框的時(shí)候掃描結(jié)果就出來(lái)了祈坠,這樣給用戶的體驗(yàn)相當(dāng)?shù)牟缓煤δ搿S谑呛酰揖腿ゾW(wǎng)上找各種資料看看大家有沒(méi)有解決過(guò)類似的問(wèn)題赦拘。
這里有為仁兄似乎已經(jīng)解決了這個(gè)問(wèn)題http://www.2cto.com/kf/201411/356046.html里面解決上面的這個(gè)問(wèn)題方法是可以有效的處理問(wèn)題慌随,但還是有些問(wèn)題,而且這個(gè)方法是我無(wú)法理解為什么要那樣子去設(shè)置rectOfInterest的躺同,大家可以試試便知阁猜。
下面我就介紹一種可以有效解決問(wèn)題的方法,在AVCaptureMetadataOutput類里面的rectOfInterest屬性注釋里面寫(xiě)著下面一段英文
/*! @propertyrectOfInterest @abstract? ? Specifies a rectangleofinterestforlimitingthesearch areaforvisual metadata. @discussion? ? The valueofthispropertyisa CGRectthatdeterminesthereceiver's rectangleofinterestforeach frameofvideo. The rectangle's originistop leftandisrelativetothecoordinatespaceofthedevice providingthemetadata. Specifying a rectOfInterest may improve detection performanceforcertain typesofmetadata. The default valueofthispropertyisthevalue CGRectMake(0,0,1,1). Metadata objectswhosebounds donotintersectwiththerectOfInterest willnotbe returned. */
掃碼時(shí) previewLayer 的掃描范圍是整個(gè)可視范圍的蹋艺,但有些需求可能需要指定掃描的區(qū)域剃袍,雖然我覺(jué)得這樣很沒(méi)有必要,因?yàn)檎麄€(gè)屏幕都可以掃又何必指定到某個(gè)框呢捎谨?但如果真的需要這么做可以設(shè)定 metadataOutput 的 rectOfInterest民效。
1)rectOfInterest 的值比較特別,需要進(jìn)行轉(zhuǎn)化涛救。它的默認(rèn)值是 (0.0, 0.0, 1.0, 1.0)畏邢。
metadataOutput.rectOfInterest=[previewLayer metadataOutputRectOfInterestForRect:CGRectMake(80, 80, 160, 160)]; // 假設(shè)掃碼框的 Rect 是 (80, 80, 160, 160)
2) rectOfInterest 不可以直接在設(shè)置 metadataOutput 時(shí)接著設(shè)置,而需要在這個(gè) AVCaptureInputPortFormatDescriptionDidChangeNotification 通知里設(shè)置州叠,否則 metadataOutputRectOfInterestForRect: 轉(zhuǎn)換方法會(huì)返回 (0, 0, 0, 0)棵红。
[[NSNotificationCenterdefaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? object:nilqueue:[NSOperationQueue currentQueue]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? usingBlock: ^(NSNotification*_Nonnull note) {? ? ? metadataOutput.rectOfInterest= [previewLayer metadataOutputRectOfInterestForRect:CGRectMake(80,80,160,160)];}];
當(dāng)如此設(shè)置完rectOfInterest之后問(wèn)題就有效的解決了,希望這個(gè)能幫助到同在此坑跌倒的同志們咧栗。