//
// ViewController.m
// 条形码三
//
// Created by mac on 15/6/17.
// Copyright (c) 2015年 郑氏集团. All rights reserved.
//
#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (weak, nonatomic) IBOutlet UIView *viewPreview;
@property (weak, nonatomic) IBOutlet UILabel *lblStatus;
@property (weak, nonatomic) IBOutlet UIButton *bbitemStart;
@property (nonatomic) BOOL isReading;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
_isReading = NO;_captureSession = nil;
[self loadBeepSound];
// Do any additional setup after loading the view, typically from a nib.
} -
(IBAction)startStopReading:(id)sender {
if (!_isReading) {
if ([self stopReading]) {
// [_bbitemStart setTitle:@"Stop"];
[_lblStatus setText:@"Scanning for QR Code..."];
}
}
else{
[self startReading];
// [_bbitemStart setTitle:@"Start!"];
}_isReading = !_isReading;
} -
(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.metadataObjectTypes=@[ AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code];_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
[_viewPreview.layer addSublayer:_videoPreviewLayer];[_captureSession startRunning];
return YES;
}
-(BOOL)stopReading{
[_captureSession stopRunning];
_captureSession = nil;[_videoPreviewLayer removeFromSuperlayer];
return YES;
}
-(void)loadBeepSound{
NSString *beepFilePath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
NSError *error;_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
if (error) {
NSLog(@"Could not play beep file.");
NSLog(@"%@", [error localizedDescription]);
}
else{
[_audioPlayer prepareToPlay];
}
}
-(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]) {
[_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
[_bbitemStart performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
_isReading = NO;
if (_audioPlayer) {
[_audioPlayer play];
}
}
}
}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
@end