iOS 原生掃描二維碼 ----掃描有效區(qū)域的問題

這幾天在寫RN插件的時(shí)候發(fā)現(xiàn)一很奇葩的問題,就是封裝原生掃描二維碼、條碼的插件,其中的問題是,在二維碼還沒有進(jìn)入掃描框的時(shí)候掃描結(jié)果就出來了,這樣給用戶的體驗(yàn)相當(dāng)?shù)牟缓?。于是乎,我就去網(wǎng)上找各種資料看看大家有沒有解決過類似的問題。

這里有為仁兄似乎已經(jīng)解決了這個問題http://www.2cto.com/kf/201411/356046.html里面解決上面的這個問題方法是可以有效的處理問題,但還是有些問題,而且這個方法是我無法理解為什么要那樣子去設(shè)置rectOfInterest的,大家可以試試便知。

下面我就介紹一種可以有效解決問題的方法,在AVCaptureMetadataOutput類里面的rectOfInterest屬性注釋里面寫著下面一段英文

/*! @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 的掃描范圍是整個可視范圍的,但有些需求可能需要指定掃描的區(qū)域,雖然我覺得這樣很沒有必要,因?yàn)檎麄€屏幕都可以掃又何必指定到某個框呢?但如果真的需要這么做可以設(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è)置,而需要在這個 AVCaptureInputPortFormatDescriptionDidChangeNotification 通知里設(shè)置,否則 metadataOutputRectOfInterestForRect: 轉(zhuǎn)換方法會返回 (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之后問題就有效的解決了,希望這個能幫助到同在此坑跌倒的同志們。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容