二维码

作者: 帥陽 | 来源:发表于2017-02-08 15:43 被阅读6次

    filter 滤镜

    使用二维码要使用这个框架

    生成二维码

    #import"ViewController.h"

    #import

    @interfaceViewController()

    @property(weak,nonatomic)IBOutletUIImageView*Qrcode;//Qr code二维码

    @end

    @implementationViewController

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

    //1.实例化二维码滤镜CIQRCodeGenerator是一个固定的值

    CIFilter*filter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];

    //2.恢复滤镜的默认属性,因为可能保存有上一次滤镜的属性

    [filtersetDefaults];

    //3.将字符串转为NSData数据

    NSData*data = [@"http://www.baidu.com"dataUsingEncoding:NSUTF8StringEncoding];

    //4.通过KVO设置滤镜,传入data ,将来滤镜就知道通过data来生成二维码

    //inputMessage是一个固定的值

    [filtersetValue:dataforKey:@"inputMessage"];

    //5.通过滤镜生成二维码

    CIImage*ciImage = [filteroutputImage];

    //6.将CIImage转为UIImage

    UIImage*uiImage = [UIImageimageWithCIImage:ciImage];

    //7.展示二维码

    self.Qrcode.image= uiImage;

    }

    @end

    扫描二维码

    #import"ViewController.h"

    #import

    @interfaceViewController()

    @property(nonatomic,strong)AVCaptureSession*session;

    @property(nonatomic,strong)AVCaptureVideoPreviewLayer*preview;

    @property(weak,nonatomic)IBOutletUILabel*labelView;

    @end

    @implementationViewController

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

    //1.实例化拍摄设备

    AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    //2.设置输入设备

    AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];

    //3.设置元数据输出

    //3.1实例化拍摄元数据输出

    AVCaptureMetadataOutput*output = [[AVCaptureMetadataOutputalloc]init];

    //3.2设置输出数据的代理

    [outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

    //4.添加拍摄会话

    //4.1实例化拍摄会话

    AVCaptureSession*session = [[AVCaptureSessionalloc]init];

    //4.2添加输出会话

    [sessionaddOutput:output];

    //4.3添加输入会话

    [sessionaddInput:input];

    //4.4设置输出数据类型,必须先将元数据输出添加到会话中,才能设置元数据类型,否则会报错

    [outputsetMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    self.session= session;

    //5.实例化预览图层

    //5.1实例化预览图层,传递_session是为了告诉预览图层要显示什么内容的

    AVCaptureVideoPreviewLayer*preview = [AVCaptureVideoPreviewLayerlayerWithSession:_session];

    //5.2设置预览图层的属性

    preview.videoGravity=AVLayerVideoGravityResizeAspectFill;

    preview.frame=self.view.bounds;

    //5.3将图层添加到当前视图

    [self.view.layerinsertSublayer:previewatIndex:100];

    self.preview= preview;

    //6.启动会话

    [_sessionstartRunning];

    }

    /**

    *  AVCaptureMetadataOutputObjectsDelegate方法

    */

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

    //1.如果扫描成功,停止会话

    [self.sessionstopRunning];

    //2.移除预览图层

    [self.previewremoveFromSuperlayer];

    if(metadataObjects.count>0) {

    AVMetadataMachineReadableCodeObject*obj = metadataObjects[0];

    self.labelView.text= obj.stringValue;

    }

    }

    @end

    相关文章

      网友评论

          本文标题:二维码

          本文链接:https://www.haomeiwen.com/subject/ofzvittx.html