美文网首页
二维码的生成与扫描

二维码的生成与扫描

作者: 狒狒James_Leo | 来源:发表于2017-09-14 11:03 被阅读0次
    //生成二维码
    
        //创建imageView来显示二维码
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
        imgView.center = self.view.center;
        [self.view addSubview:imgView];
        
        //图标的生成
        UIImage *log = [UIImage imageNamed:@"JL"];
        
        //二维码的生成
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [filter setDefaults];
        NSString *input = @"https://www.baidu.com";
        NSData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding];
        
        [filter setValue:inputData forKey:@"inputMessage"];
        
        //获得滤镜输出的二维码
        CIImage *ciImg = filter.outputImage;
        
        ciImg = [ciImg imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
        
        UIImage *img = [UIImage imageWithCIImage:ciImg];
        //开启图片上下文
        UIGraphicsBeginImageContext(imgView.frame.size);
        //绘制图片
        [img drawInRect:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
        
        CGFloat logX = (imgView.frame.size.width - log.size.width)/2;
        CGFloat logY = (imgView.frame.size.height - log.size.height)/2;
        [log drawInRect:CGRectMake(logX, logY, log.size.width, log.size.height)];
        //获取图片
        UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
        //关闭上下文
        UIGraphicsEndImageContext();
        
        imgView.image = finalImage;
    
    //扫描二维码
    @interface ScaleViewController ()<AVCaptureMetadataOutputObjectsDelegate>
    
    @property (nonatomic, strong) UIView *scanRectView;
    
    @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;
    @end
    
    @implementation ScaleViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
    
        self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
        self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
        self.output = [[AVCaptureMetadataOutput alloc]init];
        [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
        self.session = [[AVCaptureSession alloc]init];
        [self.session setSessionPreset:([UIScreen mainScreen].bounds.size.height<500)?AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];
        [self.session addInput:self.input];
        [self.session addOutput:self.output];
        self.output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];
        CGSize windowSize = [UIScreen mainScreen].bounds.size;
        
        CGSize scanSize = CGSizeMake(windowSize.width*3/4, windowSize.width*3/4);
        CGRect scanRect = CGRectMake((windowSize.width-scanSize.width)/2, (windowSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
        
        //计算rectOfInterest 注意x,y交换位置
        scanRect = CGRectMake(scanRect.origin.y/windowSize.height, scanRect.origin.x/windowSize.width, scanRect.size.height/windowSize.height,scanRect.size.width/windowSize.width);
        
        self.scanRectView = [UIView new];
        [self.view addSubview:self.scanRectView];
        self.scanRectView.frame = CGRectMake(0, 0, scanSize.width, scanSize.height);
        self.scanRectView.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));
        self.scanRectView.layer.borderColor = [UIColor redColor].CGColor;
        self.scanRectView.layer.borderWidth = 1;
        
        self.output.rectOfInterest = scanRect;
    
        self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
        self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
        self.preview.frame = [UIScreen mainScreen].bounds;
        [self.view.layer insertSublayer:self.preview atIndex:0];
    
    //开始捕获
    [self.session startRunning];
    
    }
    
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        if ( (metadataObjects.count==0) )
        {
            return;
        }
        
        if (metadataObjects.count>0) {
            
            [self.session stopRunning];
            
            AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject;
            //输出扫描字符串
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:metadataObject.stringValue message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
            
            [alert show];
        }
    }
    
    
    
    
    
        ```

    相关文章

      网友评论

          本文标题:二维码的生成与扫描

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