关闭

iOS二维码

标签: 二维码qrios
236人阅读 评论(0) 收藏 举报
分类:

生成二维码

CIFilter *qrfilter=[CIFilter filterWithName:@"CIQRCodeGenerator"];
    NSData *data=[@"开心(>_<)" dataUsingEncoding:NSUTF8StringEncoding];

    [qrfilter setValue:data forKey:@"inputMessage"];
    [qrfilter setDefaults];

    //取出二维码图片
    CIImage *qrCIImage=qrfilter.outputImage;
    qrCIImage=[qrCIImage imageByApplyingTransform:CGAffineTransformMakeScale(8, 8)];
        CIFilter *falseColorFilter=[CIFilter filterWithName:@"CIFalseColor"];

    [falseColorFilter setValue:qrCIImage forKey:@"inputImage"];
    [falseColorFilter setValue:[CIColor colorWithRed:168/255.0 green:200/255.0 blue:102/255.0] forKey:@"inputColor0"];
    //   取出图片
    qrCIImage = falseColorFilter.outputImage;
    UIImage *qrImage  = [UIImage imageWithCIImage:qrCIImage];
    //显示
    self.qrImgView.image=qrImage;

扫描二维码

导入框架AVFoundation.h

#import <AVFoundation/AVFoundation.h>
@interface XYQRViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property(nonatomic,strong)AVCaptureSession *session;

@end

@implementation XYQRViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (error) {
        NSLog(@"%@",error);
        return;
    }
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];

    self.session = [[AVCaptureSession alloc]init];
    if ([self.session canAddInput:input]) {
        [self.session addInput:input];

    }

    if ([self.session canAddOutput:output]) {
        [self.session addOutput:output];
    }
    //    NSLog(@"%@", output.availableMetadataObjectTypes);
    output.metadataObjectTypes=@[@"org.iso.QRCode"];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //开始扫面
    [self.session startRunning];
    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    layer.frame = self.view.bounds;
    [self.view.layer addSublayer:layer];
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    //    NSLog(@"%@",metadataObjects);
    AVMetadataMachineReadableCodeObject *objc=[metadataObjects firstObject];
    NSLog(@"%@",objc.stringValue);
    self.dataBlcok(objc.stringValue);
    [self.session stopRunning];
}

设置解析范围

设置AVCaptureMetadataOutput 的 rectOfInterest即可,减小解析范围可提高扫描速度。rectOfInterest是相对图片的比例,需要适配不同屏幕,还需在以下基础上优化

CGSize size = self.view.bounds.size;
CGRect cropRect = CGRectMake(40, 100, 240, 240); 
output.rectOfInterest = CGRectMake(cropRect.origin.y/size.height, cropRect.origin.x/size.width, cropRect.size.height/size.height, cropRect.size.width/size.width);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46036次
    • 积分:1225
    • 等级:
    • 排名:千里之外
    • 原创:80篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    文章分类