QR COde Reader

add AVFoundation.framework
UIKit Framework

View.h:
@property (strong, nonatomic) IBOutlet UIView *mainV;

@property (strong, nonatomic) IBOutlet UILabel *labelll;
@property (strong, nonatomic) IBOutlet UIButton *butn;

- (IBAction)startClciked:(id)sender;

view.m:
#import "ViewController.h"

#import 

@interface ViewController ()
@property (nonatomic) BOOL isReading;
-(BOOL)startReading;
-(void)stopReading;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
_isReading = NO;

_captureSession = nil;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)startClciked:(id)sender {
if (!_isReading) {
if ([self startReading]) {
// [_bbitemStart setTitle:@"Stop"];
// [_lblStatus setText:@"Scanning for QR Code..."];
}
}
else{
[self stopReading];
// [_butn setTitle:@"Start!"];
}

_isReading = !_isReading;
}

- (IBAction)startStopReading:(id)sender {

}

- (BOOL)startReading {
NSError *error;

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}

_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input];

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];

dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:self.mainV.layer.bounds];
[self.mainV.layer addSublayer:_videoPreviewLayer];

[_captureSession startRunning];

return YES;

}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[_labelll performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];

[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
[_butn performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
_isReading = NO;
}
}
}

-(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil;

[_videoPreviewLayer removeFromSuperlayer];
}

@end
Previous
Next Post »