標籤:
#import "RootViewController.h"
#import AVFoundation/AVFoundation.h //兩邊角括弧 顯示不出來
@interface RootViewController () AVCaptureMetadataOutputObjectsDelegate //協議 道理同上
@property (nonatomic, strong) UILabel *showLabel;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.showLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.bounds.size.width-40, 30)];
self.showLabel.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.showLabel];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(20, 300, self.view.bounds.size.width-40, 300);
[self.button setTitle:@"開始" forState:UIControlStateNormal];
[self.button setTitle:@"停止" forState:UIControlStateSelected];
[self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)buttonClick:(UIButton *)button {
button.selected = !button.selected;
if (button.selected) {
[self startReadingMachineReadableCodeObjects:@[AVMetadataObjectTypeQRCode] inView:self.view];
} else {
[self stopReading];
}
}
// 開始掃描
- (void)startReadingMachineReadableCodeObjects:(NSArray *)codeObjects inView:(UIView *)preview {
// 網路攝影機裝置
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
// 設定輸入口
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (error || !input) {
NSLog(@"error: %@", [error description]);
return;
}
// 會話session, 把輸入口加入會話
self.session = [[AVCaptureSession alloc] init];
[self.session addInput:input]; // 將輸入添加到session
// 設定輸出口,加入session, 設定輸出口參數
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[self.session addOutput:output];
[output setMetadataObjectTypes:codeObjects];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 使用主線程隊列,相應比較同步,使用其他隊列,相應不同步,容易讓使用者產生不好的體驗
// 設定展示層(預覽層)
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session]; // 設定預覽層資訊
self.preview.frame = preview.bounds;
[preview.layer insertSublayer:self.preview atIndex:0]; // 添加至視圖
[self beginReading];
}
// 啟動session
- (void)beginReading
{
[self.session startRunning];
}
// 關閉session
- (void)stopReading
{
self.button.selected = NO;
[self.session stopRunning];
[self.preview removeFromSuperlayer];
}
// 識別到二維碼 並解析轉換為字串
#pragma mark -
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
[self stopReading];
AVMetadataObject *metadata = [metadataObjects objectAtIndex:0];
NSString *codeStr = nil;
if ([metadata respondsToSelector:@selector(stringValue)]) {
codeStr = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
}
self.showLabel.text = codeStr;
}
@end
iOS系統方法掃描二維碼