简单实现二维码的生成和扫描

这里我用了系统的东西简单的实现了一下扫描二维码

.h

#import

#import

@interfacesaoMiaoViewController :UIViewController

@property(nonatomic,retain)UIView* sanRectView;

//硬件设备

@property(nonatomic,retain)AVCaptureDevice* device;

//输入设备

@property(nonatomic,retain)AVCaptureDeviceInput* input;

//输出设备

@property(nonatomic,retain)AVCaptureMetadataOutput* outPut;

//桥梁。链接输入和输出设备

@property(nonatomic,retain)AVCaptureSession* session;

@property(nonatomic,retain)AVCaptureVideoPreviewLayer* preView;

@end

接下来看.m文件

@interfacesaoMiaoViewController()

@end

@implementationsaoMiaoViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.title=@"进入连接";

CGSizewindowSize = [UIScreenmainScreen].bounds.size;

CGSizescanSize =CGSizeMake(windowSize.width*3/5, windowSize.width* 3/5);

CGRectscanRect =CGRectMake( ((windowSize.height-scanSize.height)/2)/windowSize.height,((windowSize.width-scanSize.width)/2)/windowSize.width,  (scanSize.height+10)/windowSize.height,(scanSize.width+ 10)/windowSize.width);

self.device= [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

self.input= [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];

self.outPut= [[AVCaptureMetadataOutputalloc]init];

[self.outPutsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

self.session= [[AVCaptureSessionalloc]init];

[self.sessionsetSessionPreset:([UIScreenmainScreen].bounds.size.height<500)?AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];

[self.sessionaddInput:self.input];

[self.sessionaddOutput:self.outPut];

self.outPut.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];

self.preView= [AVCaptureVideoPreviewLayerlayerWithSession:self.session];

self.preView.videoGravity=AVLayerVideoGravityResizeAspectFill;

self.preView.frame= [UIScreenmainScreen].bounds;

self.preView.backgroundColor= [UIColorcolorWithRed:1green:1blue:1alpha:0.7].CGColor;

[self.view.layerinsertSublayer:self.preViewatIndex:0];

self.sanRectView= [UIViewnew];

[self.viewaddSubview:self.sanRectView];

self.sanRectView.frame=CGRectMake(0, 0, scanSize.width, scanSize.height);

self.sanRectView.center=CGPointMake(CGRectGetMidX([UIScreenmainScreen].bounds),CGRectGetMidY([UIScreenmainScreen].bounds));

self.sanRectView.layer.borderColor= [UIColorwhiteColor].CGColor;

self.sanRectView.layer.borderWidth= 2;

self.outPut.rectOfInterest=scanRect;

//开始捕获

[self.sessionstartRunning];

}

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

{

if( (metadataObjects.count==0) )

{

return;

}

if(metadataObjects.count>0) {

[self.sessionstopRunning];

AVMetadataMachineReadableCodeObject*metadataObject = metadataObjects.firstObject;

SendUrlViewController* send = [[SendUrlViewControlleralloc]init];

send.nameUrl= metadataObject.stringValue;

[self.navigationControllerpushViewController:sendanimated:YES];

}

}

此文纯属个人为了记忆而写。当然要是能帮到别人当然也很开心了A.A

你可能感兴趣的:(简单实现二维码的生成和扫描)