zoukankan      html  css  js  c++  java
  • 苹果原生二维码扫描功能——可限制扫描区域

    使用原生的好处就是扫描特别快效率特别高,使用  AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围。(默认的是全屏扫描)

    首先是要用到的几个类

    @property ( strong , nonatomic ) AVCaptureDevice * device;

    @property ( strong , nonatomic ) AVCaptureDeviceInput * input;

    @property ( strong , nonatomic ) AVCaptureMetadataOutput * output;

    @property ( strong , nonatomic ) AVCaptureSession * session;

    @property ( strong , nonatomic ) AVCaptureVideoPreviewLayer * preview;

    下面分别创建他们

    // Device

    _device = [ AVCaptureDevice defaultDeviceWithMediaType : AVMediaTypeVideo ];

    // Input

    _input = [ AVCaptureDeviceInput deviceInputWithDevice : self . device error : nil ];

    // Output

    _output = [[ AVCaptureMetadataOutput alloc ] init ];

    [ _output setMetadataObjectsDelegate : self queue : dispatch_get_main_queue ()];

    // Session

    _session = [[ AVCaptureSession alloc ] init ];

    [ _session setSessionPreset : AVCaptureSessionPresetHigh ];

    if ([ _session canAddInput : self . input ])

    {

    [ _session addInput : self . input ];

    }

    if ([ _session canAddOutput : self . output ])

    {

    [ _session addOutput : self . output ];

    }

    // 条码类型 AVMetadataObjectTypeQRCode

    _output . metadataObjectTypes = @[ AVMetadataObjectTypeQRCode ] ;

    // Preview

    _preview =[ AVCaptureVideoPreviewLayer layerWithSession : _session ];

    _preview . videoGravity = AVLayerVideoGravityResizeAspectFill ;

    _preview . frame = self . view . layer . bounds ;

    [ self . view . layer insertSublayer : _preview atIndex : 0 ];

    // Start

    [ _session startRunning ];

    然后实现 AVCaptureMetadataOutputObjectsDelegate

    #pragma mark AVCaptureMetadataOutputObjectsDelegate

    - ( void )captureOutput:( AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:( AVCaptureConnection *)connection

    {

    NSString *stringValue;

    if ([metadataObjects count ] > 0 )

    {

    // 停止扫描

    [ _session stopRunning ];        

    AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];

    stringValue = metadataObject. stringValue ;        

    }

    }

    到此为止就可以成功扫描二维码了,但是有个尴尬的问题,这时的扫描是全屏扫描的。

    一般情况下项目中的扫描页面是这样的,但是当你扫描的时候会发现在二维码还没进入中心的那个小方块时,就已经成功扫描完成了,这对于体验来说很不好。但是由于那时候赶项目就没有时间优化。终于今天抽出来时间了。

    我从早上上班开始一直搞到下午,把所有想到的方法都试了一遍,但是都不行(都是泪),最后将要放弃的时候发现了一个比较可疑的点。

    @property ( nonatomic ) CGRect rectOfInterest NS_AVAILABLE_IOS ( 7 _0);

    这是的  AVCaptureMetadataOutput 一个属性,它的解释是

    @discussion

    The value of this property is a CGRect that determines the receiver's rectangle of interest for each frame of video.  

    The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata.  Specifying 

    a rectOfInterest may improve detection performance for certain types of metadata. The default value of this property is the 

    value CGRectMake(0, 0, 1, 1).  Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.

    大概意思就是设置每一帧画面感兴趣的区域(字面意思),那岂不是就是设置扫描范围喽,大喜

    于是赶紧把 rectOfInterest 设置成中间框的frame,

    [ _output setRectOfInterest : CGRectMake (( ScreenWidth - 220 )/ 2 , 60 + 64 , 220 , 220 )];

    //中间区域的宽和高都是220   ScreenWidth为设备屏幕宽度

    但是却发现怎么扫描都不能成功了。于是又看了看上面的一段话。

    第二句:区域的原点在左上方(后面才知道坑苦我了!),然后区域是相对于设备的大小的,默认值是 CGRectMake(0, 0, 1, 1) ,这时候我才知道是有比例关系的,最大值才是1,也就是说只要除以相应的设备宽和高的大小不就行了?然后就改成

    [ _output setRectOfInterest : CGRectMake ((( ScreenWidth - 220 )/ 2 )/ ScreenWidth ,( 60 + 64 )/ ScreenHigh , 220 / ScreenWidth , 220 / ScreenHigh )];

    按说这样应该就完美了,但是才知道我还是高兴得太早了,一扫描才发现完全不是那么回事,差很多。

    于是我就一点一点调,但是最后也没调成功,最后一狠心有设置了一个很确定的值。

    [ _output   setRectOfInterest : CGRectMake ( 0.5 , 0.5 , 0.5 ,  0.5 )];

    这次应该很确定是在右下方的四分之一区域吧,嘿嘿。

    但是事实又一次打击了我,扫描后发现是左下的四分之一区域,也就是说 rectOfInterest的原点是右上角!!!

    回头又一想,即使右上角是原点那也应该没有影响啊,但是为什么不行呢,不会是原点的 X 和 Y 互换了吧?算了不管怎么着,试一试吧。

    [ _output setRectOfInterest : CGRectMake (( 60 + 64 )/ ScreenHigh ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenWidth , 220 / ScreenHigh )];

    又扫描了一下发现成功了!果然原点正确了,我只想说TMD!

    但是宽和高又怎么对不上了?不会也互换了吧!赶紧试试

    [ _output setRectOfInterest : CGRectMake (( 124 )/ ScreenHigh ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHigh , 220 / ScreenWidth )];

    转载http://www.th7.cn/program/ios/201411/323619.shtml

  • 相关阅读:
    动态更新活动条件
    使用本地服务异步执行自定义活动业务逻辑
    隐藏自定义复合活动的内部实现
    如何开发一个基本的复合活动
    HTML5性能之争 —— 单线程:缺点还是特点?
    CDNJS —— Web 上最快的 JavaScript 资源库
    Comfusion 4.1 发布,桌面 Linux 发行
    FreeBSD 9.1 正式版已经可以下载
    Squid Analyzer 5.0 发布,Squid日志统计
    MemSQL 1.8 发布,号称最快的关系数据库
  • 原文地址:https://www.cnblogs.com/liyanlan/p/4648896.html
Copyright © 2011-2022 走看看