iOS二维码开发

释放双眼,带上耳机,听听看~!

生成二维码

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);

人已赞赏
iOS文章

iOS UIImage Category

2020-2-21 18:22:29

iOS文章

Xcode UI界面调试神器-injectionIII

2020-2-21 18:46:52

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索