iOS 系统二维码扫描(可限制扫描区域)

摘要:
错误);}//3.设置输出(元数据元数据)AVCaptureMetadataOutput*output=[[AVCapturemetadataOutputloc]init];输入];输出];会话];_ previewLayer=预览:

   使用 AVFoundation系统库来进行二维码扫描并且限制扫描二维码的范围。(因为默认的是全屏扫描)

   

-(void)beginCode

{

    //1.摄像头设备

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    

    /*

     // Media types

     

     AVMediaTypeVideo

     AVMediaTypeAudio

     AVMediaTypeText

     AVMediaTypeClosedCaption

     AVMediaTypeSubtitle

     AVMediaTypeTimecode

     AVMediaTypeMetadata

     AVMediaTypeMuxed

     */

    

    //2.设置输入

    /**

     *  设置输入  此方法需要判断 因为模拟器没有摄像头

     *

     *  @param  把摄像头作为输入的设备

     *

     *  @return 返回AVCaptureInput

     */

    NSError *error = nil;

    AVCaptureInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    if (error) {

        NSLog(@"没有摄像头%@",error);

        return;

    }

    //3.设置输出(Metdata元数据)

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];

    //3.1输出的代理 捕获二维码的图像

    //dispatch_get_main_queue()使用主线程队列,响应比较同步,使用其他队列响应不同步。

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

    //3.2 设置扫描区域  默认是全屏扫描

//这个CGRectMake(Y,X,H,W) 1代表最大值    原点是左上角 为起始点

//    [output setRectOfInterest:CGRectMake(0, 0.5, 0.5, 0.5)];//左上角 1/4 屏幕

//    [output setRectOfInterest:CGRectMake(0.5, 0.5, 0.5, 0.5)];//左下角 1/4 屏幕

//    [output setRectOfInterest:CGRectMake(0.5, 0, 0.5, 0.5)]; //右下角 1/4 屏幕

//    [output setRectOfInterest:CGRectMake(0, 0, 0.5, 0.5)]; //右上角 1/4 屏幕

//     [output setRectOfInterest:CGRectMake((124)/ScreenHigh,          ((ScreenWidth220)/2)/ScreenWidth,220/ScreenHigh,220/ScreenWidth)]; //设置自定义像素点的 位置

    [output setRectOfInterest:CGRectMake(0.25,0.25, 0.5, 0.5)]; //貌似 中间的感觉!!!

    //4.拍摄会话

    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    _session = session;

    //添加session的输入和输出

    [session addInput:input];

    [session addOutput:output];

    

    //4.1设置输出的格式

    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    

    //5.设置预览图层 (让用户看到扫描结果)

    AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:session];

    _previewLayer = preview;

    //5.1设置preview的属性

    preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

    //5.2设置preview的图层的大小

    [preview setFrame:self.view.bounds];

    //5.3将图层添加到视图的图层

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

    

    //6.启动会话

    [session startRunning];

}

#pragma mark 输出的代理方法

//此方法是在识别到QRCode,并且完成转换

//如果QRCode的内容越大,转换需要的时间就越长

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

{

    //会频繁的扫描

    //如果扫描完成就停止

    [_session stopRunning];

    //删除预览的图层

    [_previewLayer removeFromSuperlayer];

    

    //设置界面显示扫描结果

    NSString *reslutStr = [[NSString alloc] init];

    if (metadataObjects.count > 0) {

        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];

        reslutStr = [obj stringValue];//这个就是 扫描的结果

        //如果需要对URL 名片 等信息进行扫描 再次进行扩展

    }

    NSLog(@"%@",metadataObjects);

}

//关于设置扫描区域的解释

 @abstract  摘要

Specifies a rectangle of interest for limiting the search area for visual metadata.

   指定一个矩形限制搜索区域的视觉感兴趣的元数据。

 

 @discussion 讨论

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

     这个属性的值是CGRect中决定了接收机的矩形感兴趣的视频的每一帧

    

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可能改善检测性能对于某些类型的元数据。此属性的默认值价值CGRectMake(0,0,1,1)。元数据对象的边界不相交的rectOfInterest恕不退还

//CGRectMake(Y,X,H,W)  这个 坐标顺序很重要

免责声明:文章转载自《iOS 系统二维码扫描(可限制扫描区域)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇实现内网端口映射的方法Centos6 修改max user processes limits下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

手把手写一个html_json信息源

html_json用于从网页里提取json数据。 这里用新浪读书的书讯举个例子,手把手写一个html_json信息源。 打开新浪读书的首页,可以看到页面下方有最新、书讯、童书、小说等几个Tab,这里我们提取书讯的内容。 查看页面的源码,找不到书讯的内容。 这是因为网站使用了json技术:额外加载了json对象、并通过javascript把json的内容显...

Elasticsearch查询

Elasticsearch支持两种类型的查询:基本查询和复合查询。 基本查询,如词条查询用于查询实际数据。 复合查询,如布尔查询,可以合并多个查询, 然而,这不是全部。除了这两种类型的查询,你还可以用过滤查询,根据一定的条件缩小查询结果。不像其他查询,筛选查询不会影响得分,而且通常非常高效。 更加复杂的情况,查询可以包含其他查询。此外,一些查询可以包含过滤...

【Mybatis-Plus】使用updateById()、update()将字段更新为null或者空

问题背景: 最近测试同学给我提了个bug,字段不能置空,我查看了下项目配置发现是字段级别被设置为NOT_EMPTY导致的。 mybatis-plus FieldStrategy 有三种策略: 1.IGNORED:0 忽略 2.NOT_NULL:1 非 NULL,默认策略 3.NOT_EMPTY:2 非空 而默认更新策略是NOT_NULL:非 NULL;即通...

29网络偷包

偷包,就是在网络传输过程中,截取某一数据包,进行解析获取其发送的数据。 原理与TCP通信类似。只需在创建套接字时,参数不同。 fd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP); PF_INET                      IPV4协议 SOCK_RAW            对原始网络协议访问 IPPRO...

npm ERR! mathine_call@1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js` npm ERR! Exit status 1

internal/modules/cjs/loader.js:583 throw err; ^ Error: Cannot find module 'webpack' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15) at...

主动扫描-Nmap

仅供个人学习! 主动扫描   相对于被动扫描而言,主动扫描的范围要小得多。主动扫描一般都是针对目标发送特制的数据包,然后根据目标的反应来获得一些信息。这些信息主要包括目标主机是否在线、目标主机的指定端口是否开放、目标主机的操作系统、目标主机上运行的服务等。 Namp   攻击只是Nmap的副业,扫描才是Nmap的主要功能。   2009年7月17日,开源网...