2016.1.18scan 二维码(仿照支付宝。微信)

1 生成二维码(原生方法生成二维码)

首先  要把二维码上的信息付给他 

 tempStr=self.textField.text;


其次 创建一个图片(在此之前需要导入#import "QRCodeGenerator.h"

 UIImage*tempImage=[QRCodeGenerator qrImageForString:tempStr imageSize:360 Topimg:image withColor:RandomColor];

    _outImageView.image=t 这是一个imageviewpImage;

2.扫描二维码

2.1添加手势长按识别

    UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];

//把手势添加到图片上

    [_outImageView addGestureRecognizer:longPress];

    [self.view addSubview:_outImageView]


2.2长按手势方法并输出显示

-(void)dealLongPress:(UIGestureRecognizer*)gesture{

//

    if(gesture.state==UIGestureRecognizerStateBegan){

        _timer.fireDate=[NSDate distantFuture];

        UIImageView*tempImageView=(UIImageView*)gesture.view;

        if(tempImageView.image){

            //1. 初始化扫描仪,设置设别类型和识别质量

            CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

            //2. 扫描获取的特征组

            NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];

            //3. 获取扫描结果

            CIQRCodeFeature *feature = [features objectAtIndex:0];

            NSString *scannedResult = feature.messageString;

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }else {

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:@"您还没有生成二维码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }

        

        

    }else if (gesture.state==UIGestureRecognizerStateEnded){

        

        //定时器  可不要

        _timer.fireDate=[NSDate distantPast];

    }


}

3 正常用相机扫描(导入 #import <AVFoundation/AVFoundation.h>

            #import "UIView+SDExtension.h"

创建session对象

@property (nonatomic, strong) AVCaptureSession *session;


{

    //获取摄像设备

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //创建输入流

    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input) return;

    //创建输出流

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

    //设置代理 在主线程里刷新

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

    //设置有效扫描区域

    CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];

     output.rectOfInterest = scanCrop;

    //初始化链接对象

    _session = [[AVCaptureSession alloc]init];

    //高质量采集率

    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    

    [_session addInput:input];

    [_session addOutput:output];

    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    

    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

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

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

    //开始捕获

    [_session startRunning];



4.选择一张图片然后 在扫描

#pragma mark-> 我的相册

-(void)myAlbum{

    NSLog(@"我的相册");

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

        //1.初始化相册拾取器

        UIImagePickerController *controller = [[UIImagePickerController alloc] init];

        //2.设置代理

        controller.delegate = self;

        //3.设置资源:

        /**

         UIImagePickerControllerSourceTypePhotoLibrary,相册

         UIImagePickerControllerSourceTypeCamera,相机

         UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片库

         */

        controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        //4.随便给他一个转场动画

        controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:controller animated:YES completion:NULL];

        

    }else{

        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备不支持访问相册,请在设置->隐私->照片中进行设置!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

    }

    

}


#pragma mark-> imagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //1.获取选择的图片

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    //2.初始化一个监测器

    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

    [picker dismissViewControllerAnimated:YES completion:^{

        //监测到的结果数组

        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];

        if (features.count >=1) {

            /**结果对象 */

            CIQRCodeFeature *feature = [features objectAtIndex:0];

            NSString *scannedResult = feature.messageString;

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }

        else{

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该图片没有包含一个二维码!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];   

        }

    }];

}


5 打开闪光灯

#pragma mark-> 闪光灯

-(void)openFlash:(UIButton*)button{

    NSLog(@"闪光灯");

    button.selected = !button.selected;

    if (button.selected) {

        [self turnTorchOn:YES];

    }

    else{

        [self turnTorchOn:NO];

    }

    

}


#pragma mark-> 开关闪光灯

- (void)turnTorchOn:(BOOL)on

{

    

    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");

    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        

        if ([device hasTorch] && [device hasFlash]){

            

            [device lockForConfiguration:nil];

            if (on) {

                [device setTorchMode:AVCaptureTorchModeOn];

                [device setFlashMode:AVCaptureFlashModeOn];

                

            } else {

                [device setTorchMode:AVCaptureTorchModeOff];

                [device setFlashMode:AVCaptureFlashModeOff];

            }

            [device unlockForConfiguration];

        }

    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值